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