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;
17 public static final short INTERNAL = 5;
19 /** The global log instance */
20 private static Logger instance;
21 /** The global log level */
22 protected static short logLevel;
26 /** Sets the global Logger instance
27 * @param level The global log level.
28 * @param l The Logger instance to use
30 public static void init(short level, Logger l) {
36 * @return The global Logger instance
38 public static Logger instance() {
43 * Logs an error message
44 * @param msg The message to log
46 public static void error(String msg) {
47 instance.log(ERROR, msg);
51 * Logs an warning message
52 * @param msg The message to log
54 public static void warn(String msg) {
55 instance.log(WARN, msg);
59 * Logs an info message
60 * @param msg The message to log
62 public static void info(String msg) {
63 instance.log(INFO, msg);
67 * Logs an debug message
68 * @param msg The message to log
70 public static void debug(String msg) {
71 instance.log(DEBUG, msg);
75 * Logs an internal message
76 * @param msg The message to log
78 public static void internal(String msg) {
79 instance.log(INTERNAL, msg);
84 * Appends the text representation of the log level
85 * @param sb The stringbuffer to append to
86 * @param level The log level
88 protected static void appendLevelString(StringBuffer sb, short level) {
91 sb.append("DEBG"); break;
93 sb.append("INFO"); break;
95 sb.append("INT "); break;
97 sb.append("WARN"); break;
99 sb.append("ERR "); break;
104 * Formats a message for logging. Appends the current date+time
105 * and the log level string.
106 * @param level The log level
107 * @param msg The message to log
109 protected static String formatMessage(short level, String msg) {
111 StringBuffer sb = new StringBuffer();
112 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(
113 new Date(), sb, new FieldPosition(0));
116 appendLevelString(sb, level);
118 sb.append(Thread.currentThread().getId());
121 return sb.toString();
125 * Logs a message by passing the log level explicitly
126 * @param level The log level
127 * @param msg The message to log
129 public static void logByLevel(short level, String msg) {
130 instance.log(level, msg);
134 * Performs the actual logging. Subclasses should override
136 * @param level The log level
137 * @param msg The message to log
139 protected synchronized void log(short level, String msg) {
140 if(level > logLevel) return;
141 System.err.println(formatMessage(level, msg));