-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
85 lines (73 loc) · 2.56 KB
/
index.js
File metadata and controls
85 lines (73 loc) · 2.56 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
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
77
78
79
80
81
82
83
84
85
var fclone = require('fclone');
var os = require('os');
var path = require('path');
var winston = require('winston');
var argv = require('yargs').argv;
require('winston-log-and-exit');
var isProd = process.env.NODE_ENV === 'prod';
var isECS = process.env.HEAP_ECS === 'true';
const isK8s = !!process.env.POD_NAMESPACE;
const isProdDeployment = isECS || isK8s;
/*
TODO: This should be configured by an external file.
It shouldn't require a code change to set logging prefs.
*/
winston.exitOnError = false;
// process.mainModule is undefined when node reads from STDIN.
var mainFilename = process.mainModule == null
? 'unknown'
: path.basename(process.mainModule.filename);
// :KLUDGE: Add special handling for our pm2 loading setup.
if (mainFilename === 'pm2_loader.js') {
try {
mainFilename = path.basename(argv.pm2path)
}
catch (e) {
// An error was encountered, likely because pm2path wasn't passed
// in. In this case we'll just use pm2_loader for the mainfilename.
};
}
var hostname = os.hostname();
var rewriters = [
function(label, msg, meta) {
meta = fclone(meta); // Remove circular references.
meta.mainFilename = mainFilename;
meta.hostname = hostname;
return meta;
}
]
if (isProdDeployment) {
rewriters.push(function(label,msg, meta) {
// First, check if the meta object is actually an error. In this case we want to preserve
// the error message (which would otherwise be clobbered by the log message). Winston populates
// the `name` field of the meta object with the class of the object if available, so just check
// for `name === 'Error'` here.
if (meta.name === 'Error') {
meta.error_message = meta.message
delete meta.message;
delete meta.name; // This field just looks confusing.
return meta;
}
// Otherwise, we expect the meta object to be tags for the log line. Apply some rudimentary
// sanitizing here to stringify unexpected nested objects. All of our tags should be either
// string or number values.
for (var key of Object.keys(meta)) {
var value = meta[key];
var isString = typeof value === 'string';
var isNumber = typeof value === 'number';
if (!isString && !isNumber) {
meta[key] = '' + JSON.stringify(value);
}
}
return meta;
})
}
var logger = new (winston.Logger)({rewriters});
logger.add(winston.transports.Console, {
json: isProdDeployment,
stringify: (obj) => JSON.stringify(obj),
colorize: !isProdDeployment,
timestamp: isProd,
level: process.env.LOG_LEVEL || 'info',
});
module.exports = logger;