Skip to content

Instantly share code, notes, and snippets.

@ayaysir
Created November 15, 2019 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayaysir/43fac1154644eaf4178197cbe916fe66 to your computer and use it in GitHub Desktop.
Save ayaysir/43fac1154644eaf4178197cbe916fe66 to your computer and use it in GitHub Desktop.
간이 로그 기록 예제
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
public class LogSave{
private static String filePath = "log.txt";
public static void write(Object o) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd kk:mm:ss", Locale.getDefault());
String date = sdf.format(cal.getTime());
String out = "[" + date + "] " + o;
System.out.println(out);
try(BufferedWriter bw = new BufferedWriter(new FileWriter(filePath, true))){
bw.append(out);
bw.newLine();
} catch(Exception e) {
e.printStackTrace();
}
}
public static String getFilePath() {
return filePath;
}
public static void setFilePath(String filePath) {
LogSave.filePath = filePath;
}
}
public class LogSaveTest {
public static void main(String[] args) {
System.out.println(LogSave.getFilePath());
LogSave.setFilePath("log2.txt");
LogSave.write("초기화를 실시합니다.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment