-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use custom log formatter for journald logging
- Loading branch information
1 parent
c15a69f
commit f5e729d
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
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
83 changes: 83 additions & 0 deletions
83
src/main/java/app/attestation/server/JournaldFormatter.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package app.attestation.server; | ||
|
||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.util.Date; | ||
import java.util.logging.Formatter; | ||
import java.util.logging.Level; | ||
import java.util.logging.LogManager; | ||
import java.util.logging.LogRecord; | ||
|
||
class JournaldFormatter extends Formatter { | ||
private static final int LEVEL_EMERG = 0; | ||
private static final int LEVEL_ALERT = 1; | ||
private static final int LEVEL_CRIT = 2; | ||
private static final int LEVEL_ERR = 3; | ||
private static final int LEVEL_WARNING = 4; | ||
private static final int LEVEL_NOTICE = 5; | ||
private static final int LEVEL_INFO = 6; | ||
private static final int LEVEL_DEBUG = 7; | ||
|
||
private static int toSyslogLevel(final Level level) { | ||
final int value = level.intValue(); | ||
if (value >= 1300) { | ||
return LEVEL_EMERG; | ||
} | ||
if (value >= 1200) { | ||
return LEVEL_ALERT; | ||
} | ||
if (value >= 1100) { | ||
return LEVEL_CRIT; | ||
} | ||
// Level.SEVERE 1000 | ||
if (value >= 1000) { | ||
return LEVEL_ERR; | ||
} | ||
// Level.WARNING 900 | ||
if (value >= 900) { | ||
return LEVEL_WARNING; | ||
} | ||
if (value >= 850) { | ||
return LEVEL_NOTICE; | ||
} | ||
// Level.INFO 800 | ||
if (value >= 800) { | ||
return LEVEL_INFO; | ||
} | ||
// Level.CONFIG 700 | ||
// Level.FINE 500 | ||
// Level.FINER 400 | ||
// Level.FINEST 300 | ||
return LEVEL_DEBUG; | ||
} | ||
|
||
public synchronized String format(LogRecord record) { | ||
String source; | ||
if (record.getSourceClassName() != null) { | ||
source = record.getSourceClassName(); | ||
if (record.getSourceMethodName() != null) { | ||
source += " " + record.getSourceMethodName(); | ||
} | ||
} else { | ||
source = record.getLoggerName(); | ||
} | ||
final int level = toSyslogLevel(record.getLevel()); | ||
final String message = formatMessage(record); | ||
String throwable = ""; | ||
if (record.getThrown() != null) { | ||
final String newline = "\n<" + toSyslogLevel(record.getLevel()) + ">"; | ||
final StringWriter sw = new StringWriter(); | ||
final PrintWriter pw = new PrintWriter(sw) { | ||
@Override | ||
public void println() { | ||
write(newline); | ||
} | ||
}; | ||
pw.println(); | ||
record.getThrown().printStackTrace(pw); | ||
pw.close(); | ||
throwable = sw.toString(); | ||
} | ||
return String.format("<%s>%s: %s%s\n", level, source, message, throwable); | ||
} | ||
} |