-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logger): split logger implementation into separate files and cre…
…ate console transport
- Loading branch information
1 parent
fb4d314
commit 21a39ad
Showing
3 changed files
with
74 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const consoleTransport = (prefix) => (( | ||
timestamp, level, resourceType, resourceID, action, message, error, | ||
) => { | ||
const args = [timestamp.toISOString(), level, resourceType, resourceID, action, message]; | ||
|
||
if (error) { args.push(error); } | ||
if (prefix) { args.unshift(prefix); } | ||
|
||
console[level](...args); | ||
}); | ||
|
||
export default consoleTransport; |
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,55 @@ | ||
import {safeJsonStringify} from '../utils'; | ||
|
||
export const LEVELS = { | ||
error: 1, | ||
warn: 2, | ||
info: 3, | ||
debug: 4, | ||
}; | ||
|
||
export const createLogger = () => { | ||
const transports = []; | ||
let currentLevel = 'error'; | ||
|
||
const log = (level, ...rest) => { | ||
if (LEVELS[level] <= LEVELS[currentLevel]) { | ||
const timestamp = new Date(); | ||
|
||
for (const transport of transports) { | ||
transport(timestamp, level, ...rest); | ||
} | ||
} | ||
}; | ||
|
||
const logger = { | ||
addTransport: (transportFunction) => { transports.push(transportFunction); }, | ||
setLevel: (level) => { currentLevel = level; }, | ||
info: (...args) => log('info', ...args), | ||
warn: (...args) => log('warn', ...args), | ||
error: (...args) => log('error', ...args), | ||
debug: (...args) => log('debug', ...args), | ||
}; | ||
|
||
return logger; | ||
}; | ||
|
||
export const format = (timestamp, level, resourceType, resourceID, action, message, error) => { | ||
let msgString = message; | ||
|
||
if (Array.isArray(message)) { | ||
msgString = message.map((item) => ( | ||
(typeof item === 'string' && item) | ||
|| safeJsonStringify(item, (key, value) => ( | ||
(value instanceof MediaStream && `MediaStream([${value.getTracks().map((track) => track.kind)}])`) | ||
|| value | ||
), 2) | ||
)).join(' '); | ||
} | ||
|
||
return `${timestamp.toISOString()} ${level} ${resourceType} ${resourceID} ${action} ${msgString} ${error ? ` ${error.stack || error}` : ''}`; | ||
}; | ||
|
||
export default { | ||
createLogger, | ||
format, | ||
}; |