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