KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > grlea > log > adapters > slf4j > Slf4jAdapterFactory


1 package org.grlea.log.adapters.slf4j;
2
3 // $Id: Slf4jAdapterFactory.java,v 1.2 2006/07/13 12:44:58 grlea Exp $
4
// Copyright (c) 2004-2006 Graham Lea. All rights reserved.
5

6 // Licensed under the Apache License, Version 2.0 (the "License");
7
// you may not use this file except in compliance with the License.
8
// You may obtain a copy of the License at
9
//
10
// http://www.apache.org/licenses/LICENSE-2.0
11
//
12
// Unless required by applicable law or agreed to in writing, software
13
// distributed under the License is distributed on an "AS IS" BASIS,
14
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
// See the License for the specific language governing permissions and
16
// limitations under the License.
17

18 import org.grlea.log.SimpleLogger;
19
20 import org.slf4j.Logger;
21 import org.slf4j.ILoggerFactory;
22
23 /**
24  * <p>
25  * An implementation of {@link ILoggerFactory} which always returns {@link Slf4jAdapter}
26  * instances.
27  * </p>
28  *
29  * @author Graham Lea
30  * @version $Revision: 1.2 $
31  */

32 public class
33 Slf4jAdapterFactory
34 implements ILoggerFactory
35 {
36    private static final String JavaDoc SUPRESS_WARNINGS_PROPERTY =
37       "org.grlea.log.adapters.slf4j.supressWarnings";
38
39    /**
40     * Creates a new <code>Slf4jAdapterFactory</code>.
41     */

42    public
43    Slf4jAdapterFactory()
44    {}
45
46    public final Logger
47    getLogger(String JavaDoc loggerName)
48    {
49       SimpleLogger newLogger;
50       try
51       {
52          Class JavaDoc loggingClass = Class.forName(loggerName);
53          newLogger = new SimpleLogger(loggingClass);
54       }
55       catch (Exception JavaDoc e)
56       {
57          boolean supressWarnings = false;
58          try
59          {
60             String JavaDoc supressWarningsString = System.getProperty(SUPRESS_WARNINGS_PROPERTY);
61             supressWarnings =
62                supressWarningsString != null && supressWarningsString.toLowerCase().equals("true");
63          }
64          catch (Exception JavaDoc e1)
65          {
66             System.err.println("WARNING: Simple Log (Slf4jAdapterFA): " +
67                                "Failed to read system property '" + SUPRESS_WARNINGS_PROPERTY + "'");
68          }
69
70          if (!supressWarnings)
71          {
72             System.err.println("WARNING: Simple Log (Slf4jAdapterFA): " +
73                                "Failed to find class for logger loggerName '" + loggerName + "'. " +
74                                "Using class '" + Logger.class.getName() + "' and instanceId '" +
75                                loggerName +"'.");
76          }
77
78          newLogger = new SimpleLogger(Logger.class, loggerName);
79       }
80
81       return new Slf4jAdapter(newLogger, loggerName);
82    }
83 }
Popular Tags