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.text.convert.Formatter;
19 import ocean.meta.types.Qualifiers;
20 import ocean.util.log.Appender;
21 import ocean.util.log.Event;
22 
23 version (unittest)
24 {
25     import ocean.core.Test;
26     import ocean.util.log.ILogger;
27 }
28 
29 /*******************************************************************************
30 
31    A simple layout, prefixing each message with the log level and
32    the name of the logger.
33 
34    Example:
35    ------
36    import ocean.util.log.layout.LayoutSimple;
37    import ocean.util.log.Logger;
38    import ocean.util.log.AppendConsole;
39 
40 
41    Log.root.clear;
42    Log.root.add(new AppendConsole(new LayoutSimple));
43 
44    auto logger = Log.lookup("Example");
45 
46    logger.trace("Trace example");
47    logger.error("Error example");
48    logger.fatal("Fatal example");
49    -----
50 
51    Produced output:
52    -----
53    Trace [Example] - Trace example
54    Error [Example] - Error example
55    Fatal [Example] - Fatal example
56    ----
57 
58 *******************************************************************************/
59 
60 public class LayoutSimple : Appender.Layout
61 {
62     /***************************************************************************
63 
64         Subclasses should implement this method to perform the formatting
65         of the actual message content.
66 
67     ***************************************************************************/
68 
69     public override void format (LogEvent event, scope FormatterSink dg)
70     {
71         sformat(dg, "{} [{}] - {}", event.levelName, event.name, event);
72     }
73 }
74 
75 unittest
76 {
77     mstring result = new mstring(2048);
78     result.length = 0;
79     assumeSafeAppend(result);
80 
81     scope dg = (cstring v) { result ~= v; };
82     scope layout = new LayoutSimple();
83     LogEvent event = {
84         msg_: "Have you met Ted?",
85         name_: "Barney",
86         level_: ILogger.Level.Warn,
87     };
88 
89     testNoAlloc(layout.format(event, dg));
90     test!("==")(result, "Warn [Barney] - Have you met Ted?");
91 }