Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Insanely configurable logging for Node.js
Sponsored by Leadnomics.
NPM has no shortage of loggers. Bristol was created to address a few common shortcomings in the current pool of options:
util.format
; if you're injecting
values into a string, chances are they should be logged under their own
keys.Those points and more drove the development of a brand new breed of logging library. Introducing Bristol.
In your project folder, type:
npm install bristol --save
var log = require('bristol');
log.addTarget('console');
log.info("We're up and running!", {port: 3000});
Outputs (pretty-printed for README only):
{
"message": "We're up and running!",
"date": "2014-04-09 00:45:37",
"severity": "info",
"file": "/path/to/my/file.js",
"line": "4",
"port": 3000
}
Bristol can now be require()'d in any other file, and the settings will follow it.
log.addTarget('console')
.withFormatter('human');
log.debug("Hello, world", {event: 'bootstrap:welcome'});
Outputs:
[2014-04-09 00:53:59] DEBUG: Hello, world (/path/to/my/file.js:5)
event: bootstrap:welcome
By default, Bristol provides error, warn, info, debug, and trace
severity levels. These can be called as function names from the log object,
or passed to the log
function as the first parameter.
To change these levels to something else:
// List in order from MOST to LEAST severe:
log.setSeverities(['panic', 'omg', 'uhoh', 'crap', 'ok']);
// Functions matching the provided severities now exist
log.panic("WHAT ARE WE GOING TO DO?!");
log.log('omg', new Error("Something failed."));
// The old ones do not.
log.debug("This will throw an exception now");
log.log('trace', "So will this.");
Bristol allows you to send near-infinite arguments to the logging functions, and attempts to intelligently turn those into useful log messages. If your application has its own data structures, it can be useful to inform Bristol of them so that only pertinent values are logged if one of those is passed to a logging function.
log.addTransform(function(elem) {
// This function should return data that should be logged instead of
// the raw 'elem', or NULL if 'elem' isn't a type that we care about.
if (elem.userName && elem.userId) {
// elem is our enormous proprietary user object!
return {
username: elem.userName,
connected: elem.lastSuccessfulConnection,
ping: elem.getLastPingTime()
};
}
return null;
});
// Now our "incomingConn" user object will just add its 3 most important
// properties to the resulting log message, rather than all of them.
log.info("New connection", incomingConn, {connections: server.getUserCount()});
Do you need certain pieces of information logged with every message?
log.addGlobal('hostname', require('os').hostname);
log.addGlobal('msg_uuid', function() {
return uuid.v4();
});
log.info("This message contains both of those key/value pairs!");
Functions provided to addGlobal
will be executed for every log message.
Note that they're only called once per message even if multiple
targets have been added; therefore, the uuid in the example above will be
consistent across all configured targets.
Of course, you can also delete globals with:
log.deleteGlobal('msg_uuid');
// high_priority.log will contain only errors and warns
log.addTarget('file', {file: '/tmp/high_priority.log'})
.withLowestSeverity('warn');
// debug.log will only store debug messages
log.addTarget('file', {file: '/tmp/debug.log'})
.withLowestSeverity('debug')
.withHighestSeverity('debug');
// The console will only output trace messages
log.addTarget('console')
.withHighestSeverity('trace');
Sometimes, even within a severity level, it can be useful to filter out some kinds of log messages. Bristol leverages its key/value logging to allow you to blacklist or whitelist messages on a target, based on the values of certain keys.
// Don't log messages with event=disconnect, OR messages for certain channels
log.addTarget('console')
.withFormatter('human')
.excluding({
event: 'disconnect',
channel: ['#help', /^#anime.*$/]
});
// Only log the trace messages with event starting with "test", from userId
// 1, 2, or 3, and with destination set to a test server.
log.addTarget('file')
.withFormatter('commonInfoModel')
.withHighestSeverity('trace')
.onlyIncluding({
event: /^test:/,
userId: [1, 2, 3],
destination: function(val) { return isTestServer(val); }
Restrictions on keys can be static types like strings or numbers, RegExp objects to check for a match, functions to test the value each time the target is hit, or arrays of any of the above to allow more than one match. Exclusions and inclusions can also be combined in one target to summon Captain Planet.
Have a use case that requires more than one 'log' object, so you can maintain different sets of targets? No problem:
var log2 = new log.Bristol();
The default log object will remain the default, but now log2 is a completely independent instance with zero configuration.
Instead of passing in a target or formatter as a string, you can pass your own functions! Both of these modules are simply functions that take an options object as the first argument, and context-specific arguments after that. Check out some of the built-in targets and formatters for examples. They're super easy!
Testing requires the grunt-cli package to be globally installed. Do that with:
npm install -g grunt-cli
And then test with:
npm test
Bristol is distributed under the MIT license.
Bristol was created by Tom Frost at Leadnomics in 2014.
[v0.2.0]
FAQs
Insanely configurable logging for Node.js
The npm package bristol receives a total of 529 weekly downloads. As such, bristol popularity was classified as not popular.
We found that bristol demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.