00001 /******************************************************************************* 00002 00003 @file Logger.d 00004 00005 Copyright (c) 2004 Kris Bell 00006 00007 This software is provided 'as-is', without any express or implied 00008 warranty. In no event will the authors be held liable for damages 00009 of any kind arising from the use of this software. 00010 00011 Permission is hereby granted to anyone to use this software for any 00012 purpose, including commercial applications, and to alter it and/or 00013 redistribute it freely, subject to the following restrictions: 00014 00015 1. The origin of this software must not be misrepresented; you must 00016 not claim that you wrote the original software. If you use this 00017 software in a product, an acknowledgment within documentation of 00018 said product would be appreciated but is not required. 00019 00020 2. Altered source versions must be plainly marked as such, and must 00021 not be misrepresented as being the original software. 00022 00023 3. This notice may not be removed or altered from any distribution 00024 of the source. 00025 00026 4. Derivative works are permitted, but they must carry this notice 00027 in full and credit the original source. 00028 00029 00030 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00031 00032 00033 @version Initial version, May 2004 00034 @author Kris 00035 00036 00037 *******************************************************************************/ 00038 00039 module mango.log.Logger; 00040 00041 private import mango.log.Manager, 00042 mango.log.Appender; 00043 00044 public import mango.log.model.ILogger; 00045 00046 /******************************************************************************* 00047 00048 This is the primary API to the log package. Use the two static 00049 methods to access and/or create Logger instances, and the other 00050 methods to modify specific Logger attributes. 00051 00052 @code 00053 Logger myLogger = Logger.getLogger ("my.logger"); 00054 00055 myLogger.info ("an informational message"); 00056 myLogger.error ("an exception message: " ~ exception.toString); 00057 00058 etc ... 00059 @endcode 00060 00061 Messages passed to a Logger are assumed to be pre-formatted. You 00062 may find that the TextFormat class is handy for collating various 00063 components of the message. 00064 00065 @code 00066 TextFormat tf = new TextFormat (256); 00067 00068 myLogger.warn (tf.format("temperature is %d degrees!", 101)); 00069 @endcode 00070 00071 You may also need to use one of the two classes BasicConfigurator 00072 and PropertyConfigurator, along with the various Layout & Appender 00073 implementations to support your exact rendering needs. 00074 00075 Mango.log closely follows both the API and the behaviour as documented 00076 at the official Log4J site, where you'll find a good tutorial. Those 00077 pages are hosted over 00078 <A HREF="http://logging.apache.org/log4j/docs/documentation.html">here</A>. 00079 00080 *******************************************************************************/ 00081 00082 public class Logger : ILogger 00083 { 00084 static char[][] LevelNames = ["TRACE ", "INFO ", "WARN ", 00085 "ERROR ", "FATAL ", "NONE "]; 00086 00087 /*********************************************************************** 00088 00089 Return the root Logger instance. This is the ancestor of 00090 all loggers and, as such, can be used to manipulate the 00091 entire hierarchy. For instance, setting the root 'level' 00092 attribute will affect all other loggers in the tree. 00093 00094 ***********************************************************************/ 00095 00096 static Logger getRootLogger () 00097 { 00098 return Manager.getRootLogger (); 00099 } 00100 00101 /*********************************************************************** 00102 00103 Return an instance of the named logger. Names should be 00104 hierarchical in nature, using dot notation (with '.') to 00105 seperate each name section. For example, a typical name 00106 might be something like "mango.io.Buffer". 00107 00108 If the logger does not currently exist, it is created and 00109 inserted into the hierarchy. A parent will be attached to 00110 it, which will be either the root logger or the closest 00111 ancestor in terms of the hierarchical name space. 00112 00113 ***********************************************************************/ 00114 00115 static Logger getLogger (char[] name) 00116 { 00117 return Manager.getLogger (name); 00118 } 00119 00120 /*********************************************************************** 00121 00122 Add a trace messages. This is called 'debug' in Log4J but 00123 that is a reserved word in the D language. This needs some 00124 more thought. 00125 00126 ***********************************************************************/ 00127 00128 abstract void trace (char[] msg); 00129 00130 /*********************************************************************** 00131 00132 Add an info message 00133 00134 ***********************************************************************/ 00135 00136 abstract void info (char[] msg); 00137 00138 /*********************************************************************** 00139 00140 Add a warning message 00141 00142 ***********************************************************************/ 00143 00144 abstract void warn (char[] msg); 00145 00146 /*********************************************************************** 00147 00148 Add an error message 00149 00150 ***********************************************************************/ 00151 00152 abstract void error (char[] msg); 00153 00154 /*********************************************************************** 00155 00156 Add a fatal message 00157 00158 ***********************************************************************/ 00159 00160 abstract void fatal (char[] msg); 00161 00162 /*********************************************************************** 00163 00164 Return the name of this Logger 00165 00166 ***********************************************************************/ 00167 00168 abstract char[] getName (); 00169 00170 /*********************************************************************** 00171 00172 Return the current level assigned to this logger 00173 00174 ***********************************************************************/ 00175 00176 abstract Level getLevel (); 00177 00178 /*********************************************************************** 00179 00180 Set the activity level of this logger. Levels control how 00181 much information is emitted during runtime, and relate to 00182 each other as follows: 00183 00184 Trace < Info < Warn < Error < Fatal < None 00185 00186 That is, if the level is set to Error, only calls to the 00187 error() and fatal() methods will actually produce output: 00188 all others will be inhibited. 00189 00190 Note that Log4J is a hierarchical environment, and each 00191 logger defaults to inheriting a level from its parent. 00192 00193 00194 ***********************************************************************/ 00195 00196 abstract void setLevel (Level level); 00197 00198 /*********************************************************************** 00199 00200 same as setLevel (Level), but with additional control over 00201 whether the children are forced to accept the changed level 00202 or not. If 'force' is false, then children adopt the parent 00203 level only if they have their own level set to Level.None 00204 00205 ***********************************************************************/ 00206 00207 abstract void setLevel (Level level, bool force); 00208 00209 /*********************************************************************** 00210 00211 Is this logger enabled for the provided level? 00212 00213 ***********************************************************************/ 00214 00215 abstract bool isEnabled (Level level); 00216 00217 /*********************************************************************** 00218 00219 Return whether this logger uses additive appenders or not. 00220 See setAdditive(). 00221 00222 ***********************************************************************/ 00223 00224 abstract bool isAdditive (); 00225 00226 /*********************************************************************** 00227 00228 Specify whether or not this logger has additive behaviour. 00229 This is enabled by default, and causes a logger to invoke 00230 all appenders within its ancestry (until an ancestor is 00231 found with an additive attribute of false). 00232 00233 ***********************************************************************/ 00234 00235 abstract void setAdditive (bool enabled); 00236 00237 /*********************************************************************** 00238 00239 Remove all appenders from this logger. 00240 00241 ***********************************************************************/ 00242 00243 abstract void clearAppenders (); 00244 00245 /*********************************************************************** 00246 00247 Add an appender to this logger. You may add multiple 00248 appenders to appropriate loggers, and each of them 00249 will be invoked for that given logger, and for each 00250 of its child loggers (assuming isAdditive() is true 00251 for those children). Note that multiple instances 00252 of the same appender, regardless of where they may 00253 reside within the tree, are not invoked at runtime. 00254 That is, only one from a set of identical loggers 00255 will execute. 00256 00257 Use clearAttributes() to remove all from a given logger. 00258 00259 ***********************************************************************/ 00260 00261 abstract void addAppender (Appender appender); 00262 00263 /*********************************************************************** 00264 00265 Get number of milliseconds since this application started 00266 00267 ***********************************************************************/ 00268 00269 abstract ulong getUptime (); 00270 }