1 package org.opensrf.util;
2 import java.text.SimpleDateFormat;
3 import java.text.FieldPosition;
7 * Basic OpenSRF logging API. This default implementation
13 public static final short ERROR = 1;
14 public static final short WARN = 2;
15 public static final short INFO = 3;
16 public static final short DEBUG = 4;
18 /** The global log instance */
19 private static Logger instance;
20 /** The global log level */
21 protected static short logLevel;
25 /** Sets the global Logger instance
26 * @param level The global log level.
27 * @param l The Logger instance to use
29 public static void init(short level, Logger l) {
35 * @return The global Logger instance
37 public static Logger instance() {
42 * Logs an error message
43 * @param msg The message to log
45 public static void error(String msg) {
46 instance.log(ERROR, msg);
50 * Logs an warning message
51 * @param msg The message to log
53 public static void warn(String msg) {
54 instance.log(WARN, msg);
58 * Logs an info message
59 * @param msg The message to log
61 public static void info(String msg) {
62 instance.log(INFO, msg);
66 * Logs an debug message
67 * @param msg The message to log
69 public static void debug(String msg) {
70 instance.log(DEBUG, msg);
74 * Appends the text representation of the log level
75 * @param sb The stringbuffer to append to
76 * @param level The log level
78 protected static void appendLevelString(StringBuffer sb, short level) {
81 sb.append("DEBG"); break;
83 sb.append("INFO"); break;
85 sb.append("WARN"); break;
87 sb.append("ERR "); break;
92 * Formats a message for logging. Appends the current date+time
93 * and the log level string.
94 * @param level The log level
95 * @param msg The message to log
97 protected static String formatMessage(short level, String msg) {
99 StringBuffer sb = new StringBuffer();
100 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(
101 new Date(), sb, new FieldPosition(0));
104 appendLevelString(sb, level);
107 return sb.toString();
111 * Logs a message by passing the log level explicitly
112 * @param level The log level
113 * @param msg The message to log
115 public static void logByLevel(short level, String msg) {
116 instance.log(level, msg);
120 * Performs the actual logging. Subclasses should override
122 * @param level The log level
123 * @param msg The message to log
125 protected synchronized void log(short level, String msg) {
126 if(level > logLevel) return;
127 System.err.println(formatMessage(level, msg));