Giter Club home page Giter Club logo

uber-node-spruce's Introduction

Spruce

Spruce is a configurable node.js logging library. Its coolest feature is the ability to use custom logging handlers, so you can do something like this:

 var emailDevelopers = function(message){
        sendMail( {'from' : '[email protected]',
                    'to' : '[email protected]',
                    'subject' : 'error: '+message,
                    'body' : makeMessageBody(message)});
        //do some hackery here
    };
 var options = { 'methods' :
                    { 'error' : 
                        { 'handlers' :  [ emailDevelopers ]},
                    }
                }; 

var spruce = require('spruce').init(options);
spruce.error('My eyes! The goggles do nothing!');

and then all of your developers will get an email from McBain deploring the quality of his optical protection.

Usage

Download and add to your code, or use npm:

npm install spruce

Then, do this:

var spruce = require('spruce').init();

The variable 'spruce' will now be be an object with several functions:

{ 'info' : [Function],
  'trace' : [Function],
  'debug' : [Function],
  'warn' : [Function],
  'error' : [Function]}

Each of these functions takes a single argument, the message, and logs it and some other cool stuff to the console. For example, this code:

spruce.info('Yes! we have no bonanza!')

produces this output: [2011-05-20 09:16:05.030] INFO [readme.js:3:8] - Yes! we have no bonanaza!

Options

Everybody loves options! Spruce is highly configurable, so you can do a lot of neat stuff with it. Here are the features it supports:

  • colored console printing.
  • custom date formatting.
  • custom log levels.
  • print module name and line number of log message.
  • handler functions for any log level.
  • will not pee on the floor or complain about your taste in clothes.

Here are the default options:

var defaultOptions = {
        'dateFormat' : '[%y-%m-%d %H:%M:%S.%l]',
        'stripNewlines' : true,
        'methods' : {
            'info' : {'lineNum' : false,
                        'color' : 30,
                        'handlers': []},
            'trace' : {'lineNum' : true,
                        'color' : 30,
                        'handlers': []},
            'debug' : {'lineNum' : true,
                        'color' : 34,
                        'handlers' : []},
            'warn' : {'lineNum' : true,
                     'color' : 35,
                    'handlers' : []},
            'error': {'lineNum' : true,
                      'color' :  31,
                      'handlers' : []},
            },
        'moduleName' : null,
        'useColor' : false,
};

stripNewlines determines whether the logger should remove newlines from all log messages. This is useful when logging things like xml or json.

dateFormat is the string used to specify how the date should appear. it uses standard strftime options, although I did not implement day of the year (%j) or week of the year (%u).

methods is a config object for each of the log methods you want to have in your logging system. The options for each of these objects are explained below.

By default, every logger returned will have have info, trace, debug, info, warn, and error methods. You are free to add as many as you would like.

moduleName is used to print the name of the module where the log message occured. if moduleName is null, only the file and line where the error occurred is printed. Note that moduleName only takes effect for methods that have the lineNum option enabled. To use this effectively, you will need to specify the module you're in every time you require the spruce module.

useColor determines whether to use awesome colors or boring plain text.

Method Options

color: what color to display this text in, if useColor is enabled.

lineNum: whether to compute a stack trace to get the line number. This is disabled for the info level be default, for performance reasons.

handlers: a list of single-argument functions to be called with the log message whenever that log method is called. Using handlers you can do things like:

  • Email all developers whenever an error occurs
  • Dump all log messages matching a regex into a database
  • Find log messages encoding turing machines that halt for every input and... well, maybe not.

Example

var move = function(zig) { //for great justice! }; 
var opts = {'methods' : {'operator': { 'handlers' : [move]}}}; 
var spruce = require('spruce').init(opts);

if (somebodySetUpUsTheBomb())
    spruce.operator('Main Screen Turn on!')

... and then operator will move the zig when the bomb is set up.

License

MIT License. Enjoy and Fork!

Credits

Based upon node-logger by igo.

uber-node-spruce's People

Contributors

neyer avatar squamos avatar

Watchers

 avatar

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.