Giter VIP home page Giter VIP logo

node-booker's Introduction

Loggie

Loggie is a lovely logger for node.js which is SMART, SMALL, and EASY-TO-USE.

Loggie has few options, which makes it work at your will.

Installation

npm install loggie --save

Usage

For most cases, you could use loggie immediately with no extra configurations

var loggie = require('loggie');
logger = loggie();

logger.info('{{cyan install}}', 'loggie'); // will print a cyan 'install', space, and 'loggie'.

You could use typo template here to format your output.

There're several built-in log methods.

Built-in log methods

Method) Enabled By default) Binded Argv) Leading String)
verbose no --verbose 'VERB ' in gray
debug no --debug '[D] ' in magenta
error yes bold 'ERR! ' in red
warn yes 'WARN ' in yellow
info yes (nothing)
logger.debug('blah-blah'); // will do nothing

Because 'debug' is not enabled by default.

But if you start your app with '--debug' option, logger.debug will be activated. Or, you could add 'debug' into log levels in loggie options or with logger.addLevel method.

Programming

loggie(options)

Will create a new loggie instance

options.level

Array.<String>|String

options.level is just the loggie methods you want to activate. For example:

var logger = loggie({
	level: 'info, error' // can also be ['info', 'error']
});

Then, logger.warn('blah-blah') will be deactivated and do nothing.

If set to '*', Loggie will enable all log methods.

options.use_exit

Boolean=

Default to true

If set to true, Loggie will detect 'exit' event of process, if process exited without logger.end() method, it will be considered as a failure.

Best practices

var logger = loggie({
	level: process.env['MY_LOG_LEVEL'] || 
	
		// log level for production
		'info, error, warn'
});

And your environment variables (maybe on Mac OS) could be:

# file: ~/.profile
export MY_LOG_LEVEL=debug,info,error,warn

So, you can use local settings for debugging and development, and will never worry about the possible forgetness of changing debug configurations to production.

Define custom log methods

logger.register({
	detail: {
		template: '{{cyan|bold|underline Detail}} {{arguments}}', // typo template
		argv: '--detail'
	}
});

Then, we defined a log method logger.detail():

logger.detail('a'); // might print a bold cyan 'detail' with a underline, a whitespace, and an 'a'

By default, logger.detail() will do nothing because it is not in the log level list(options.level), but it will be activated if your app is started with '--detail' argument.

You can also use logger.addLevel('detail') to add 'detail' to level list.

.register(methods)

.register(name, setting)

Define your own log method. You can also use this method to override existing log methods.

name

String

The name of the log method you want. If you register() with name = 'verbose', there will be a logger.verbose() method.

setting.template

String

A typo syntax template.

There are several built-in template parameters to be used:

'arguments': arguments joined by a single whitespace (' ')

number: such as '0', the argument at index 0

If you use the template, all arguments will be stringified

Example

logger.register('verbose', {
	template: '{{gray verbose}} {{0}} {{arguments}}'
	// notice that the first argument will be duplicated once
});
logger.verbose('mylabel', 'blah', new Error('error:blah-blah'));

Will print: verbose(gray) mylabel mylabel blah error:blah-blah

setting.fn

function()

The log method.

Notice that if setting.template is defined, setting.fn will be overridden.

node-booker's People

Stargazers

Jagger Yu avatar

Watchers

Kael avatar James Cloos avatar  avatar

node-booker's Issues

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.