mirror of
https://github.com/denoland/deno.git
synced 2024-12-28 10:09:20 -05:00
Add colors to log module (#175)
This commit is contained in:
parent
fda9c98d05
commit
d895c60a51
1 changed files with 25 additions and 1 deletions
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue