-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
46 lines (41 loc) · 1.34 KB
/
index.js
File metadata and controls
46 lines (41 loc) · 1.34 KB
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
require('dotenv').config({
path: '.env',
// overwrite: true,
});
const { createLogger, transports, format } = require('winston');
const Synology = require('./src');
const { printf, timestamp, prettyPrint } = format;
const { NODE_ENV } = process.env;
const myFormat = printf(({ level, message, _timestamp }) => {
const time = new Date(_timestamp).toLocaleString();
return `[${time}] ${level}: ${message}`;
});
const logger = createLogger({
transports: [new transports.Console()],
format: format.combine(
timestamp(),
prettyPrint(),
myFormat,
),
});
const wrapper = original => (...args) => {
const argAry = args;
for (let index = 0; index < args.length; index += 1) {
if (args[index] instanceof Error) {
argAry[index] = args[index].stack;
}
if (typeof args[index] === 'object') {
argAry[index] = JSON.stringify(args[index]);
}
}
original(argAry.join(' '));
};
logger.error = wrapper(logger.error);
logger.warn = wrapper(logger.warn);
logger.info = wrapper(logger.info);
logger.verbose = wrapper(logger.verbose);
logger.debug = wrapper(logger.debug);
logger.silly = wrapper(logger.silly);
function noop() {}
global.logger = (NODE_ENV === 'dev' || NODE_ENV === 'test') ? logger : { info: noop, warn: noop, error: noop };
module.exports = Synology;