KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > util > logger > jdk14 > Jdk14Logger


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/jdk14/org/apache/slide/util/logger/jdk14/Jdk14Logger.java,v 1.2 2004/07/28 09:38:52 ib Exp $
3  * $Revision: 1.2 $
4  * $Date: 2004/07/28 09:38:52 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.util.logger.jdk14;
25
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29 /**
30  * Default logger implementation. Uses java.util.logging implementation provided by Java 1.4.
31  *
32  */

33 public class Jdk14Logger implements org.apache.slide.util.logger.Logger {
34
35     public final static int DEFAULT_LEVEL = INFO;
36
37     public Jdk14Logger() {
38         LoggerConfiguration.configure();
39     }
40
41     public void log(Object JavaDoc data, Throwable JavaDoc throwable, String JavaDoc channel, int slideLevel) {
42         Logger JavaDoc logger = Logger.getLogger(channel);
43         Level JavaDoc level = getLevelForSlideLevel(slideLevel);
44         logger.log(level, data.toString(), throwable);
45     }
46
47     public void log(Object JavaDoc data, String JavaDoc channel, int slideLevel) {
48         Logger JavaDoc logger = Logger.getLogger(channel);
49         Level JavaDoc level = getLevelForSlideLevel(slideLevel);
50         if (data instanceof Throwable JavaDoc) {
51             logger.log(level, data.toString(), (Throwable JavaDoc) data);
52         } else {
53             logger.log(level, data.toString());
54         }
55     }
56
57     public void log(Object JavaDoc data, int level) {
58         this.log(data, DEFAULT_CHANNEL, level);
59     }
60
61     public void log(Object JavaDoc data) {
62         this.log(data, DEFAULT_CHANNEL, DEFAULT_LEVEL);
63     }
64
65     public int getLoggerLevel(String JavaDoc channel) {
66         Logger JavaDoc logger = Logger.getLogger(channel);
67         Level JavaDoc level = logger.getLevel();
68         return getSlideLevelForLevel(level);
69     }
70
71     public int getLoggerLevel() {
72         return getLoggerLevel(DEFAULT_CHANNEL);
73     }
74
75     public void setLoggerLevel(int slideLevel) {
76         Logger.getLogger("").setLevel(getLevelForSlideLevel(slideLevel));
77         setLoggerLevel(DEFAULT_CHANNEL, slideLevel);
78     }
79
80     public void setLoggerLevel(String JavaDoc channel, int slideLevel) {
81         Logger JavaDoc logger = Logger.getLogger(channel);
82         Level JavaDoc level = getLevelForSlideLevel(slideLevel);
83         logger.setLevel(level);
84     }
85
86     public boolean isEnabled(String JavaDoc channel, int slideLevel) {
87         Logger JavaDoc logger = Logger.getLogger(channel);
88         Level JavaDoc level = getLevelForSlideLevel(slideLevel);
89         return logger.isLoggable(level);
90     }
91
92     public boolean isEnabled(int level) {
93         return isEnabled(DEFAULT_CHANNEL, level);
94     }
95
96     protected Level JavaDoc getLevelForSlideLevel(int slideLevel) {
97         Level JavaDoc level = Level.ALL;
98         switch (slideLevel) {
99             case EMERGENCY :
100                 level = Level.SEVERE;
101                 break;
102             case CRITICAL :
103                 level = Level.SEVERE;
104                 break;
105             case ERROR :
106                 level = Level.SEVERE;
107                 break;
108             case WARNING :
109                 level = Level.WARNING;
110                 break;
111             case INFO :
112                 level = Level.INFO;
113                 break;
114             case DEBUG :
115                 level = Level.FINE;
116                 break;
117         }
118         return level;
119     }
120
121     protected int getSlideLevelForLevel(Level JavaDoc level) {
122         if (level.equals(Level.ALL))
123             return DEBUG;
124         if (level.equals(Level.FINEST))
125             return DEBUG;
126         if (level.equals(Level.FINER))
127             return DEBUG;
128         if (level.equals(Level.FINE))
129             return DEBUG;
130         if (level.equals(Level.CONFIG))
131             return DEBUG;
132
133         if (level.equals(Level.INFO))
134             return INFO;
135
136         if (level.equals(Level.WARNING))
137             return WARNING;
138
139         if (level.equals(Level.SEVERE))
140             return ERROR;
141
142         if (level.equals(Level.OFF))
143             return -1;
144
145         return DEFAULT_LEVEL;
146     }
147
148 }
149
Popular Tags