KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > jmx > MX4JLoggerMonolog


1 /**
2  * Copyright (C) 2001-2005 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.jmx;
19
20 import org.objectweb.util.monolog.Monolog;
21 import org.objectweb.util.monolog.api.BasicLevel;
22 import org.objectweb.util.monolog.api.Logger;
23
24 /**
25  * This class is a wrapper permiting to use Monolog as logging system, in MX4J.
26  *
27  * @author S.Chassande-Barrioz
28  */

29 public class MX4JLoggerMonolog extends mx4j.log.Logger {
30     
31     /**
32      * The inner monolog Logger.
33      */

34     Logger logger;
35     
36     /**
37      * Builds a MX4JLoggerMonolog instance. MX4J requires an empty constructor.
38      */

39     public MX4JLoggerMonolog() {
40     }
41     
42     /**
43      * Builds a MX4JLoggerMonolog with a particular inner Logger.
44      * @param lo the inner logger
45      */

46     public MX4JLoggerMonolog(Logger lo) {
47         this.logger = lo;
48     }
49     
50     /**
51      * Assignes the name/topic of the logger. The inner logger is allocated at
52      * this time.
53      */

54     protected void setCategory(String JavaDoc name) {
55         super.setCategory(name);
56         logger = Monolog.monologFactory.getLogger(name);
57     }
58     
59     /**
60      * Assignes the priority/level on the logger.
61      */

62     public void setPriority(int p) {
63         super.setPriority(p);
64         logger.setIntLevel(priority2Level(p));
65     }
66     
67     /**
68      * Converts a priority (MX4J) to a level (Monolog)
69      */

70     private final static int priority2Level(int p) {
71         switch (p) {
72         case mx4j.log.Logger.FATAL:
73             return BasicLevel.FATAL;
74         case mx4j.log.Logger.ERROR:
75             return BasicLevel.ERROR;
76         case mx4j.log.Logger.WARN:
77             return BasicLevel.WARN;
78         case mx4j.log.Logger.INFO:
79             return BasicLevel.INFO;
80         case mx4j.log.Logger.DEBUG:
81         default:
82             return BasicLevel.DEBUG;
83         }
84     }
85     
86     /**
87      * Log the MX4J event into the inner monolog logger.
88      */

89     protected void log(int p, Object JavaDoc m, Throwable JavaDoc t) {
90         if (t == null) {
91             logger.log(priority2Level(p), m);
92         } else {
93             logger.log(priority2Level(p), m, t);
94         }
95     }
96 }
97
Popular Tags