1 /******************************************************************************* 2 3 Simple Layout to be used with the tango logger 4 5 Copyright: 6 Copyright (c) 2009-2016 dunnhumby Germany GmbH. 7 All rights reserved. 8 9 License: 10 Boost Software License Version 1.0. See LICENSE_BOOST.txt for details. 11 Alternatively, this file may be distributed under the terms of the Tango 12 3-Clause BSD License (see LICENSE_BSD.txt for details). 13 14 *******************************************************************************/ 15 16 module ocean.util.log.layout.LayoutSimple; 17 18 import ocean.transition; 19 import Integer = ocean.text.convert.Integer_tango; 20 import ocean.text.Util; 21 import ocean.time.Clock; 22 import ocean.time.WallClock; 23 import ocean.util.log.Appender; 24 import ocean.util.log.Event; 25 26 27 /******************************************************************************* 28 29 A simple layout, prefixing each message with the log level and 30 the name of the logger. 31 32 Example: 33 ------ 34 import ocean.util.log.layout.LayoutSimple; 35 import ocean.util.log.Logger; 36 import ocean.util.log.AppendConsole; 37 38 39 Log.root.clear; 40 Log.root.add(new AppendConsole(new LayoutSimple)); 41 42 auto logger = Log.lookup("Example"); 43 44 logger.trace("Trace example"); 45 logger.error("Error example"); 46 logger.fatal("Fatal example"); 47 ----- 48 49 Produced output: 50 ----- 51 Trace [Example] - Trace example 52 Error [Example] - Error example 53 Fatal [Example] - Fatal example 54 ---- 55 56 *******************************************************************************/ 57 58 public class LayoutSimple : Appender.Layout 59 { 60 /*************************************************************************** 61 62 Subclasses should implement this method to perform the formatting 63 of the actual message content. 64 65 ***************************************************************************/ 66 67 public override void format (LogEvent event, scope void delegate(cstring) dg) 68 { 69 auto level = event.levelName; 70 71 // format date according to ISO-8601 (lightweight formatter) 72 char[20] tmp = void; 73 char[256] tmp2 = void; 74 dg(layout(tmp2, "%0 [%1] - ", 75 level, 76 event.name 77 )); 78 dg(event.toString); 79 } 80 81 /********************************************************************** 82 83 Convert an integer to a zero prefixed text representation 84 85 **********************************************************************/ 86 87 private cstring convert (mstring tmp, long i) 88 { 89 return Integer.formatter(tmp, i, 'u', '?', 8); 90 } 91 }