1 /*******************************************************************************
2 
3     Copyright:
4         Copyright (c) 2004 Kris Bell.
5         Some parts copyright (c) 2009-2016 dunnhumby Germany GmbH.
6         All rights reserved.
7 
8     License:
9         Tango Dual License: 3-Clause BSD License / Academic Free License v3.0.
10         See LICENSE_TANGO.txt for details.
11 
12     Version: Initial release: May 2004
13 
14     Authors: Kris
15 
16 *******************************************************************************/
17 
18 module ocean.util.log.LayoutDate;
19 
20 import ocean.meta.types.Qualifiers;
21 import ocean.text.convert.Formatter;
22 import ocean.time.Clock;
23 import ocean.time.WallClock;
24 import ocean.util.log.Appender;
25 import ocean.util.log.Event;
26 
27 version (unittest)
28 {
29     import ocean.core.Test;
30     import ocean.util.log.ILogger;
31 }
32 
33 /*******************************************************************************
34 
35     A layout with ISO-8601 date information prefixed to each message
36 
37 *******************************************************************************/
38 
39 public class LayoutDate : Appender.Layout
40 {
41     private bool localTime;
42 
43     /***************************************************************************
44 
45         Ctor with indicator for local vs UTC time. Default is local time.
46 
47     ***************************************************************************/
48 
49     this (bool localTime = true)
50     {
51         this.localTime = localTime;
52     }
53 
54     /***************************************************************************
55 
56         Subclasses should implement this method to perform the formatting
57         of the actual message content.
58 
59     ***************************************************************************/
60 
61     public override void format (LogEvent event, scope FormatterSink dg)
62     {
63         // convert time to field values
64         const tm = event.time;
65         const dt = (localTime) ? WallClock.toDate(tm) : Clock.toDate(tm);
66 
67         // format date according to ISO-8601 (lightweight formatter)
68         sformat(dg, "{u4}-{u2}-{u2} {u2}:{u2}:{u2},{u2} {} [{}] - {}",
69             dt.date.year, dt.date.month, dt.date.day,
70             dt.time.hours, dt.time.minutes, dt.time.seconds, dt.time.millis,
71             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 LayoutDate(false);
83        LogEvent event = {
84            msg_: "Have you met Ted?",
85                name_: "Barney",
86                time_: Time.fromUnixTime(1525048962) + TimeSpan.fromMillis(420),
87                level_: ILogger.Level.Warn,
88                host_: null,
89        };
90 
91        testNoAlloc(layout.format(event, dg));
92        test!("==")(result, "2018-04-30 00:42:42,420 Warn [Barney] - Have you met Ted?");
93 }