001: /*
002: * Copyright (C) The Apache Software Foundation. All rights reserved.
003: *
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008: package org.jivesoftware.util.log;
009:
010: import org.jivesoftware.util.log.format.PatternFormatter;
011: import org.jivesoftware.util.log.output.io.StreamTarget;
012: import org.jivesoftware.util.log.util.DefaultErrorHandler;
013:
014: /**
015: * This class encapsulates a basic independent log hierarchy.
016: * The hierarchy is essentially a safe wrapper around root logger.
017: *
018: * @author <a href="mailto:peter@apache.org">Peter Donald</a>
019: */
020: public class Hierarchy {
021: ///Format of default formatter
022: private static final String FORMAT = "%7.7{priority} %5.5{time} [%8.8{category}] (%{context}): %{message}\\n%{throwable}";
023:
024: ///The instance of default hierarchy
025: private static final Hierarchy c_hierarchy = new Hierarchy();
026:
027: ///Error Handler associated with hierarchy
028: private ErrorHandler m_errorHandler;
029:
030: ///The root logger which contains all Loggers in this hierarchy
031: private Logger m_rootLogger;
032:
033: /**
034: * Retrieve the default hierarchy.
035: * <p/>
036: * <p>In most cases the default LogHierarchy is the only
037: * one used in an application. However when security is
038: * a concern or multiple independent applications will
039: * be running in same JVM it is advantageous to create
040: * new Hierarchies rather than reuse default.</p>
041: *
042: * @return the default Hierarchy
043: */
044: public static Hierarchy getDefaultHierarchy() {
045: return c_hierarchy;
046: }
047:
048: /**
049: * Create a hierarchy object.
050: * The default LogTarget writes to stdout.
051: */
052: public Hierarchy() {
053: m_errorHandler = new DefaultErrorHandler();
054: m_rootLogger = new Logger(new InnerErrorHandler(), "", null,
055: null);
056:
057: //Setup default output target to print to console
058: final PatternFormatter formatter = new PatternFormatter(FORMAT);
059: final StreamTarget target = new StreamTarget(System.out,
060: formatter);
061:
062: setDefaultLogTarget(target);
063: }
064:
065: /**
066: * Set the default log target for hierarchy.
067: * This is the target inherited by loggers if no other target is specified.
068: *
069: * @param target the default target
070: */
071: public void setDefaultLogTarget(final LogTarget target) {
072: if (null == target) {
073: throw new IllegalArgumentException(
074: "Can not set DefaultLogTarget to null");
075: }
076:
077: final LogTarget[] targets = new LogTarget[] { target };
078: getRootLogger().setLogTargets(targets);
079: }
080:
081: /**
082: * Set the default log targets for this hierarchy.
083: * These are the targets inherited by loggers if no other targets are specified
084: *
085: * @param targets the default targets
086: */
087: public void setDefaultLogTargets(final LogTarget[] targets) {
088: if (null == targets || 0 == targets.length) {
089: throw new IllegalArgumentException(
090: "Can not set DefaultLogTargets to null");
091: }
092:
093: for (int i = 0; i < targets.length; i++) {
094: if (null == targets[i]) {
095: throw new IllegalArgumentException(
096: "Can not set DefaultLogTarget element to null");
097: }
098: }
099:
100: getRootLogger().setLogTargets(targets);
101: }
102:
103: /**
104: * Set the default priority for hierarchy.
105: * This is the priority inherited by loggers if no other priority is specified.
106: *
107: * @param priority the default priority
108: */
109: public void setDefaultPriority(final Priority priority) {
110: if (null == priority) {
111: throw new IllegalArgumentException(
112: "Can not set default Hierarchy Priority to null");
113: }
114:
115: getRootLogger().setPriority(priority);
116: }
117:
118: /**
119: * Set the ErrorHandler associated with hierarchy.
120: *
121: * @param errorHandler the ErrorHandler
122: */
123: public void setErrorHandler(final ErrorHandler errorHandler) {
124: if (null == errorHandler) {
125: throw new IllegalArgumentException(
126: "Can not set default Hierarchy ErrorHandler to null");
127: }
128:
129: m_errorHandler = errorHandler;
130: }
131:
132: /**
133: * Retrieve a logger for named category.
134: *
135: * @param category the context
136: * @return the Logger
137: */
138: public Logger getLoggerFor(final String category) {
139: return getRootLogger().getChildLogger(category);
140: }
141:
142: // /**
143: // * Logs an error message to error handler.
144: // * Default Error Handler is stderr.
145: // *
146: // * @param message a message to log
147: // * @param throwable a Throwable to log
148: // * @deprecated Logging components should use ErrorHandler rather than Hierarchy.log()
149: // */
150: // public void log(final String message, final Throwable throwable) {
151: // m_errorHandler.error(message, throwable, null);
152: // }
153: //
154: // /**
155: // * Logs an error message to error handler.
156: // * Default Error Handler is stderr.
157: // *
158: // * @param message a message to log
159: // * @deprecated Logging components should use ErrorHandler rather than Hierarchy.log()
160: // */
161: // public void log(final String message) {
162: // log(message, null);
163: // }
164:
165: private class InnerErrorHandler implements ErrorHandler {
166: /**
167: * Log an unrecoverable error.
168: *
169: * @param message the error message
170: * @param throwable the exception associated with error (may be null)
171: * @param event the LogEvent that caused error, if any (may be null)
172: */
173: public void error(final String message,
174: final Throwable throwable, final LogEvent event) {
175: m_errorHandler.error(message, throwable, event);
176: }
177: }
178:
179: /**
180: * Utility method to retrieve logger for hierarchy.
181: * This method is intended for use by sub-classes
182: * which can take responsibility for manipulating
183: * Logger directly.
184: *
185: * @return the Logger
186: */
187: protected final Logger getRootLogger() {
188: return m_rootLogger;
189: }
190: }
|