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