Browse Source
Improve log readability and allow control over what is loggedrenovate/lint-staged-8.x
Ben Woosley
7 years ago
committed by
GitHub
4 changed files with 125 additions and 21 deletions
@ -0,0 +1,78 @@ |
|||
import debugLogger from 'debug-logger' |
|||
|
|||
// Enable colours for object inspection.
|
|||
debugLogger.inspectOptions = { |
|||
colors: true |
|||
} |
|||
|
|||
// Enable all zap logs if DEBUG has not been explicitly set.
|
|||
if (!process.env.DEBUG) { |
|||
debugLogger.debug.enable('zap:*') |
|||
} |
|||
|
|||
// Method to configure a logger instance with a specific namespace suffix.
|
|||
const logConfig = name => ({ |
|||
levels: { |
|||
trace: { |
|||
prefix: '[TRC] ', |
|||
namespaceSuffix: `:${name}` |
|||
}, |
|||
debug: { |
|||
prefix: '[DBG] ', |
|||
namespaceSuffix: `:${name}` |
|||
}, |
|||
log: { |
|||
prefix: '[LOG] ', |
|||
namespaceSuffix: `:${name}` |
|||
}, |
|||
info: { |
|||
prefix: '[INF] ', |
|||
namespaceSuffix: `:${name}` |
|||
}, |
|||
warn: { |
|||
prefix: '[WRN] ', |
|||
namespaceSuffix: `:${name}` |
|||
}, |
|||
error: { |
|||
prefix: '[ERR] ', |
|||
namespaceSuffix: `:${name}` |
|||
}, |
|||
critical: { |
|||
color: debugLogger.colors.magenta, |
|||
prefix: '[CRT] ', |
|||
namespaceSuffix: `:${name}`, |
|||
level: 6, |
|||
fd: 2 |
|||
} |
|||
} |
|||
}) |
|||
|
|||
|
|||
// Create 2 logs for use in the app.
|
|||
export const mainLog = debugLogger.config(logConfig('main'))('zap') |
|||
export const lndLog = debugLogger.config(logConfig('lnd '))('zap') |
|||
|
|||
export const lndLogGetLevel = (msg) => { |
|||
// Define a mapping between log level prefixes and log level names.
|
|||
const levelMap = { |
|||
TRC: 'trace', |
|||
DBG: 'debug', |
|||
INF: 'info', |
|||
WRN: 'warn', |
|||
ERR: 'error', |
|||
CRT: 'critical' |
|||
} |
|||
|
|||
// We set the default level to trace.
|
|||
// The only log lines that don't include a level prefix are a part of trace entries
|
|||
let level = 'trace' |
|||
|
|||
// Parse the log line to determine its level.
|
|||
Object.entries(levelMap).forEach(([key, value]) => { |
|||
if (msg.includes(`[${key}]`)) { |
|||
level = value |
|||
} |
|||
}) |
|||
|
|||
return level |
|||
} |
Loading…
Reference in new issue