View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of Java-Common.
5    *
6    * Java-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * Java-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with Java-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.logging;
21  
22  import java.util.logging.Level;
23  import java.util.logging.LogManager;
24  import java.util.logging.Logger;
25  
26  
27  /**
28   * <code>DebugLogger</code> is wrapper around <code>Logger</code> class, which
29   * produces logger with preset arbitrary logging level and handler
30   * <code>ConsoleLogHandler</code>.
31   *
32   * @author <a href="mailto:igor.kriznar@cosylab.com">Igor Kriznar</a>
33   * @version $Id: DebugLogger.java,v 1.3 2008-04-22 12:26:29 jbobnar Exp $
34   *
35   * @since Jun 27, 2004.
36   */
37  public class DebugLogger extends Logger
38  {
39  	//private static boolean init = false;
40  
41  	/**
42  	 * Returns logger with specified name and initial logging level. If level
43  	 * is OFF , then instance of <code>NullLogger</code> is return, which
44  	 * statically sends all logs to dev/null.
45  	 *
46  	 * @param name a name for the logger
47  	 * @param level initial level of logger
48  	 *
49  	 * @return a logger, <code>NullLogger</code> if level is OFF
50  	 *
51  	 * @see Logger#getLogger(java.lang.String)
52  	 */
53  	public synchronized static Logger getLogger(String name, Level level)
54  	{
55  		// TODO workaround becouse maven junit tester fails to load handler
56  
57  		/*if (!init) {
58  		    // to make sure class loaded, because of strange problems with JUnit class loader
59  		    //ConsoleLogHandler clh = new ConsoleLogHandler();
60  		    //clh.close();
61  		    ConsoleLogHandler.initializeConsoleLogging();
62  		    init = true;
63  		}*/
64  		if (Level.OFF.equals(level)) {
65  			return NullLogger.getNullLogger();
66  		}
67  
68  		// TODO workaround becouse maven junit tester fails to load handler
69  		Logger l = LogManager.getLogManager().getLogger(name);
70  
71  		if (l == null) {
72  			l = new DebugLogger(name, null);
73  			LogManager.getLogManager().addLogger(l);
74  			l.setUseParentHandlers(false);
75  			l.setLevel(level);
76  			l.addHandler(new ConsoleLogHandler());
77  		}
78  
79  		return l;
80  	}
81  
82  	protected DebugLogger(String name, String resourceBundleName)
83  	{
84  		super(name, resourceBundleName);
85  	}
86  
87  	/**
88  	 * DOCUMENT ME!
89  	 *
90  	 * @param args DOCUMENT ME!
91  	 */
92  	public static void main(String[] args)
93  	{
94  		try {
95  			Logger l = getLogger("dummy", Level.ALL);
96  
97  			l.info("Test");
98  
99  			l.finest("Finest Test");
100 		} catch (Exception e) {
101 			e.printStackTrace();
102 		}
103 	}
104 }
105 
106 /* __oOo__ */