1 // Copyright (c) 2000 Just Objects B.V. <just@justobjects.nl>
2 // Distributable under LGPL license. See terms of license at gnu.org.
3 
4 package nl.justobjects.pushlet.util;
5 
6 import org.apache.log4j.Level;
7 import org.apache.log4j.LogManager;
8 import org.apache.log4j.Logger;
9 
10/**
11 * Logger to use Log4j for logging.
12 * <p/>
13 * Logs using Log4j.
14 * This class will require a log4j library in the classpath of the Pushlet.
15 *
16 * @author Uli Romahn
17 * @version $Id: Log4jLogger.java,v 1.1 2007/12/07 12:57:40 justb Exp $
18 */
19public class Log4jLogger implements PushletLogger {
20
21    /**
22     * Level intialized with default.
23     */
24    private Logger logger = LogManager.getLogger("pushlet");
25
26
27    /* (non-Javadoc)
28         * @see nl.justobjects.pushlet.util.PushletLogger#init()
29         */
30    public void init() {
31        setLevel(LOG_LEVEL_INFO);
32    }
33
34    /* (non-Javadoc)
35     * @see nl.justobjects.pushlet.util.PushletLogger#debug(java.lang.String)
36     */
37    public void debug(String aMessage) {
38        if (!logger.isDebugEnabled()) {
39            return;
40        }
41        logger.debug(aMessage);
42    }
43
44    /* (non-Javadoc)
45     * @see nl.justobjects.pushlet.util.PushletLogger#error(java.lang.String)
46     */
47    public void error(String aMessage) {
48        logger.error(aMessage);
49    }
50
51    /* (non-Javadoc)
52     * @see nl.justobjects.pushlet.util.PushletLogger#error(java.lang.String, java.lang.Throwable)
53     */
54    public void error(String aMessage, Throwable aThrowable) {
55        logger.error(aMessage, aThrowable);
56    }
57
58    /* (non-Javadoc)
59     * @see nl.justobjects.pushlet.util.PushletLogger#fatal(java.lang.String)
60     */
61    public void fatal(String aMessage) {
62        logger.fatal(aMessage);
63    }
64
65    /* (non-Javadoc)
66     * @see nl.justobjects.pushlet.util.PushletLogger#fatal(java.lang.String, java.lang.Throwable)
67     */
68    public void fatal(String aMessage, Throwable aThrowable) {
69        logger.fatal(aMessage, aThrowable);
70    }
71
72    /* (non-Javadoc)
73     * @see nl.justobjects.pushlet.util.PushletLogger#info(java.lang.String)
74     */
75    public void info(String aMessage) {
76        if (!logger.isInfoEnabled()) {
77            return;
78        }
79        logger.info(aMessage);
80    }
81
82    /* (non-Javadoc)
83     * @see nl.justobjects.pushlet.util.PushletLogger#trace(java.lang.String)
84     */
85    public void trace(String aMessage) {
86        logger.trace(aMessage);
87    }
88
89    /* (non-Javadoc)
90     * @see nl.justobjects.pushlet.util.PushletLogger#warn(java.lang.String)
91     */
92    public void warn(String aMessage) {
93        logger.warn(aMessage);
94    }
95
96    /* (non-Javadoc)
97     * @see nl.justobjects.pushlet.util.PushletLogger#warn(java.lang.String, java.lang.Throwable)
98     */
99    public void warn(String aMessage, Throwable aThrowable) {
00        logger.warn(aMessage, aThrowable);
01    }
02
03    /* (non-Javadoc)
04         * @see nl.justobjects.pushlet.util.PushletLogger#setLevel(int)
05         */
06    public void setLevel(int aLevel) {
07        if (aLevel < LOG_LEVEL_FATAL) {
08            logger.setLevel(Level.OFF);
09        } else {
10            switch (aLevel) {
11                case LOG_LEVEL_FATAL:
12                    logger.setLevel(Level.FATAL);
13                    break;
14                case LOG_LEVEL_ERROR:
15                    logger.setLevel(Level.ERROR);
16                    break;
17                case LOG_LEVEL_WARN:
18                    logger.setLevel(Level.WARN);
19                    break;
20                case LOG_LEVEL_INFO:
21                    logger.setLevel(Level.INFO);
22                    break;
23                case LOG_LEVEL_DEBUG:
24                    logger.setLevel(Level.DEBUG);
25                    break;
26                case LOG_LEVEL_TRACE:
27                    logger.setLevel(Level.TRACE);
28                    break;
29                default:
30                    logger.setLevel(Level.INFO);
31            }
32        }
33    }
34}
35