From d895c60a51e7cdbf517fee226ffb0274904979f8 Mon Sep 17 00:00:00 2001 From: Dmitry Sharshakov Date: Mon, 11 Feb 2019 05:28:52 +0300 Subject: [PATCH] Add colors to log module (#175) --- log/handlers.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/log/handlers.ts b/log/handlers.ts index 9241bd77e4..5c65c4fd09 100644 --- a/log/handlers.ts +++ b/log/handlers.ts @@ -1,7 +1,8 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. import { open, File, Writer } from "deno"; -import { getLevelByName } from "./levels.ts"; +import { getLevelByName, LogLevel } from "./levels.ts"; import { LogRecord } from "./logger.ts"; +import { red, yellow, blue, bold } from "../colors/mod.ts"; const DEFAULT_FORMATTER = "{levelName} {msg}"; type FormatterFunction = (logRecord: LogRecord) => string; @@ -52,6 +53,29 @@ export class BaseHandler { } export class ConsoleHandler extends BaseHandler { + format(logRecord: LogRecord): string { + let msg = super.format(logRecord); + + switch (logRecord.level) { + case LogLevel.INFO: + msg = blue(msg); + break; + case LogLevel.WARNING: + msg = yellow(msg); + break; + case LogLevel.ERROR: + msg = red(msg); + break; + case LogLevel.CRITICAL: + msg = bold(red(msg)); + break; + default: + break; + } + + return msg; + } + log(msg: string) { console.log(msg); }