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