BaseTransport.js
2.02 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
86
87
88
89
90
91
"use strict";
//winston logger
const Transport = require('winston').Transport,
os = require('os');
/**
* @class BaseTranport
* @description Logger transport base class
* @author hbomb qiqi.zhou@gmail.com
*/
class BaseTranport extends Transport {
/**
* @description init options
*/
constructor(options) {
super(options);
this.level = options.level || 'info';
this.options = options;
this.logs = [];
}
/**
* @method log
* @description tranport log to influxdb
* @param {String} level log level
* @param {String} msg log message
* @param {Object} meta log meta object
* @param {Function} callback callback function
*/
log(level, msg, meta, callback) {
if (!this.options.measurement) {
this.options.measurement = 'log';
}
//make influxdb create line
this.options.line = this.makeLine(this.options.measurement, level, msg, meta);
this.addLine();
callback(null, true);
}
/**
* @method addLine
* @description abstract method,for sub class implement
*/
addline() {
}
/**
* @method makeLine
* @description format line protocol
* @param {String} measurement influxdb's measurement
* @param {String} level log level
* @param {String} msg log message
* @param {String} meta log meta
*/
makeLine(measurement, level, msg, meta) {
//if meta is object to string.
if (typeof meta === 'object') {
meta = JSON.stringify(meta);
}
//make tags
let tags = [
'host=' + os.hostname(),
'pid=' + process.pid,
'level=' + level,
]
//make fields
let fields = [
'message="' + msg + '"',
'meta="' + meta + '"'
];
//make a line
tags = tags.join(',');
fields = fields.join(',');
return measurement + ',' + tags + ' ' + fields;
}
}
module.exports = BaseTranport;