diff options
Diffstat (limited to 'src/net/java/sip/communicator/util/ScLogFormatter.java')
-rw-r--r-- | src/net/java/sip/communicator/util/ScLogFormatter.java | 123 |
1 files changed, 105 insertions, 18 deletions
diff --git a/src/net/java/sip/communicator/util/ScLogFormatter.java b/src/net/java/sip/communicator/util/ScLogFormatter.java index ad2129b..5d22db5 100644 --- a/src/net/java/sip/communicator/util/ScLogFormatter.java +++ b/src/net/java/sip/communicator/util/ScLogFormatter.java @@ -33,11 +33,53 @@ import java.util.logging.*; public class ScLogFormatter extends java.util.logging.Formatter { + + /** + * Program name logging property name + */ + private static final String PROGRAM_NAME_PROPERTY = ".programname"; + + /** + * Disable timestamp logging property name. + */ + private static final String DISABLE_TIMESTAMP_PROPERTY + = ".disableTimestamp"; + + /** + * Line separator used by current platform + */ private static String lineSeparator = System.getProperty("line.separator"); + + /** + * Two digit <tt>DecimalFormat</tt> instance, used to format datetime + */ private static DecimalFormat twoDigFmt = new DecimalFormat("00"); + + /** + * Three digit <tt>DecimalFormat</tt> instance, used to format datetime + */ private static DecimalFormat threeDigFmt = new DecimalFormat("000"); /** + * The application name used to generate this log + */ + private static String programName; + + /** + * Whether logger will add date to the logs, enabled by default. + */ + private static boolean timestampDisabled = false; + + /** + * The default constructor for <tt>ScLogFormatter</tt> which loads + * program name property from logging.properties file, if it exists + */ + public ScLogFormatter() + { + loadConfigProperties(); + } + + /** * Format the given LogRecord. * @param record the log record to be formatted. * @return a formatted log record @@ -46,24 +88,36 @@ public class ScLogFormatter public synchronized String format(LogRecord record) { StringBuffer sb = new StringBuffer(); + + + if (programName != null) + { + // Program name + sb.append(programName); + + sb.append(' '); + } - //current time - Calendar cal = Calendar.getInstance(); - int year = cal.get(Calendar.YEAR); - int month = cal.get(Calendar.MONTH) + 1; - int day = cal.get(Calendar.DAY_OF_MONTH); - int hour = cal.get(Calendar.HOUR_OF_DAY); - int minutes = cal.get(Calendar.MINUTE); - int seconds = cal.get(Calendar.SECOND); - int millis = cal.get(Calendar.MILLISECOND); - - sb.append(year).append('-'); - sb.append(twoDigFmt.format(month)).append('-'); - sb.append(twoDigFmt.format(day)).append(' '); - sb.append(twoDigFmt.format(hour)).append(':'); - sb.append(twoDigFmt.format(minutes)).append(':'); - sb.append(twoDigFmt.format(seconds)).append('.'); - sb.append(threeDigFmt.format(millis)).append(' '); + if(!timestampDisabled) + { + //current time + Calendar cal = Calendar.getInstance(); + int year = cal.get(Calendar.YEAR); + int month = cal.get(Calendar.MONTH) + 1; + int day = cal.get(Calendar.DAY_OF_MONTH); + int hour = cal.get(Calendar.HOUR_OF_DAY); + int minutes = cal.get(Calendar.MINUTE); + int seconds = cal.get(Calendar.SECOND); + int millis = cal.get(Calendar.MILLISECOND); + + sb.append(year).append('-'); + sb.append(twoDigFmt.format(month)).append('-'); + sb.append(twoDigFmt.format(day)).append(' '); + sb.append(twoDigFmt.format(hour)).append(':'); + sb.append(twoDigFmt.format(minutes)).append(':'); + sb.append(twoDigFmt.format(seconds)).append('.'); + sb.append(threeDigFmt.format(millis)).append(' '); + } //log level sb.append(record.getLevel().getLocalizedName()); @@ -146,7 +200,8 @@ public class ScLogFormatter } ix++; } - // Now search for the first frame before the SIP Communicator Logger class. + // Now search for the first frame + // before the SIP Communicator Logger class. while (ix < stack.length) { StackTraceElement frame = stack[ix]; @@ -164,4 +219,36 @@ public class ScLogFormatter return lineNumber; } + + /** + * Loads all config properties. + */ + private void loadConfigProperties() + { + loadProgramNameProperty(); + loadTimestampDisabledProperty(); + } + + /** + * Checks and loads timestamp disabled property if any. + */ + private static void loadTimestampDisabledProperty() + { + LogManager manager = LogManager.getLogManager(); + String cname = ScLogFormatter.class.getName(); + timestampDisabled = Boolean.parseBoolean( + manager.getProperty(cname + DISABLE_TIMESTAMP_PROPERTY)); + } + + /** + * Load the programname property to be used in logs to identify Jitsi-based + * application which produced the logs + */ + private static void loadProgramNameProperty() + { + LogManager manager = LogManager.getLogManager(); + String cname = ScLogFormatter.class.getName(); + programName = manager.getProperty(cname + PROGRAM_NAME_PROPERTY); + } + } |