-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace logger. TODO: slack/line integration
- Loading branch information
1 parent
311aaa2
commit 5fe4ce4
Showing
7 changed files
with
188 additions
and
22 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,8 +1,22 @@ | ||
import LoggerFactory, { Logger } from './LoggerFactory'; | ||
import * as _ from 'lodash'; | ||
import * as pino from 'pino'; | ||
import * as util from 'util'; | ||
|
||
const factory = new LoggerFactory(); | ||
const logger = pino({ level: 'debug' }); | ||
const cache = new Map(); | ||
|
||
export function getLogger(name: string): Logger { | ||
return factory.create(_.trimEnd(name, 'Impl')); | ||
export function getLogger(name: string) { | ||
const label = _.trimEnd(name, 'Impl'); | ||
if (cache.has(label)) { | ||
return cache.get(label); | ||
} | ||
const childLogger = logger.child({ label }); | ||
const normalized = { | ||
debug: (s: string, ...args) => childLogger.debug(util.format(s, ...args)), | ||
info: (s: string, ...args) => childLogger.info(util.format(s, ...args)), | ||
warn: (s: string, ...args) => childLogger.warn(util.format(s, ...args)), | ||
error: (s: string, ...args) => childLogger.error(util.format(s, ...args)) | ||
}; | ||
cache.set(label, normalized); | ||
return normalized; | ||
} |
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,63 @@ | ||
import { EOL } from 'os'; | ||
import * as split from 'split2'; | ||
const Parse = require('fast-json-parse'); | ||
const chalk = require('chalk'); | ||
import { format as formatDate } from 'date-fns'; | ||
|
||
interface LogObject { | ||
level: number; | ||
msg: string; | ||
time: number; | ||
label: string; | ||
} | ||
|
||
const dateFormat = 'YYYY-MM-DD HH:mm:ss.SSS'; | ||
|
||
const levels = { | ||
default: 'USERLVL', | ||
60: 'FATAL', | ||
50: 'ERROR', | ||
40: 'WARN', | ||
30: 'INFO', | ||
20: 'DEBUG', | ||
10: 'TRACE' | ||
}; | ||
|
||
export default function pretty(opts: {colorize: boolean, withLabel: boolean, debug: boolean }) { | ||
const { colorize, withLabel, debug } = opts; | ||
const stream = split(mapLine); | ||
let ctx; | ||
let levelColors; | ||
const pipe = stream.pipe; | ||
stream.pipe = (dest: any) => { | ||
ctx = new chalk.constructor({ | ||
enabled: !!(chalk.supportsColor && colorize) | ||
}); | ||
levelColors = { | ||
default: ctx.white, | ||
60: ctx.bgRed, | ||
50: ctx.red, | ||
40: ctx.yellow, | ||
30: ctx.green, | ||
20: ctx.blue, | ||
10: ctx.grey | ||
}; | ||
return pipe.call(stream, dest); | ||
}; | ||
return stream; | ||
|
||
function mapLine(json: string): string { | ||
const parsed = new Parse(json); | ||
const logObj: LogObject = parsed.value; | ||
if (parsed.err) { | ||
return json + EOL; | ||
} | ||
if (!debug && logObj.level <= 20) { | ||
return ''; | ||
} | ||
const dateString = formatDate(new Date(logObj.time), dateFormat); | ||
const levelString = levelColors[logObj.level](levels[logObj.level]); | ||
const labelString = withLabel ? `[${logObj.label}] ` : ''; | ||
return `${dateString} ${levelString} ${labelString}${logObj.msg}${EOL}`; | ||
} | ||
} |
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,8 @@ | ||
import pretty from './pretty'; | ||
import * as fs from 'fs'; | ||
|
||
const infoFile = fs.createWriteStream('logs/info.log', { flags: 'a' }); | ||
const debugFile = fs.createWriteStream('logs/debug.log', { flags: 'a' }); | ||
process.stdin.pipe(pretty({ colorize: true, withLabel: false, debug: false })).pipe(process.stdout); | ||
process.stdin.pipe(pretty({ colorize: false, withLabel: true, debug: false })).pipe(infoFile); | ||
process.stdin.pipe(pretty({ colorize: false, withLabel: true, debug: true })).pipe(debugFile); |
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