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