-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
71 lines (57 loc) · 1.92 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
/*
* An Ain2 based Syslog transport for winston.
*/
// Load necessary modules
var util = require('util');
var winston = require('winston');
var SysLogger = require('ain2');
/**
* @constructor
*
* @param {Object} [options] - Options are passed through to ain2
*/
var SyslogAin2 = winston.transports.SyslogAin2 = function(options)
{
// Call parent transport constructor
winston.Transport.call(this, options);
// Name this logger
this.name = 'syslog-ain2';
// Set the level from your options
this.level = options.level || 'emerg';
// Setup the levels for checking
this.validLevels = Object.keys(winston.config.syslog.levels);
// Setup options if skipped
options = options || {};
// Setup a new ain2 instance with the options
this.ain = new SysLogger(options);
};
// Inherit from `winston.Transport` so you can take advantage
// of the base functionality and `.handleExceptions()`.
util.inherits(SyslogAin2, winston.Transport);
// Expose the name of this Transport on the prototype
SyslogAin2.prototype.name = 'SyslogAin2';
/**
* Core logging method exposed to Winston. Logs the `msg` and optional
* metadata, `meta`, to the specified `level`.
*
* @param {String} level - Target level to log to
* @param {String} msg - Message to log
* @param {Object} meta - **Optional** Additional metadata to log.
* @param {Function} callback - Continuation to respond to when complete.
*/
SyslogAin2.prototype.log = function(level, msg, meta, callback)
{
if (!~this.validLevels.indexOf(level)) {
return callback(new Error('Cannot log unknown syslog level: ' + level));
}
// Build a suffix if we got meta info
var suffix = (
typeof(meta) == 'object' && meta && Object.keys(meta).length
? (' ' + util.inspect(meta))
: ''
);
// Send the message to ain2
this.ain.send(msg + suffix, level);
// Inform winston
callback(null, true);
};