01: /*
02: This software is OSI Certified Open Source Software.
03: OSI Certified is a certification mark of the Open Source Initiative.
04:
05: The license (Mozilla version 1.0) can be read at the MMBase site.
06: See http://www.MMBase.org/license
07:
08: */
09:
10: package org.mmbase.util.logging;
11:
12: import java.util.*;
13: import org.apache.log4j.spi.LocationInfo;
14:
15: /**
16: * A very simple implementation of Logger. It ignores everything below
17: * warn (or what else if configured), and throws an exception for
18: * everything higher. In junit tests this generates test-case failures
19: * if a warn or error is issued (we don't want that in normal
20: * situations).
21: *
22: * Logging can be set up like this in the setup of your test:
23: <pre>
24: Logging.configure(System.getProperty("mmbase.config") + File.separator + "log" + File.separator + "log.xml");
25: </pre>
26: *
27: * @author Michiel Meeuwissen
28: * @since MMBase-1.7
29: * @version $Id: ExceptionImpl.java,v 1.8 2007/03/02 21:05:15 nklasens Exp $
30: */
31:
32: public class ExceptionImpl extends AbstractSimpleImpl implements Logger {
33:
34: private String cat;
35: private static Map<String, Logger> instances = new HashMap<String, Logger>();
36: private static int exceptionLevel = Level.WARN_INT;
37: private static Level staticLevel = Level.WARN;
38:
39: private ExceptionImpl(String c) {
40: cat = c;
41: }
42:
43: public static ExceptionImpl getLoggerInstance(String name) {
44: if (instances.containsKey(name)) {
45: return (ExceptionImpl) instances.get(name);
46: } else {
47: ExceptionImpl i = new ExceptionImpl(name);
48: i.setLevel(staticLevel);
49: instances.put(name, i);
50: return i;
51: }
52: }
53:
54: /**
55: * The configure method of this Logger implemenation.
56: *
57: * @param c A string, which can contain the output (stdout or
58: * stderr) and the priority (e.g. 'info')
59: */
60: public static void configure(String c) {
61: if (c == null) {
62: return; // everything default
63: } else {
64: StringTokenizer t = new StringTokenizer(c, ",");
65: if (t.hasMoreTokens()) {
66: exceptionLevel = Level.toLevel(t.nextToken()).toInt();
67: }
68: if (t.hasMoreTokens()) {
69: Level l = Level.toLevel(t.nextToken());
70: staticLevel = l;
71: for (Logger log : instances.values()) {
72: log.setLevel(l);
73: }
74: }
75: }
76: }
77:
78: protected final void log(String s, Level l) {
79: if (l.toInt() >= level) {
80: Throwable t = new Throwable();
81: LocationInfo info = new LocationInfo(t,
82: AbstractSimpleImpl.class.getName());
83: System.out.println(info.getFileName() + ":"
84: + info.getMethodName() + "." + info.getLineNumber()
85: + ": " + s);
86: System.out.println(Logging.stackTrace(t));
87: }
88: if (l.toInt() >= exceptionLevel) {
89: throw new LoggingException(cat + ":" + s, l);
90: }
91: }
92:
93: }
|