-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (53 loc) · 2.61 KB
/
index.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
/* global process */
const util = require("util");
const { createLogger, format, transports } = require("winston");
const { combine, timestamp } = format;
const log = createLogger({
level: "info",
format: combine(timestamp(), format.json()),
transports: [new transports.Console({ json: true })]
});
const isError = e => e instanceof Error || (e && e.stack && e.message && true) || false;
const isObject = obj => typeof obj === "object" && !Array.isArray(obj) && obj !== null;
const stringify = data => (isError(data) ? util.inspect(data) : typeof data === "string" ? data : JSON.stringify(data));
const sanatize = data => data.replace(/\\n|\n|\\t|\t|\\r|\r/g, " ");
const dataStringify = data => sanatize(stringify(data));
const addMessage = (arg, { messages = [] } = {}) => (arg || arg === 0 ? messages.concat(dataStringify(arg)) : messages);
const addServiceContext = (env, obj) => {
if (!env.APP_NAME && !env.NEW_RELIC_APP_NAME) {
return obj;
}
return { ...obj, serviceContext: { service: env.APP_NAME || env.NEW_RELIC_APP_NAME, version: env.APP_VERSION || "1.0.0" } };
};
const addArg = (data, res = { context: {}, messages: [] }) => (isError(data) && withError(data, res)) || (isObject(data) && withObject(data, res)) || withMessage(data, res);
const withError = (err, res = { context: {}, messages: [] }) => {
if (err) {
const { statusCode: status = 500, stack, message } = err;
let messages = res.messages || [];
if (message) {
messages = messages.concat(`Error: ${message}`);
}
return { ...res, context: Object.assign({}, res.context, status && { status }, stack && { stack }), messages };
}
return res;
};
const withObject = (obj, res = { context: {}, messages: [] }) => (obj ? { ...res, context: Object.assign({}, res.context, obj), messages: res.messages || [] } : res);
const withMessage = (obj, res = { context: {}, messages: [] }) => Object.assign({}, res, { messages: addMessage(obj, res) });
const parseArgs = args => {
const obj = args.reduce((res, data) => addArg(data, res), { context: {}, messages: [] });
return { ...obj, messages: obj.messages.join(",") };
};
const logByLevel = level => (...args) => {
const { messages, context } = parseArgs(args);
const { env } = process;
return log[level](messages, addServiceContext(env, { ...context, severity: level.toUpperCase(), textPayload: messages }));
};
const logger = Object.keys(log.levels).reduce((acc, val) => {
acc[val] = logByLevel(val);
return acc;
}, {});
logger.setLevel = level => {
log.transports = log.transports.map(transport => ({ ...transport, level }));
};
module.exports = logger;
module.exports.default = logger;