-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLogger.js
74 lines (67 loc) · 1.55 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* eslint no-console: "off" */
'use strict';
/**
* A class for writing JSON-formated logs to STDOUT
*/
class Logger {
/**
* @param {Object} [options] - options object
* @param {string} [options.sender='cumulus-ecs-task'] - options.sender - an optional sender for
* the log messages
*/
constructor(options = {}) {
this._sender = options.sender || 'cumulus-ecs-task';
}
/**
* Setter for the sender property
*
* @param {string} value - the sender for the log messages
*/
set sender(value) {
this._sender = value;
}
/**
* Log an info message
*
* @param {string} message - the message to log
* @returns {undefined} no return value
*/
info(message) {
this.writeMessage('info', message);
}
/**
* Log an error message
*
* @param {string} message - the message to log
* @param {Error} err - the error to log
* @returns {undefined} no return value
*/
error(message, err) {
let msg;
if (err.stack) {
msg = `${message} ${err.stack.replace(/\n/g, ' ')}`;
}
else {
msg = err;
}
this.writeMessage('error', msg);
}
/**
* Log a message to stdout
*
* @param {string} level - the level of the message
* @param {string} message - the message to log
* @returns {undefined} no return value
* @memberof Logger
*/
writeMessage(level, message) {
const output = {
level,
message,
sender: this._sender,
timestamp: (new Date()).toISOString()
};
console.log(JSON.stringify(output));
}
}
module.exports = Logger;