KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > log > Log4JLogger


1 /*
2  * Copyright (C) MX4J.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.log;
10
11
12 import org.apache.log4j.Category;
13 import org.apache.log4j.Priority;
14
15 /**
16  * Log that redirects log calls to a Log4J Category. <p>
17  *
18  * @see org.apache.log4j.Category at <a HREF="http://jakarta.apache.org/log4j">Apache's Log4J</a>
19  * @author <a HREF="mailto:biorn_steedom@users.sourceforge.net">Simone Bordet</a>
20  * @version $Revision: 1.4 $
21  */

22 public class Log4JLogger extends Logger
23 {
24     private Category m_category;
25
26     /**
27      * This constructor is called to create instances of this Logger, following the prototype pattern.
28      */

29     public Log4JLogger() {}
30
31     protected void setCategory(String JavaDoc category)
32     {
33         super.setCategory(category);
34         m_category = Category.getInstance(getCategory());
35     }
36
37     protected void log(int priority, Object JavaDoc message, Throwable JavaDoc t)
38     {
39         // Convert MX4J priority to log4j priority
40
Priority p = convertPriority(priority);
41         m_category.log(p, message, t);
42     }
43
44     /**
45      * Converts MX4J priority to Log4J priority
46      */

47     protected Priority convertPriority(int mx4jPriority)
48     {
49         Priority log4jPriority = Priority.DEBUG;
50         switch (mx4jPriority)
51         {
52             case Logger.FATAL: log4jPriority = Priority.FATAL; break;
53             case Logger.ERROR: log4jPriority = Priority.ERROR; break;
54             case Logger.WARN: log4jPriority = Priority.WARN; break;
55             case Logger.INFO: log4jPriority = Priority.INFO; break;
56             case Logger.DEBUG: log4jPriority = Priority.DEBUG; break;
57             case Logger.TRACE: log4jPriority = Priority.DEBUG; break;
58             default: log4jPriority = Priority.INFO; break;
59         }
60         return log4jPriority;
61     }
62 }
63
Popular Tags