View Javadoc

1   /*
2    * Copyright (c) 2004-2005 SLF4J.ORG
3    * Copyright (c) 2004-2005 QOS.ch
4    *
5    * All rights reserved.
6    *
7    * Permission is hereby granted, free of charge, to any person obtaining
8    * a copy of this software and associated documentation files (the
9    * "Software"), to  deal in  the Software without  restriction, including
10   * without limitation  the rights to  use, copy, modify,  merge, publish,
11   * distribute, and/or sell copies of  the Software, and to permit persons
12   * to whom  the Software is furnished  to do so, provided  that the above
13   * copyright notice(s) and this permission notice appear in all copies of
14   * the  Software and  that both  the above  copyright notice(s)  and this
15   * permission notice appear in supporting documentation.
16   *
17   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
18   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
19   * MERCHANTABILITY, FITNESS FOR  A PARTICULAR PURPOSE AND NONINFRINGEMENT
20   * OF  THIRD PARTY  RIGHTS. IN  NO EVENT  SHALL THE  COPYRIGHT  HOLDER OR
21   * HOLDERS  INCLUDED IN  THIS  NOTICE BE  LIABLE  FOR ANY  CLAIM, OR  ANY
22   * SPECIAL INDIRECT  OR CONSEQUENTIAL DAMAGES, OR  ANY DAMAGES WHATSOEVER
23   * RESULTING FROM LOSS  OF USE, DATA OR PROFITS, WHETHER  IN AN ACTION OF
24   * CONTRACT, NEGLIGENCE  OR OTHER TORTIOUS  ACTION, ARISING OUT OF  OR IN
25   * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26   *
27   * Except as  contained in  this notice, the  name of a  copyright holder
28   * shall not be used in advertising or otherwise to promote the sale, use
29   * or other dealings in this Software without prior written authorization
30   * of the copyright holder.
31   */
32  
33  
34  package org.slf4j.impl;
35  
36  import it.imolinfo.jbi4cics.jbi.Messages;
37  import java.util.MissingResourceException;
38  import org.apache.log4j.LogManager;
39  import org.slf4j.ILoggerFactory;
40  import it.imolinfo.jbi4cics.Logger;
41  
42  /**
43   * Log4jLoggerFactory is an implementation of
44   * <code>org.slf4j.ILoggerFactory</code> returning
45   * the appropriate named {@link Log4jLoggerAdapter} instance.
46   * <p>
47   *
48   * @author Ceki G&uuml;lc&uuml;
49   * @author <a href="mailto:acannone@imolinfo.it">Amedeo Cannone</a>
50   * @author <a href="mailto:mcimatti@imolinfo.it">Marco Cimatti</a>
51   */
52  final class Log4jLoggerFactory implements ILoggerFactory {
53  
54      /**
55       * Creates a new instance of this class.
56       */
57      Log4jLoggerFactory() {
58      }
59  
60      /**
61       * Return an appropriate <code>org.slf4j.Logger</code> instance as specified
62       * by the <code>name</code> parameter.
63       * <p>
64       * If <code>name</code> is equal to the string value <code>"NULL"</code>
65       * (case insensitive), then the root logger of the underlying logging system
66       * is returned.
67       * <p>
68       * Null-valued name arguments are considered invalid.
69       * <p>
70       * Certain extremely simple logging systems, e.g. NOP, may always return the
71       * same logger instance regardless of the requested name.
72       *
73       * @param   name  the name of the Logger to return.
74       * @return  the <code>org.slf4j.Logger</code> instance as specified by
75       *          <code>name</code>.
76       */
77      public Logger getLogger(final String name) {
78          org.apache.log4j.Logger log4jLogger;
79          Messages messages;
80  
81          if (name.equalsIgnoreCase(Logger.ROOT_LOGGER_NAME)) {
82              log4jLogger = LogManager.getRootLogger();
83          } else {
84              log4jLogger = LogManager.getLogger(name);
85          }
86  
87          // If name identifies a Class, there may be an associated resource
88          // bundle to apply I18N
89          try {
90              Class clazz = Class.forName(name);
91  
92              messages = Messages.getMessages(clazz);
93          } catch (ClassNotFoundException e) {
94              messages = null;
95          } catch (MissingResourceException e) {
96              messages = null;
97          }
98  
99          return new Log4jLoggerAdapter(log4jLogger, messages);
100     }
101 }