forked from pokusew/nfc-pcsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretty-logger.js
76 lines (55 loc) · 1.62 KB
/
pretty-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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"use strict";
// pretty-logger for debugging
// uses great winston logger library, visit https://github.com/winstonjs/winston
import util from 'util';
import chalk from 'chalk';
import winston from 'winston';
import { SPLAT } from 'triple-beam';
const colors = {
exception: 'red',
error: 'red',
warn: 'yellow',
info: 'green',
verbose: 'blue',
debug: 'blue',
silly: 'gray',
};
winston.addColors(colors);
// we could use instanceof but to avoid import we simply check obj structure
const isReader = obj => typeof obj === 'object' && obj.reader && obj.name;
const printf = winston.format.printf(({ timestamp, level, message, [SPLAT]: splat }) => {
let splatString = '';
let reader = '';
if (splat) {
let readerObj = splat.find(isReader);
if (readerObj) {
reader = chalk.cyan(readerObj.name) + ' ';
splat = splat.filter(obj => !isReader(obj));
}
if (splat.length > 1) {
splatString = ' ' + util.inspect(splat, { colors: true });
}
else if (splat.length > 0) {
splatString = ' ' + util.inspect(splat[0], { colors: true });
}
}
// see https://stackoverflow.com/questions/10729276/how-can-i-get-the-full-object-in-node-jss-console-log-rather-than-object
return `${timestamp ? timestamp + ' – ' : ''}${reader}${level}: ${message}${splatString}`;
});
const FORMAT = winston.format.combine(
winston.format.timestamp({
format: () => chalk.gray(new Date().toLocaleTimeString()),
}),
winston.format.colorize(),
printf,
);
const logger = winston.createLogger({
transports: [
new (winston.transports.Console)({
level: 'silly',
format: FORMAT,
}),
],
exitOnError: true,
});
export default logger;