KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > Log4jLoggerFactory


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.util;
23
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.avalon.framework.logger.Logger;
27 import org.apache.avalon.framework.logger.Log4JLogger;
28 import org.jacorb.config.LoggerFactory;
29 import org.jboss.logging.log4j.Log4jLoggerPlugin;
30
31 import java.util.HashMap JavaDoc;
32 import java.util.Map JavaDoc;
33
34 /**
35  * JacORB logger factory that creates named Avalon loggers with log4j
36  * as the underlying log mechanism.
37  *
38  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
39  * @version $Revision: 57960 $
40  */

41 public class Log4jLoggerFactory
42    implements LoggerFactory
43 {
44    /** logging back-end mechanism used by all Log4jLoggerFactory instances */
45    private final static String JavaDoc name = "log4j";
46    
47    /** cache of created loggers */
48    private final Map JavaDoc namedLoggers = new HashMap JavaDoc();
49
50    // Auxiliary static methods --------------------------------------
51

52    /**
53     * Gets a log4j logger by name.
54     *
55     * @param name the name of the logger
56     * @return an <code>org.apache.log4j.Logger</code> instance
57     */

58    private static org.apache.log4j.Logger getLog4jLogger(String JavaDoc name)
59    {
60       org.jboss.logging.Logger l = org.jboss.logging.Logger.getLogger(name);
61       org.jboss.logging.LoggerPlugin lp = l.getLoggerPlugin();
62       if (lp instanceof Log4jLoggerPlugin)
63          return ((Log4jLoggerPlugin)lp).getLogger();
64       else
65       {
66          return null;
67       }
68    }
69    
70    
71    // Implementation of org.apache.avalon.framework.configuration.Configuration
72

73    public void configure(Configuration configuration)
74       throws ConfigurationException
75    {
76    }
77    
78    // Implementation of org.jacorb.util.LoggerFactory ---------------
79

80    /**
81     * Gets the name of the logging back-end mechanism.
82     *
83     * @return the string <code>"log4j"</code>
84     */

85    public final String JavaDoc getLoggingBackendName()
86    {
87       return name;
88    }
89    
90    /**
91     * Gets an Avalon logger by name.
92     *
93     * @param name the name of the logger
94     * @return an <code>org.apache.avalon.framework.logger.Logger</code>
95     * instance
96     */

97    public Logger getNamedLogger(String JavaDoc name)
98    {
99       Object JavaDoc o = namedLoggers.get(name);
100       
101       if (o != null)
102          return (Logger)o;
103       
104       org.apache.log4j.Logger log4jLogger = getLog4jLogger(name);
105       Logger logger = new Log4JLogger(log4jLogger);
106       
107       namedLoggers.put(name, logger);
108       return logger;
109    }
110    
111    /**
112     * Gets an Avalon root logger by name.
113     *
114     * @param name the name of the logger
115     * @return an <code>org.apache.avalon.framework.logger.Logger</code>
116     * instance
117     */

118    public Logger getNamedRootLogger(String JavaDoc name)
119    {
120       return getNamedLogger(name);
121    }
122    
123    /**
124     * Creates a named Avalon logger with given <code>logFileName</code>
125     * and <code>maxLogSize</code> parameters. This is a dummy implementation
126     * that always return null.
127     *
128     * @param name the name of the logger
129     * @param logFileName the name of the file to log to
130     * @param maxLogSize maximum size of the log file.
131     *
132     * @return the new logger (null in this implementation).
133     */

134    public Logger getNamedLogger(String JavaDoc name,
135                                 String JavaDoc logFileName, long maxLogSize)
136       throws java.io.IOException JavaDoc
137    {
138       return null;
139    }
140    
141    /**
142     * set the file name and max file size for logging to a file
143     */

144    public void setDefaultLogFile(String JavaDoc fileName, long maxLogSize)
145       throws java.io.IOException JavaDoc
146    {
147       // not implemented
148
}
149
150 }
151
Popular Tags