-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.js
134 lines (109 loc) · 3.13 KB
/
log.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* @Author: Microsoft ARD Incubation Team
* @Date: 16-Mar-2017
* @Email: [email protected]
* @File: log.js
* @Project: Health Care
* @Last modified by: Arthur Jiang
* @Last modified time: 11:19:03+08:00 16-Mar-2017
* @License: Copyright © 2016 Microsoft. All rights reserved.
*/
const dgram = require('dgram');
const client = dgram.createSocket('udp4');
const debug = require('debug');
const chalk = require('chalk');
const os = require('os');
const uuid = require('uuid/v1');
const usr = process.env.USER;
const logHost = process.env.NOMAD_META_FLUENTD_HOST;
const logPort = process.env.NOMAD_META_FLUENTD_PORT;
const colDebug = process.env.COL_DEBUG;
const isColor = process.env.IS_COLOR == 'true';
const isUdp = logHost && logPort;
const logColor = {
T: chalk.blue,
D: chalk.white.bgBlack,
I: chalk.green,
W: chalk.yellow,
E: chalk.red,
F: chalk.red.bgYellow.bold
};
class Log {
constructor(options) {
this.tag = (options && options.tag) || `${os.hostname()}:${usr}:${process.pid}`;
this.instance = (options && options.instance) || `${os.hostname()}:${process.pid}`;
this.scope = (options && options.scope) || `${os.hostname()}:${usr}:${process.pid}`;
this.session = (options && options.session) || `${uuid()}`;
if (!isUdp) {
this.locDebug = debug(this.tag);
}
this._nomalize();
}
trace() {
this._dispatch('T', arguments);
}
debug() {
this._dispatch('D', arguments);
}
info() {
this._dispatch('I', arguments);
}
warn() {
this._dispatch('W', arguments);
}
error() {
this._dispatch('E', arguments);
}
fatal() {
this._dispatch('F', arguments);
}
_nomalize() {
this.tag = this.tag.toString().replace(/\s+/, '_');
this.instance = this.instance.toString().replace(/\s+/, '_');
this.scope = this.scope.toString().replace(/\s+/, '_');
this.session = this.session.toString().replace(/\s+/, '_');
}
_dispatch(level, args) {
let msg = Array.from(args).map(arg => typeof arg == 'object' ? JSON.stringify(arg) : arg).join(' ');
let formattedMsg = this._formatter(level, msg);
if (isUdp) {
this._udpLog(formattedMsg);
} else {
if (colDebug != '*') {
formattedMsg = this._cutter(level, msg);
}
if (isColor) {
this.locDebug(`${logColor[level](`${formattedMsg}`)}`);
} else {
this.locDebug(`${formattedMsg}`);
}
}
}
_udpLog(msg) {
let buf = new Buffer(msg);
client.send(buf, 0, buf.length, logPort, logHost, this._blackHole);
}
_blackHole() {
return;
}
_formatter(level, meta) {
return [this.tag, level, [this.session, this.instance, this.scope].join(':'), meta].join(' ');
}
_cutter(level, meta) {
if (colDebug) {
return [
colDebug.includes('tag') ? this.tag : '',
colDebug.includes('level') ? level : '',
[
colDebug.includes('session') ? this.session : '',
colDebug.includes('instance') ? this.instance : '',
colDebug.includes('scope') ? this.scope : ''
].join(':'),
meta
].join(' ');
} else {
return meta;
}
}
}
module.exports = Log;