forked from pllee/nm-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
useLogger.js
32 lines (25 loc) · 1.01 KB
/
useLogger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const fs = require('fs');
const yaml = require('js-yaml');
const { Logger, LEVELS } = require('./logger');
const rootLogger = new Logger({ root: 'driver' });
rootLogger.log({ message: 'starting the app' });
rootLogger.log('an error occured', LEVELS.ERROR);
// transport is an overridable method that outputs the log. By default it just logs to the console.
// In this instance it is being used to write to a file based on the current level being logged.
const fileLogger = new Logger({
transport(level, message) {
const fMessage = `${message} \n`;
fs.appendFile(`out/${level}.log`, fMessage, (err) => {
if (err) throw err;
});
},
});
fileLogger.log({ cwd: __dirname }, LEVELS.DEBUG);
// format is an overridable function that takes an object and returns a string that gets written.
// By default it jsonifies the passed in object. In this case it outputs yaml string.
const yamlLogger = new Logger({
format(logObj) {
return yaml.safeDump(logObj);
},
});
yamlLogger.log('This is some yaml');