Created
November 15, 2019 18:13
-
-
Save ayaysir/43fac1154644eaf4178197cbe916fe66 to your computer and use it in GitHub Desktop.
간이 로그 기록 예제
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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