From 322c973a7a01363e6ae9400270ef085871438104 Mon Sep 17 00:00:00 2001 From: erickson Date: Fri, 3 Aug 2007 15:49:27 +0000 Subject: [PATCH] adding basic logging api with a filelogger version and a test module git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1073 9efc2488-bf62-4759-914b-345cdb29e865 --- src/java/org/opensrf/test/TestLog.java | 15 ++++ src/java/org/opensrf/util/FileLogger.java | 44 ++++++++++ src/java/org/opensrf/util/Logger.java | 130 ++++++++++++++++++++++++++++++ 3 files changed, 189 insertions(+) create mode 100644 src/java/org/opensrf/test/TestLog.java create mode 100644 src/java/org/opensrf/util/FileLogger.java create mode 100644 src/java/org/opensrf/util/Logger.java diff --git a/src/java/org/opensrf/test/TestLog.java b/src/java/org/opensrf/test/TestLog.java new file mode 100644 index 0000000..1d60242 --- /dev/null +++ b/src/java/org/opensrf/test/TestLog.java @@ -0,0 +1,15 @@ +package org.opensrf.test; +import org.opensrf.util.Logger; +import org.opensrf.util.FileLogger; + + +/** Simple test class for tesing the logging functionality */ +public class TestLog { + public static void main(String args[]) { + Logger.init(Logger.DEBUG, new FileLogger("test.log")); + Logger.error("Hello, world"); + Logger.warn("Hello, world"); + Logger.info("Hello, world"); + Logger.debug("Hello, world"); + } +} diff --git a/src/java/org/opensrf/util/FileLogger.java b/src/java/org/opensrf/util/FileLogger.java new file mode 100644 index 0000000..9eb838d --- /dev/null +++ b/src/java/org/opensrf/util/FileLogger.java @@ -0,0 +1,44 @@ +package org.opensrf.util; +import java.io.BufferedWriter; +import java.io.FileWriter; + + +public class FileLogger extends Logger { + + /** File to log to */ + private String filename; + + /** + * FileLogger constructor + * @param filename The path to the log file + */ + public FileLogger(String filename) { + this.filename = filename; + } + + /** + * Logs the mesage to a file. + * @param level The log level + * @param msg The mesage to log + */ + protected synchronized void log(short level, String msg) { + if(level > logLevel) return; + + BufferedWriter out = null; + try { + out = new BufferedWriter(new FileWriter(this.filename, true)); + out.write(formatMessage(level, msg) + "\n"); + + } catch(Exception e) { + /** If we are unable to write our log message, go ahead and + * fall back to the default (stdout) logger */ + Logger.init(logLevel, new Logger()); + Logger.logByLevel(ERROR, "Unable to write to log file " + this.filename); + Logger.logByLevel(level, msg); + } + + try { + out.close(); + } catch(Exception e) {} + } +} diff --git a/src/java/org/opensrf/util/Logger.java b/src/java/org/opensrf/util/Logger.java new file mode 100644 index 0000000..cc201fd --- /dev/null +++ b/src/java/org/opensrf/util/Logger.java @@ -0,0 +1,130 @@ +package org.opensrf.util; +import java.text.SimpleDateFormat; +import java.text.FieldPosition; +import java.util.Date; + +/** + * Basic OpenSRF logging API. This default implementation + * logs to stderr. + */ +public class Logger { + + /** Log levels */ + public static final short ERROR = 1; + public static final short WARN = 2; + public static final short INFO = 3; + public static final short DEBUG = 4; + + /** The global log instance */ + private static Logger instance; + /** The global log level */ + protected static short logLevel; + + public Logger() {} + + /** Sets the global Logger instance + * @param level The global log level. + * @param l The Logger instance to use + */ + public static void init(short level, Logger l) { + instance = l; + logLevel = level; + } + + /** + * @return The global Logger instance + */ + public static Logger instance() { + return instance; + } + + /** + * Logs an error message + * @param msg The message to log + */ + public static void error(String msg) { + instance.log(ERROR, msg); + } + + /** + * Logs an warning message + * @param msg The message to log + */ + public static void warn(String msg) { + instance.log(WARN, msg); + } + + /** + * Logs an info message + * @param msg The message to log + */ + public static void info(String msg) { + instance.log(INFO, msg); + } + + /** + * Logs an debug message + * @param msg The message to log + */ + public static void debug(String msg) { + instance.log(DEBUG, msg); + } + + /** + * Appends the text representation of the log level + * @param sb The stringbuffer to append to + * @param level The log level + */ + protected static void appendLevelString(StringBuffer sb, short level) { + switch(level) { + case DEBUG: + sb.append("DEBG"); break; + case INFO: + sb.append("INFO"); break; + case WARN: + sb.append("WARN"); break; + case ERROR: + sb.append("ERR "); break; + } + } + + /** + * Formats a message for logging. Appends the current date+time + * and the log level string. + * @param level The log level + * @param msg The message to log + */ + protected static String formatMessage(short level, String msg) { + + StringBuffer sb = new StringBuffer(); + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format( + new Date(), sb, new FieldPosition(0)); + + sb.append(" ["); + appendLevelString(sb, level); + sb.append("] "); + sb.append(msg); + return sb.toString(); + } + + /** + * Logs a message by passing the log level explicitly + * @param level The log level + * @param msg The message to log + */ + public static void logByLevel(short level, String msg) { + instance.log(level, msg); + } + + /** + * Performs the actual logging. Subclasses should override + * this method. + * @param level The log level + * @param msg The message to log + */ + protected synchronized void log(short level, String msg) { + if(level > logLevel) return; + System.err.println(formatMessage(level, msg)); + } +} + -- 2.11.0