Giter Site home page Giter Site logo

nice-logger's Introduction

nice-logger

A logger with levels and a nice format.

Example

var Logger = require("nice-logger");

var log = new Logger("main", "error");

try {
    log.debug("Uh oh.");
    /* Something dangerous. */
} catch (error) {
    log.error(error);  // Stack trace is automatically logged for Error objects.
}

Usage

Use it like log.<level>(arg1, arg2, ...). You can pass in as many arguments as you'd like. Anything that is not an Error or a String will be JSON stringified.

The logger normally writes to process.stdout. If any of the arguments is an Error object, the logger will write the whole message to stderr instead of stdout (so call error.toString() first if you'd like the logger to write to stdout.

Levels

The five allowed levels are, in descending order of priority:

  1. critical
  2. error
  3. warning
  4. info
  5. debug

You can (optionally) pass in a level as the second argument of the Logger constructor. This will set the default level. If you pass in something unrecognized, or don't pass in anything at all, the default level is set lower than debug. In other words, everything will get logged by default.

Methods

Logging methods:

  • #critical(...): Log a message at the critical level.
  • #error(...): Log a message at the critical level.
  • #warning(...): Log a message at the warning level.
  • #info(...): Log a message at the info level.
  • #debug(...): Log a message at the debug level.

Other methods:

  • #out(String): Write to process.stdout.
  • #err(String): Write to process.stderr.
  • #setLevel(level): Change the logger level.

nice-logger's People

Contributors

tdb-alcorn avatar

Watchers

James Cloos avatar  avatar

nice-logger's Issues

Functions are not printed when they are attributes of objects

var obj = {x: 1, y: "foo", z: function() { return true; }};
log.info(obj);
>> {"x": 1, "y": "foo"}

This occurs because the logger defaults to calling JSON.stringify on anything that isn't a String or an Error, and JSON.stringify does not serialize functions because it is intended as a wire serialization format.

Add an object stringifier (that makes an effort to print everything, including functions), and call it in a new case for handling objects.

Errors where the constructor is not `Error` are swallowed.

For example, TypeErrors fall through to the default handler (which JSON.stringifys them, effectively swallowing them). The handler should instead check err instanceof Error, or possibly just check for the existence of the .stack key.

Output JSON

Create an option to output json logs for use with a service like loggly.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.