logginator
Advanced tools
Comparing version 1.1.2 to 1.1.3
103
index.js
var fs = require('fs'); | ||
var path = require('path'); | ||
var os = require('os'); | ||
var winston = require('winston'); | ||
var winstonSyslog = require('winston-syslog').Syslog; | ||
var TaggedConsoleTarget = require('tagged-console-target'); | ||
var TaggedLogger = require('tagged-logger'); | ||
var moduleName = require('./module-name'); | ||
function moduleName() { | ||
function moduleId() { | ||
if (module && module.parent && module.parent.id) { | ||
var id = module.parent.id; | ||
var moduleFile = path.basename(id); | ||
var nameMatch = /(.*)\.js$/.exec(moduleFile); | ||
if (nameMatch && nameMatch[1]) | ||
return nameMatch[1]; | ||
} | ||
} | ||
function packageDef() { | ||
var ppath = path.dirname(module.parent.filename); | ||
while (ppath) { | ||
var packFile = path.join(ppath, "package.json"); | ||
if (fs.existsSync(packFile)) { | ||
try { | ||
var packageConfig = JSON.parse(fs.readFileSync(packFile)); | ||
return packageConfig.name; | ||
} catch(_) { /* ignore */ } | ||
// List of syslog levels, because the one present in winston is | ||
// incorrect and makes logging fail. | ||
var syslogLevels = { | ||
debug: 0, | ||
info: 1, | ||
notice: 2, | ||
warning: 3, | ||
warn: 3, // Keep warn for API compatibility | ||
error: 4, | ||
crit: 5, | ||
alert: 6, | ||
emerg: 7 | ||
}; | ||
// Monkey-patch winston-syslog to treat "warn" as "warning", to | ||
// maintain API compatibility. | ||
var originalSyslogLog = winstonSyslog.prototype.log; | ||
winstonSyslog.prototype.log = function (level, msg, meta, callback) { | ||
if (level === "warn") level = "warning"; | ||
return originalSyslogLog.call(this, level, msg, meta, callback); | ||
}; | ||
var engines = { | ||
"console": function () { return new TaggedConsoleTarget(); }, | ||
"syslog": function (spec) { | ||
var options = {}; | ||
var LINUX_LOG = "/dev/log", BSD_LOG = "/var/run/log"; | ||
options.protocol = spec.protocol || "unix"; | ||
if (options.protocol === "unix") { | ||
options.path = spec.path; | ||
if (!options.path) { | ||
if (fs.statSync(LINUX_LOG).isSocket()) options.path = LINUX_LOG; | ||
else if (fs.statSync(BSD_LOG).isSocket()) options.path = BSD_LOG; | ||
else { | ||
throw new Error("Failed to find log socket path, and no such " + | ||
"path was configured in \"path\" for the syslog logger backend"); | ||
} | ||
} | ||
ppath = path.dirname(ppath); | ||
} | ||
// For TCP or UDP: | ||
options.host = spec.host; | ||
options.port = spec.port; | ||
options.app_name = spec.appname || moduleName(module); | ||
options.facility = spec.facility; | ||
options.localhost = spec.localhost || os.hostname(); | ||
return new winstonSyslog(options); | ||
} | ||
function filename() { | ||
return path.basename(module.parent.filename); | ||
} | ||
return moduleId() || packageDef() || filename() || "main"; | ||
} | ||
}; | ||
var defaultConfig = [ { "transport": "console" } ]; | ||
module.exports = function (config) { | ||
var winstonConfig = {}; | ||
if (config) { | ||
// Currently no config format to understand. | ||
} else { | ||
// Default to console output if no config specified. | ||
winstonConfig = { transports: [ new TaggedConsoleTarget() ] }; | ||
} | ||
config = config || defaultConfig; | ||
if (!Array.isArray(config)) config = []; // Handle {} as no output | ||
var winstonConfig = { | ||
transports: [] | ||
}; | ||
config.forEach(function (spec) { | ||
if (engines.hasOwnProperty(spec.transport)) { | ||
winstonConfig.transports.push(engines[spec.transport](spec)); | ||
} | ||
}); | ||
var winstonLogger = new (winston.Logger)(winstonConfig); | ||
winstonLogger.setLevels(syslogLevels); | ||
var log = new TaggedLogger(winstonLogger, []); | ||
var tag = moduleName(); | ||
var tag = moduleName(module); | ||
return log.createSublogger(tag); | ||
}; |
{ | ||
"name": "logginator", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "Creates an instance of TaggedLogger", | ||
@@ -9,3 +9,4 @@ "main": "index.js", | ||
"tagged-console-target": "~1.0.4", | ||
"tagged-logger": "~1.0.0" | ||
"tagged-logger": "~1.0.0", | ||
"winston-syslog": "git://github.com/indexzero/winston-syslog.git#v0.2.3" | ||
}, | ||
@@ -12,0 +13,0 @@ "repository": "https://github.com/brikteknologier/logginator.git", |
@@ -16,5 +16,56 @@ Installation | ||
Optional. If left empty, will default to console output. | ||
Optional. If left unspecified, logginator will default to console output. | ||
If specified, is assumed to define where the output will go. This is currently not | ||
defined, so the only valid value is `{}`, which results in no output. | ||
To configure the backends, specify an array with the desired backend | ||
configurations, for example: | ||
var log = require('logginator')([ | ||
{ | ||
"type": "console" | ||
}, { | ||
"type": "syslog" | ||
} | ||
]); | ||
console | ||
------- | ||
To output logs to the console, use this configuration: | ||
{ | ||
"type": "console" | ||
} | ||
Console output has no configuration options. | ||
syslog | ||
------ | ||
To output logs to syslog, this configuration is sufficient: | ||
{ | ||
"type": "syslog" | ||
} | ||
Additional options are: | ||
* `appname`: The name this process should use to identify itself to syslog. By | ||
default, logginator tries to deduce the name of the node project that uses | ||
logginator as a module. | ||
* `localhost`: The hostname of the current machine, as sent to syslog. Defaults | ||
to `os.hostname()`. | ||
* `facility`: The facility, in syslog terminology, that the logger should log | ||
to. This concept only makes sense in a syslog setting, so refer to syslog | ||
documentation if you want to make an informed choice. Otherwise, stick with | ||
the default value, which is `"local0"`. | ||
* `protocol`: The protocol via which to log. The default, and recommended, | ||
value, is `"unix"`, which makes logging go via Unix datagram sockets to the | ||
path specified in the `path` option. Other choices are `"tcp4"`, `"tcp6"`, | ||
`"udp4"` and `"udp6"`, which all require `host` and `port` to be specified. | ||
* `path`: The path to log to when using `"unix"` for `protocol`. If not set, | ||
logginator will try to deduce the default system log pipe by trying | ||
`/dev/log` and `/var/run/log`. If both of these fail, logginator will raise | ||
an exception. | ||
* `host` and `port`: The host and port pair for the TCP or UDP log target if | ||
using any other protocol than `"unix"`. Note that the target syslog daemon | ||
must be configured to accept connections on the specified protocol. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Git dependency
Supply chain riskContains a dependency which resolves to a remote git URL. Dependencies fetched from git URLs are not immutable can be used to inject untrusted code or reduce the likelihood of a reproducible install.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6201
7
104
71
4
1
3