/
index.js
97 lines (79 loc) · 2.38 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
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
//
// Winston transport for residue
//
// Copyright 2017-present Amrayn Web Services
//
// Author: @abumusamq
//
// https://muflihun.com
// https://amrayn.com
// https://github.com/amrayn/residue-winston
//
"use strict";
const util = require('util');
const ResidueClient = require('residue');
const winston = require('winston')
const TransportStream = require('winston-transport');
const residueInstance = new ResidueClient();
const Residue = module.exports = function(options) {
if (!options) {
throw "Please provide residue-node options";
}
if (!options.logger_id) {
throw "Please provide logger_id in options";
}
TransportStream.call(this, options);
if (options.config_file) {
residue_instance.loadConfiguration(options.config_file);
} else {
residue_instance.loadConfiguration(options);
}
residueInstance.connect();
this.logger = residueInstance.getLogger(options.logger_id);
this.logger.log_sources = {
base_idx: this.logger.log_sources.base_idx + (options.log_source_layer_count || 6),
getSourceFile: this.logger.log_sources.getSourceFile,
getSourceLine: this.logger.log_sources.getSourceLine,
getSourceFunc: this.logger.log_sources.getSourceFunc,
};
};
// Inherit from `winston.Transport`
util.inherits(Residue, TransportStream);
// Transport name
Residue.prototype.name = 'residue';
// Backwards compatibility
winston.transports.Residue = Residue;
// Winston Transport Function
Residue.prototype.log = function(arg1, arg2, meta, cb) {
const level = typeof arg1 === 'string' ? arg1 : arg1.level;
const msg = typeof arg2 === 'string' ? arg2 : arg1.message;
const callback = typeof arg2 === 'function' ? arg2 : cb;
switch (level) {
case 'info':
this.logger.info(msg);
break;
case 'error':
this.logger.error(msg);
break;
case 'warn':
this.logger.warn(msg);
break;
case 'debug':
this.logger.debug(msg);
break;
case 'silly':
this.logger.info(msg);
break;
case 'verbose':
this.logger.verbose(1, msg);
break;
default:
this.logger.info(msg);
break;
}
this.emit('logged');
if (callback) {
callback();
}
};
module.exports.instance = residueInstance;