Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

logops

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

logops

Simple and performant nodejs logger

  • 0.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.1K
decreased by-14.93%
Maintainers
1
Weekly downloads
 
Created
Source

logops

Really simple and performant logger for node.js projects.

Installation

npm install logops

Basic usage

var logger = require('logops');

//plain strings
logger.debug('This is an example');

//util.format support
logger.info('Request %s %d %j', 'is', 5, {key: 'value'});

//Multi string
logger.warn('Something went wrong:', value);

//error to print stack traces
logger.error(new Error('Something went REALLY wrong'));

//errors as parameter to print messages only
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");
//output: {"time":"2015-06-11T08:36:16.628Z","lvl":"INFO","corr":null,"trans":null,"op":null,"msg":"This is an example: 5", "key: "value"}

logger.format = logger.formatters.pipe;
logger.info('This is an example: %d', 5, {key:"value");
//output: time=2015-06-11T08:36:16.628Z | lvl=INFO | corr=n/a | trans=n/a | op=n/a | msg=This is an example: 5 { key: 'value' }

logger.format = logger.formatters.dev;
logger.info('This is an example: %d', 5, {key:"value"});
//output: INFO This is an example: 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');

// {String} level one of the following values ['DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL']
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');
/**
 * Return a String representation for a trace.
 * @param {String} level One of the following values
 *      ['DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL']
 * @param {Object} context Additional information to add to the trace
 * @param {String} message The main message to be added to the trace
 * @param {Array} args More arguments provided to the log function
 *
 * @return {String} The trace formatted
 */
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.

Keywords

FAQs

Package last updated on 15 Jun 2015

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc