I'm using a simple logger on a server, which uses a method that looks like this:
@Override
public synchronized void write(String logtag, String reason, int severity) {
long logtime = System.currentTimeMillis();
String message = "";
switch (severity) {
case DEBUG:
if (!debug) return;
message = "[" + logtime + "] {DEBUG} " + logtag + "% " + reason + "\n$";
break;
case INFO:
message = "[" + logtime + "] {INFO} " + logtag + "% " + reason + "\n$";
break;
case ERROR:
message = "[" + logtime + "] {ERROR} " + logtag + "% " + reason + "\n$";
break;
case FATAL:
message = "[" + logtime + "] {FATAL} " + logtag + "% " + reason + "\n$";
break;
case EXCEPTION:
message = "[" + logtime + "] {EXCEPTION} " + logtag + "% " + reason + "\n$";
break;
default:
message = "Anthrazit error! Invalid severity: " + severity;
}
try {
out.write(message.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
errorOnInit(e);
}
if (exitOnFatal && severity == FATAL) {
System.err.println("Anthrazit exit on fatal: " + message + ". Please check the logs.");
close();
System.exit(1);
}
}
After doing some research I found some classes designed to to this, like PrintWriter
and others. Is there any performance difference between my approach and the PrintWriter
s?