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