]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/util/Logger.java
cc201fd05bf45daf55ca7e7dc7ee070af1269472
[OpenSRF.git] / src / java / org / opensrf / util / Logger.java
1 package org.opensrf.util;
2 import java.text.SimpleDateFormat;
3 import java.text.FieldPosition;
4 import java.util.Date;
5
6 /**
7  * Basic OpenSRF logging API.  This default implementation
8  * logs to stderr.
9  */
10 public class Logger {
11
12     /** Log levels */
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
18     /** The global log instance */
19     private static Logger instance;
20     /** The global log level */
21     protected static short logLevel;
22
23     public Logger() {}
24
25     /** Sets the global Logger instance
26      * @param level The global log level.
27      * @param l The Logger instance to use
28      */
29     public static void init(short level, Logger l) {
30         instance = l;
31         logLevel = level;
32     }
33
34     /** 
35      * @return The global Logger instance
36      */
37     public static Logger instance() {
38         return instance;
39     }
40
41     /**
42      * Logs an error message
43      * @param msg The message to log
44      */
45     public static void error(String msg) {
46         instance.log(ERROR, msg);
47     }
48
49     /**
50      * Logs an warning message
51      * @param msg The message to log
52      */
53     public static void warn(String msg) {
54         instance.log(WARN, msg);
55     }
56
57     /**
58      * Logs an info message
59      * @param msg The message to log
60      */
61     public static void info(String msg) {
62         instance.log(INFO, msg);
63     }
64
65     /**
66      * Logs an debug message
67      * @param msg The message to log
68      */
69     public static void debug(String msg) {
70         instance.log(DEBUG, msg);
71     }
72
73     /** 
74      * Appends the text representation of the log level
75      * @param sb The stringbuffer to append to
76      * @param level The log level
77      */
78     protected static void appendLevelString(StringBuffer sb, short level) {
79         switch(level) {
80             case DEBUG:
81                 sb.append("DEBG"); break;
82             case INFO:
83                 sb.append("INFO"); break;
84             case WARN:
85                 sb.append("WARN"); break;
86             case ERROR:
87                 sb.append("ERR "); break;
88         }
89     }
90
91     /**
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
96      */
97     protected static String formatMessage(short level, String msg) {
98
99         StringBuffer sb = new StringBuffer();
100         new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(
101             new Date(), sb, new FieldPosition(0));
102
103         sb.append(" [");
104         appendLevelString(sb, level);
105         sb.append("] ");
106         sb.append(msg);
107         return sb.toString();
108     }
109
110     /**
111      * Logs a message by passing the log level explicitly
112      * @param level The log level
113      * @param msg The message to log
114      */
115     public static void logByLevel(short level, String msg) {
116         instance.log(level, msg);
117     }
118
119     /**
120      * Performs the actual logging.  Subclasses should override 
121      * this method.
122      * @param level The log level
123      * @param msg The message to log
124      */
125     protected synchronized void log(short level, String msg) {
126         if(level > logLevel) return;
127         System.err.println(formatMessage(level, msg));
128     }
129 }
130