KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > logging > ExceptionImpl


1 /*
2 This software is OSI Certified Open Source Software.
3 OSI Certified is a certification mark of the Open Source Initiative.
4
5 The license (Mozilla version 1.0) can be read at the MMBase site.
6 See http://www.MMBase.org/license
7
8 */

9
10 package org.mmbase.util.logging;
11 import java.util.*;
12 import org.apache.log4j.spi.LocationInfo;
13
14 /**
15  * A very simple implementation of Logger. It ignores everything below
16  * warn (or what else if configured), and throws an exception for
17  * everything higher. In junit tests this generates test-case failures
18  * if a warn or error is issued (we don't want that in normal
19  * situations).
20  *
21  * Logging can be set up like this in the setup of your test:
22    <pre>
23   Logging.configure(System.getProperty("mmbase.config") + File.separator + "log" + File.separator + "log.xml");
24    </pre>
25  *
26  * @author Michiel Meeuwissen
27  * @since MMBase-1.7
28  * @version $Id: ExceptionImpl.java,v 1.6 2005/10/02 16:42:15 michiel Exp $
29  */

30
31 public class ExceptionImpl extends AbstractSimpleImpl implements Logger {
32
33     private String JavaDoc cat;
34     private static Map instances = new HashMap();
35     protected static int exceptionLevel = Level.WARN_INT;
36     protected static Level staticLevel = Level.WARN;
37
38     private ExceptionImpl(String JavaDoc c) {
39         cat = c;
40     }
41
42     public static ExceptionImpl getLoggerInstance(String JavaDoc name) {
43         if (instances.containsKey(name)) {
44             return (ExceptionImpl) instances.get(name);
45         } else {
46             ExceptionImpl i = new ExceptionImpl(name);
47             i.setLevel(staticLevel);
48             instances.put(name, i);
49             return i;
50         }
51     }
52
53     /**
54      * The configure method of this Logger implemenation.
55      *
56      * @param c A string, which can contain the output (stdout or
57      * stderr) and the priority (e.g. 'info')
58      */

59     public static void configure(String JavaDoc c) {
60         if (c == null) {
61            return; // everything default
62
} else {
63             StringTokenizer t = new StringTokenizer(c, ",");
64             if (t.hasMoreTokens()) {
65                 exceptionLevel = Level.toLevel(t.nextToken()).toInt();
66             }
67             if (t.hasMoreTokens()) {
68                 Level l = Level.toLevel(t.nextToken());
69                 staticLevel = l;
70                 Iterator i = instances.values().iterator();
71                 while (i.hasNext()) {
72                     Logger log = (Logger) i.next();
73                     log.setLevel(l);
74                 }
75             }
76         }
77     }
78
79     protected final void log (String JavaDoc s, Level l) {
80         if (l.toInt() >= level) {
81             Throwable JavaDoc t = new Throwable JavaDoc();
82             LocationInfo info = new LocationInfo(t, AbstractSimpleImpl.class.getName());
83             System.out.println(info.getFileName() + ":" + info.getMethodName() + "." + info.getLineNumber() + ": " + s);
84             System.out.println(Logging.stackTrace(t));
85         }
86         if (l.toInt() >= exceptionLevel) {
87             throw new LoggingException(cat + ":" + s, l);
88         }
89     }
90
91
92 }
93
Popular Tags