1 module gfm.logger.log;
2 
3 import std.stream,
4        std..string;
5 
6 static if( __VERSION__ >= 2067 )
7     import std.experimental.logger;
8 else
9     import std.historical.logger;
10 
11 import colorize;
12 
13 /// Coloured console logger.
14 class ConsoleLogger : Logger
15 {
16     public
17     {
18         this()
19         {
20             super(LogLevel.info);
21         }
22 
23         override protected void writeLogMsg(ref LogEntry payload) @trusted
24         {
25             LogLevel logLevel;
26 
27             auto foregroundColor = fg.white;
28             switch(payload.logLevel)
29             {
30                 case LogLevel.info:
31                     foregroundColor = fg.light_white;
32                     break;
33 
34                 case LogLevel.warning:
35                     foregroundColor = fg.light_yellow;
36                     break;
37 
38                 case LogLevel.error:
39                 case LogLevel.critical:
40                 case LogLevel.fatal:
41                     foregroundColor = fg.light_red;
42                     break;
43 
44                 case LogLevel.trace:
45                 default:
46                     foregroundColor = fg.white;
47             }
48 
49             import colorize.cwrite;
50             cwritefln( color("%s: %s", foregroundColor), logLevelToString(payload.logLevel), payload.msg);
51         }
52     }
53 
54     private
55     {
56         static pure string logLevelToString(const LogLevel lv)
57         {
58             switch(lv)
59             {
60                 case LogLevel.trace:
61                     return "trace";
62                 case LogLevel.info:
63                     return "info";
64                 case LogLevel.warning:
65                     return "warning";
66                 case LogLevel.error:
67                     return "error";
68                 case LogLevel.critical:
69                     return "critical";
70                 case LogLevel.fatal:
71                     return "fatal";
72                 default:
73                     assert(false);
74             }
75         }
76     }
77 }