Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

Configurator.d

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003         @file Configurator.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.Configurator;
00040 
00041 private import  mango.log.Logger,
00042                 mango.log.Layout,
00043                 mango.log.DateLayout,
00044                 mango.log.ConsoleAppender;
00045 
00046 /*******************************************************************************
00047 
00048         A utility class for initializing the basic behaviour of the
00049         default logging hierarchy.
00050 
00051 *******************************************************************************/
00052 
00053 public class BasicConfigurator
00054 {
00055         /***********************************************************************
00056 
00057                 Create a default StdioAppender with a SimpleTimerLayout.
00058 
00059         ***********************************************************************/
00060 
00061         static protected Logger defaultAppender ()
00062         {
00063                 // get the hierarchy root
00064                 Logger root = Logger.getRootLogger();
00065 
00066                 // setup a default appender
00067                 root.addAppender (new ConsoleAppender (new SimpleTimerLayout));
00068 
00069                 return root;
00070         }
00071 
00072         /***********************************************************************
00073 
00074                 Add a default StdioAppender, with a SimpleTimerLayout, to 
00075                 the root node, and set the default activity level to be
00076                 everything enabled.
00077                 
00078         ***********************************************************************/
00079 
00080         static void configure ()
00081         {
00082                 // enable all messages for all loggers
00083                 defaultAppender().setLevel (Logger.Level.Trace);
00084         }
00085 }
00086 
00087 /*******************************************************************************
00088 
00089         A utility class for initializing the basic behaviour of the
00090         default logging hierarchy.
00091 
00092         PropertyConfigurator parses a much simplified version of the 
00093         property file. Mango.log only supports the settings of Logger 
00094         levels at this time; setup of Appenders and Layouts are currently 
00095         done "in the code", though this should not be a major hardship. 
00096 
00097 *******************************************************************************/
00098 
00099 version (Mango)
00100 {
00101 public class PropertyConfigurator
00102 {
00103         private import mango.io.Properties;
00104 
00105         private static Logger.Level[char[]] map;
00106 
00107         /***********************************************************************
00108         
00109                 Populate a map of acceptable level names
00110 
00111         ***********************************************************************/
00112 
00113         static this()
00114         {
00115                 map["TRACE"]    = Logger.Level.Trace;
00116                 map["trace"]    = Logger.Level.Trace;
00117                 map["INFO"]     = Logger.Level.Info;
00118                 map["info"]     = Logger.Level.Info;
00119                 map["WARN"]     = Logger.Level.Warn;
00120                 map["warn"]     = Logger.Level.Warn;
00121                 map["ERROR"]    = Logger.Level.Error;
00122                 map["error"]    = Logger.Level.Error;
00123                 map["FATAL"]    = Logger.Level.Fatal;
00124                 map["fatal"]    = Logger.Level.Fatal;
00125                 map["NONE"]     = Logger.Level.None;
00126                 map["none"]     = Logger.Level.None;
00127         }
00128 
00129         /***********************************************************************
00130         
00131                 Add a default StdioAppender, with a SimpleTimerLayout, to 
00132                 the root node. The activity levels of all nodes are set
00133                 via a property file with name=value pairs specified that
00134                 follow this format:
00135 
00136                 name: the actual logger name, in dot notation format. The
00137                 name "root" is reserved to match the root logger node.
00138 
00139                 value: one of TRACE, INFO, WARN, ERROR, FATAL or NONE (or
00140                 the lowercase equivalents).
00141 
00142                 For example, the declaration
00143 
00144                 mango.unittest=INFO
00145 
00146                 sets the level of the logger called "mango.unittest".
00147 
00148         ***********************************************************************/
00149 
00150         static void configure (char[] filepath)
00151         {
00152                 void loader (char[] name, char[] value)
00153                 {
00154                         Logger l;
00155 
00156                         if (name == "root")
00157                             l = Logger.getRootLogger ();                            
00158                         else
00159                            l = Logger.getLogger (name);
00160 
00161                         if (l && value in map)
00162                             l.setLevel (map[value]);
00163                 }
00164 
00165                 // setup the basic stuff
00166                 BasicConfigurator.defaultAppender ();
00167 
00168                 // read and parse properties from file
00169                 Properties.load (filepath, &loader);
00170         }
00171 }
00172 }

Generated on Tue Jan 25 21:18:20 2005 for Mango by doxygen 1.3.6