KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > log > Hierarchy


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8 package org.jivesoftware.util.log;
9
10 import org.jivesoftware.util.log.format.PatternFormatter;
11 import org.jivesoftware.util.log.output.io.StreamTarget;
12 import org.jivesoftware.util.log.util.DefaultErrorHandler;
13
14 /**
15  * This class encapsulates a basic independent log hierarchy.
16  * The hierarchy is essentially a safe wrapper around root logger.
17  *
18  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
19  */

20 public class Hierarchy {
21     ///Format of default formatter
22
private static final String JavaDoc FORMAT =
23             "%7.7{priority} %5.5{time} [%8.8{category}] (%{context}): %{message}\\n%{throwable}";
24
25     ///The instance of default hierarchy
26
private static final Hierarchy c_hierarchy = new Hierarchy();
27
28     ///Error Handler associated with hierarchy
29
private ErrorHandler m_errorHandler;
30
31     ///The root logger which contains all Loggers in this hierarchy
32
private Logger m_rootLogger;
33
34     /**
35      * Retrieve the default hierarchy.
36      * <p/>
37      * <p>In most cases the default LogHierarchy is the only
38      * one used in an application. However when security is
39      * a concern or multiple independent applications will
40      * be running in same JVM it is advantageous to create
41      * new Hierarchies rather than reuse default.</p>
42      *
43      * @return the default Hierarchy
44      */

45     public static Hierarchy getDefaultHierarchy() {
46         return c_hierarchy;
47     }
48
49     /**
50      * Create a hierarchy object.
51      * The default LogTarget writes to stdout.
52      */

53     public Hierarchy() {
54         m_errorHandler = new DefaultErrorHandler();
55         m_rootLogger = new Logger(new InnerErrorHandler(), "", null, null);
56
57         //Setup default output target to print to console
58
final PatternFormatter formatter = new PatternFormatter(FORMAT);
59         final StreamTarget target = new StreamTarget(System.out, formatter);
60
61         setDefaultLogTarget(target);
62     }
63
64     /**
65      * Set the default log target for hierarchy.
66      * This is the target inherited by loggers if no other target is specified.
67      *
68      * @param target the default target
69      */

70     public void setDefaultLogTarget(final LogTarget target) {
71         if (null == target) {
72             throw new IllegalArgumentException JavaDoc("Can not set DefaultLogTarget to null");
73         }
74
75         final LogTarget[] targets = new LogTarget[]{target};
76         getRootLogger().setLogTargets(targets);
77     }
78
79     /**
80      * Set the default log targets for this hierarchy.
81      * These are the targets inherited by loggers if no other targets are specified
82      *
83      * @param targets the default targets
84      */

85     public void setDefaultLogTargets(final LogTarget[] targets) {
86         if (null == targets || 0 == targets.length) {
87             throw new IllegalArgumentException JavaDoc("Can not set DefaultLogTargets to null");
88         }
89
90         for (int i = 0; i < targets.length; i++) {
91             if (null == targets[i]) {
92                 throw new IllegalArgumentException JavaDoc("Can not set DefaultLogTarget element to null");
93             }
94         }
95
96         getRootLogger().setLogTargets(targets);
97     }
98
99     /**
100      * Set the default priority for hierarchy.
101      * This is the priority inherited by loggers if no other priority is specified.
102      *
103      * @param priority the default priority
104      */

105     public void setDefaultPriority(final Priority priority) {
106         if (null == priority) {
107             throw new IllegalArgumentException JavaDoc("Can not set default Hierarchy Priority to null");
108         }
109
110         getRootLogger().setPriority(priority);
111     }
112
113     /**
114      * Set the ErrorHandler associated with hierarchy.
115      *
116      * @param errorHandler the ErrorHandler
117      */

118     public void setErrorHandler(final ErrorHandler errorHandler) {
119         if (null == errorHandler) {
120             throw new IllegalArgumentException JavaDoc("Can not set default Hierarchy ErrorHandler to null");
121         }
122
123         m_errorHandler = errorHandler;
124     }
125
126     /**
127      * Retrieve a logger for named category.
128      *
129      * @param category the context
130      * @return the Logger
131      */

132     public Logger getLoggerFor(final String JavaDoc category) {
133         return getRootLogger().getChildLogger(category);
134     }
135
136 // /**
137
// * Logs an error message to error handler.
138
// * Default Error Handler is stderr.
139
// *
140
// * @param message a message to log
141
// * @param throwable a Throwable to log
142
// * @deprecated Logging components should use ErrorHandler rather than Hierarchy.log()
143
// */
144
// public void log(final String message, final Throwable throwable) {
145
// m_errorHandler.error(message, throwable, null);
146
// }
147
//
148
// /**
149
// * Logs an error message to error handler.
150
// * Default Error Handler is stderr.
151
// *
152
// * @param message a message to log
153
// * @deprecated Logging components should use ErrorHandler rather than Hierarchy.log()
154
// */
155
// public void log(final String message) {
156
// log(message, null);
157
// }
158

159     private class InnerErrorHandler
160             implements ErrorHandler {
161         /**
162          * Log an unrecoverable error.
163          *
164          * @param message the error message
165          * @param throwable the exception associated with error (may be null)
166          * @param event the LogEvent that caused error, if any (may be null)
167          */

168         public void error(final String JavaDoc message,
169                           final Throwable JavaDoc throwable,
170                           final LogEvent event) {
171             m_errorHandler.error(message, throwable, event);
172         }
173     }
174
175     /**
176      * Utility method to retrieve logger for hierarchy.
177      * This method is intended for use by sub-classes
178      * which can take responsibility for manipulating
179      * Logger directly.
180      *
181      * @return the Logger
182      */

183     protected final Logger getRootLogger() {
184         return m_rootLogger;
185     }
186 }
187
Popular Tags