-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlogger.js
52 lines (45 loc) · 1.46 KB
/
logger.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { format as _format, transports as _transports, createLogger } from 'winston';
import { dirname } from 'path';
require('dotenv').config();
function getLabel(moduleName) {
if (typeof (moduleName) === 'string') {
return moduleName;
}
const projectPath = dirname(require.main.filename);
let parts = moduleName.filename.replace(__dirname, 'Path');
parts = parts.replace(`${projectPath}/`, '');
return parts.replace('/', '.').replace('.js', '').replace('/index', '');
}
function Logger(CONFIG) {
/**
* Logger factory to create winston logger labelled with module.
* @param {Object} moduleName Module from where the logger is called
* @return {Logger} Logger
*/
return (moduleName) => {
const myFormat = _format.combine(
_format.splat(),
_format.timestamp(),
_format.label({ label: getLabel(moduleName) }),
_format.simple(),
_format.printf(info => `${info.timestamp} [${info.label}] ${info.level.toUpperCase()}: ${JSON.stringify(info.message, null, 4)}`),
);
const ConsoleLogger = new _transports.Console({
level: CONFIG.LOG_LEVEL || 'silly',
format: myFormat,
});
const FileLogger = new _transports.File({
level: CONFIG.LOG_LEVEL || 'silly',
format: myFormat,
filename: 'logfile.log',
});
return createLogger({
level: CONFIG.LOG_LEVEL || 'silly',
transports: [
ConsoleLogger,
FileLogger,
],
});
};
}
export default Logger;