logops
Really simple and performant logger for node.js projects.
Installation
npm install logops
Basic usage
var logger = require('logops');
logger.debug('This is an example');
logger.info('Request %s %d %j', 'is', 5, {key: 'value'});
logger.warn('Something went wrong:', value);
logger.error(new Error('Something went REALLY wrong'));
logger.fatal('SYSTEM UNSTABLE. BYE', error);
Advanced usage
Context support
Logops supports using a context holding information about a correlator (corr), transaction (trans) and operation (op).
If you pass a context object as a first argument, those fields are also logged as separate fields.
var logger = require('logops');
var context = {
corr: 'cbefb082-3429-4f5c-aafd-26b060d6a9fc',
trans: '110ec58a-a0f2-4ac4-8393-c866d813b8d1',
op: 'SendEMail'
};
logger.debug(context, 'This is an example');
If you are holding your context information in other places, like Domains, you don't
need to pass a context to every log function. Simply override the logger.getContext
method to let the logger to get it.
var logger = require('logops');
logger.getContext = function getDomainContext() {
return require('domain').active.myContextObject;
}
logger.debug('This is an example');
Trace format
This library incorporates three flavors of trace formatting:
- "json": writes logs as JSON.
- "pipe": writes logs separating fields with pipes. This is the default value in logops
- "dev": for development, used if the 'de-facto' NODE_ENV variable is set to 'development'
logger.format = logger.formatters.json;
logger.info('This is an example: %d', 5, {key:"value");
logger.format = logger.formatters.pipe;
logger.info('This is an example: %d', 5, {key:"value");
logger.format = logger.formatters.dev;
logger.info('This is an example: %d', 5, {key:"value"});
Logger Level
You can set the logging level at any time. All the disabled logging methods are replaced by a noop,
so there is not any performance penalty at production using an undesired level
var logger = require('logops');
logger.setLevel('DEBUG');
Writing to files
This library writes by default to process.stdout
, the safest, fastest and easy way to manage logs. It's how you execute your app when you define how to manage logs.
This approach is also compatible with logratate as this is how many servers and PaaS manage the logs.
Therefore you don't need to put anything in your source code relative to logs, and all is done at execution time depending on the deployment.
Recommended execution: Pipelining the stdout to [tee](http://en.wikipedia.org/wiki/Tee_(command).
With this configuration, you will not fail when the disk is full. It's also the best
performant solution
node index.js | tee -a ./out.log > /dev/null
You can also write logs and fail miserably stopping your app when the disk is full by doing
node index.js > ./out.log
Please read carefully in the node documentation how the stdout
/stderr
stream behaves regarding synchronous/asynchronous writing
Customization
Trace format
You can override the format function and manage by yourself the formatting taking into account your own environment variables by
overriding the logger.format
function
var logger = require('logops');
logger.format = function myCustomFormat(level, context, message, args) {
var str = '';
return str;
};
Output stream
If you want to pipe the output stream to any other stream in your source code, or even write to files (not recommended),
you can override the stream used by this library
var logger = require('logops');
logger.stream = new MyOtherSuperStreamThatDoesGreatThingsExceptWriteToDisk();
More info
License
Copyright 2014, 2015 Telefonica Investigación y Desarrollo, S.A.U
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.