A more compact formatter. Equivalent log4j config: log4j.rootCategory=WARN, A1 log4j.appender.A1=org.apache.log4j.ConsoleAppender log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.Target=System.err log4j.appender.A1.layout.ConversionPattern=%r %-20.20c{2} %-1.1p %
| 33 | * Example: 1130122891846 Http11BaseProtocol I Initializing Coyote HTTP/1.1 on http-8800 |
| 34 | */ |
| 35 | public class JdkLoggerFormatter extends Formatter { |
| 36 | |
| 37 | /** |
| 38 | * Constructs a new JdkLoggerFormatter. |
| 39 | */ |
| 40 | public JdkLoggerFormatter() { |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Log level value for TRACE. |
| 45 | */ |
| 46 | public static final int LOG_LEVEL_TRACE = 400; |
| 47 | /** |
| 48 | * Log level value for DEBUG. |
| 49 | */ |
| 50 | public static final int LOG_LEVEL_DEBUG = 500; |
| 51 | /** |
| 52 | * Log level value for INFO. |
| 53 | */ |
| 54 | public static final int LOG_LEVEL_INFO = 800; |
| 55 | /** |
| 56 | * Log level value for WARN. |
| 57 | */ |
| 58 | public static final int LOG_LEVEL_WARN = 900; |
| 59 | /** |
| 60 | * Log level value for ERROR. |
| 61 | */ |
| 62 | public static final int LOG_LEVEL_ERROR = 1000; |
| 63 | /** |
| 64 | * Log level value for FATAL. |
| 65 | */ |
| 66 | public static final int LOG_LEVEL_FATAL = 1000; |
| 67 | |
| 68 | @Override |
| 69 | public String format(LogRecord record) { |
| 70 | Throwable t = record.getThrown(); |
| 71 | int level = record.getLevel().intValue(); |
| 72 | String name = record.getLoggerName(); |
| 73 | long time = record.getMillis(); |
| 74 | String message = formatMessage(record); |
| 75 | |
| 76 | if (name == null) { |
| 77 | name = ""; |
| 78 | } |
| 79 | if (name.indexOf('.') >= 0) { |
| 80 | name = name.substring(name.lastIndexOf('.') + 1); |
| 81 | } |
| 82 | |
| 83 | // Use a string buffer for better performance |
| 84 | StringBuilder buf = new StringBuilder(); |
| 85 | |
| 86 | buf.append(time); |
| 87 | |
| 88 | // Append a readable representation of the log level. |
| 89 | switch (level) { |
| 90 | case LOG_LEVEL_TRACE: |
| 91 | buf.append(" T "); |
| 92 | break; |
nothing calls this directly
no outgoing calls
no test coverage detected