Skip to content

Instantly share code, notes, and snippets.

@tpoerschke
Created July 11, 2019 15:36
Show Gist options
  • Save tpoerschke/fdb4a80d8fcf51048e59c824fbd67f0e to your computer and use it in GitHub Desktop.
Save tpoerschke/fdb4a80d8fcf51048e59c824fbd67f0e to your computer and use it in GitHub Desktop.
A simple logging class written in Java.
package logging;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class SimpleLoggingUtil {
private File logFile;
public SimpleLoggingUtil(File logFile) {
this.logFile = logFile;
if(!logFile.exists()) {
try {
logFile.createNewFile();
} catch (IOException e) {
//e.printStackTrace();
}
}
}
public void info(String message) {
try {
Files.write(logFile.toPath(), buildMessage("INFO", message).getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) {}
}
public void warning(String message) {
try {
Files.write(logFile.toPath(), buildMessage("WARNING", message).getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) {}
}
public void warning(Exception ex) {
try {
Files.write(logFile.toPath(), buildMessage("WARNING", ex).getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) {}
}
public void error(String message) {
try {
Files.write(logFile.toPath(), buildMessage("ERROR", message).getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) {}
}
public void error(Exception ex) {
try {
Files.write(logFile.toPath(), buildMessage("ERROR", ex).getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) {}
}
private String buildMessage(String type, Exception ex) {
String output = getDateTime() + " " + type + " " + ex.getMessage() + "\n";
for(StackTraceElement ste : ex.getStackTrace()) {
output += ste.toString() + "\n";
}
output += "====================\n";
return output;
}
private String buildMessage(String type, String message) {
String output = getDateTime() + " " + type + " " + message + "\n";
output += "====================\n";
return output;
}
private String getDateTime() {
return "[" + LocalDateTime.now().format(
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
.withLocale(Locale.getDefault()).withZone(ZoneId.systemDefault())) + "]";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment