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