001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Library License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 2000 by DigitalSesame. All rights reserved.
006: //
007: // $Id: LogWriterLog4JImpl.java,v 1.2 2002/05/17 08:55:47 per_nyfelt Exp $
008:
009: package org.ozoneDB.util;
010:
011: import java.io.OutputStream;
012: import java.io.PrintWriter;
013: import java.util.HashSet;
014: import java.util.Set;
015:
016: import org.apache.log4j.*;
017:
018: /**
019: * @author <a href="mailto:david@d11e.com">David Li</a>
020: * @author <a href="mailto:per.nyfelt@nordicwave.com">Per Nyfelt</a>
021: */
022: public class LogWriterLog4JImpl implements LogWriter {
023:
024: private static final String OZONE_BASE_PACKAGE = "org.ozoneDB";
025:
026: /* The ozone root category */
027: private Logger ozoneRoot;
028:
029: /**
030: * Current log level
031: */
032: //private int fLogLevel = ERROR | WARN | INFO;
033: private int currentLogLevel;
034:
035: /**
036: * A set of log target. This is to avoid the duplicated addition
037: * of log target
038: */
039: private Set logTargets = new HashSet();
040:
041: /**
042: * get Ozone pattern layout
043: */
044: private PatternLayout getLayout() {
045: return new PatternLayout("%-6p [%t] %c{1} - %m%n");
046: }
047:
048: public LogWriterLog4JImpl() {
049:
050: // the base logging category for all our classes
051: ozoneRoot = Logger.getLogger(OZONE_BASE_PACKAGE);
052:
053: // this is needed by the Server shutdownHook
054: createLogger(Logger.getLogger("java.lang"));
055: }
056:
057: /**
058: * This method allows to quickly find out if there is any log target that
059: * would receive entries of the specified level.
060: */
061: public boolean hasTarget(int level) {
062: return level <= currentLogLevel;
063: }
064:
065: private void addLogTarget(int level) {
066: currentLogLevel = level;
067: ozoneRoot.setLevel(OzoneDebugLevel.toLevel(level));
068: }
069:
070: public void addLogTarget(String name, OutputStream out, int level) {
071: if (logTargets.add(out)) {
072: Appender appender = new WriterAppender(getLayout(), out);
073: appender.setName(name);
074: ozoneRoot.addAppender(appender);
075: addLogTarget(level);
076: }
077: }
078:
079: public void addLogTarget(PrintWriter writer, int level, String name) {
080: if (logTargets.add(writer)) {
081: Appender appender = new WriterAppender(getLayout(), writer);
082: appender.setName(name);
083: ozoneRoot.addAppender(appender);
084: addLogTarget(level);
085: }
086: }
087:
088: private void createLogger(Category logger) {
089: Appender appender = new ConsoleAppender(getLayout());
090: appender.setName(logger.getName());
091: logger.addAppender(appender);
092:
093: logger.setPriority(OzoneDebugLevel.INFO);
094: }
095:
096: public void newEntry(Object sender, String msg, int level) {
097: Logger logger = Logger.getLogger(sender.getClass());
098: logger.log(OzoneDebugLevel.toLevel(level), msg);
099: }
100:
101: public void newEntry(Object sender, String msg, Throwable e,
102: int level) {
103: Logger logger = Logger.getLogger(sender.getClass());
104: logger.log(OzoneDebugLevel.toLevel(level), msg, e);
105: }
106:
107: public static void printLogInfo(Logger logger) {
108: if (logger == null ) {
109: System.out.println("logger is null");
110: return;
111: }
112: java.util.Enumeration enum = logger.getAllAppenders();
113: Appender appender;
114: System.out.println("\nLog info for " + logger.getName());
115: final String space = "\t\t\t";
116: while (enum.hasMoreElements()) {
117: appender = (Appender)enum.nextElement();
118: System.out.println(appender.getName() + space
119: + appender.getClass().getName());
120: }
121: }
122: }
|