Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hariko-logger): logger make from lib #20
- Loading branch information
Showing
3 changed files
with
76 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import 'colors' | ||
import dateformat from 'dateformat' | ||
|
||
type LevelType = 'debug' | 'verbose' | 'info' | 'warn' | 'error' | ||
|
||
const LOG_LEVELS = { | ||
error: 5, | ||
warn: 4, | ||
info: 3, | ||
verbose: 2, | ||
debug: 1 | ||
} | ||
const LOG_COLORS = { | ||
error: 'red', | ||
warn: 'yellow', | ||
info: 'green', | ||
verbose: 'cyan', | ||
debug: 'magenta' | ||
} | ||
|
||
export class Logger { | ||
private level_name: LevelType | ||
private time: boolean | ||
|
||
set level(value: LevelType) { | ||
if (!LOG_LEVELS[value]) | ||
throw new Error("Unsupported log level '" + value + "'") | ||
this.level_name = value | ||
} | ||
|
||
get level() { | ||
return this.level_name | ||
} | ||
|
||
log(level: LevelType, ...args: any[]) { | ||
// logger.log(level, 'format text', arg1, arg2 ...); | ||
if (LOG_LEVELS[this.level] > LOG_LEVELS[level]) return | ||
console.log.apply(console, this.format(level, args)) | ||
} | ||
format(level: LevelType, args: any[]) { | ||
var format = args.shift() | ||
var tag = '[' + level.toUpperCase() + ']' | ||
var time = this.time ? '[' + dateformat(new Date(), 'HH:MM:ss:l') + ']' : '' | ||
args.unshift(time.grey + tag[LOG_COLORS[level]] + ' ' + format) | ||
return args | ||
} | ||
debug(...args: any[]) { | ||
this.log('debug', ...args) | ||
} | ||
verbose(...args: any[]) { | ||
this.log('verbose', ...args) | ||
} | ||
info(...args: any[]) { | ||
this.log('info', ...args) | ||
} | ||
warn(...args: any[]) { | ||
this.log('warn', ...args) | ||
} | ||
error(...args: any[]) { | ||
this.log('error', ...args) | ||
} | ||
} | ||
|
||
export const logger = new Logger() | ||
|
||
export default logger // migration |