GithubHelp home page GithubHelp logo

khuongphp / node-console-stamp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from starak/node-console-stamp

0.0 1.0 0.0 215 KB

Patch NodeJS console methods in order to add timestamp information by pattern

JavaScript 100.00%

node-console-stamp's Introduction

console-stamp

npm downloads build

Attention!

Version 3.0.0 (! don't ask...) Release Candidate is now out! Try it out by using the @next tag like this:

npm install console-stamp@next

Documentation for 3.0.0 can be found here for now...

Note: There are breaking changes in the new version.


This module enables you to patch the console's methods in Node.js, to add timestamp prefix based on a given string pattern, and more...

Usage

Install

npm install console-stamp

Patching the console

require('console-stamp')(console, [options]);

console

The global console or custom console.

options {Object|String}

From version 2.0 the second parameter is an object with several options. As a backward compatibillity feature this parameter can be a string containing the pattern.

  • options.pattern {String}
    A string with date format based on Javascript Date Format
    Default: 'ddd mmm dd yyyy HH:MM:ss'

  • options.formatter {Function}
    A custom date formatter that should return a formmatted date string.

  • options.label {Boolean}
    If true it will show the label (LOG | INFO | WARN | ERROR)
    Default: true

  • options.labelPrefix {String}
    A custom prefix for the label.
    For an example see Custom prefix and suffix example
    Default: "["

  • options.labelSuffix {String}
    A custom suffix for the label.
    For an example see Custom prefix and suffix example
    Default: "]"

  • options.include {Array}
    An array containing the methods to include in the patch
    Default: ["log", "info", "warn", "error", "dir", "assert"]

  • options.exclude {Array}
    An array containing the methods to exclude in the patch
    Default: [] (none)

  • options.disable {Array}
    An array containing the methods to disable in the patch
    Default: [] (none)

  • options.level {String}
    A string choosing the most verbose logging function to allow. Ordered/grouped as such: 'log dir', 'info', 'warn assert', 'error'
    Default: log

  • options.extend {Object}
    An object describing methods and their associated log level, to extend the existing method <-> log level pairs.
    For an example see Custom methods.

  • options.metadata {String/Object/Function}
    Types can be String, Object (interpreted with util.inspect), or Function. See the test-metadata.js for examples.
    Note that metadata can still be sent as the third parameter (as in vesion 1.6) as a backward compatibillity feature, but this is deprecated.
    Default: undefined

  • options.stdout {WritableStream}
    A custom stdout to use with custom console.
    Default: process.stdout

  • options.stderr {WritableStream}
    A custom stderr to use with custom console.
    Default: options.stdout or process.stdout

  • options.colors {Object}
    An object representing a color theme. More info here.

    • options.colors.stamp {String/Array/Function}
      Default: []

    • options.colors.label {String/Array/Function}
      Default: []

    • options.colors.metadata {String/Array/Function}
      Default: []

  • options.datePrefix {String}
    A custom prefix for the datestamp.
    For an example see Custom prefix and suffix example
    Default: "["

  • options.dateSuffix {String}
    A custom suffix for the datestamp.
    For an example see Custom prefix and suffix example
    Default: "]" Note: To combine colors, bgColors and style, set them as an array like this:

...
    stamp: ['black', 'bgYellow', 'underline']
...

Or chain Chalk functions like this:

...
stamp: require('chalk').red.bgYellow.underline;
...

Note also that by sending the parameter --no-color when you start your node app, will prevent any colors from console.

$ node my-app.js --no-color

Example

// Patch console.x methods in order to add timestamp information
require('console-stamp')(console, { pattern: 'dd/mm/yyyy HH:MM:ss.l' });

console.log('Hello World!');
// -> [26/06/2015 14:02:48.062] [LOG] Hello World!

const port = 8080;
console.log('Server running at port %d', port);
// -> [26/06/2015 16:02:35.325] [LOG] Server running at port 8080

 

console.log('This is a console.log message');
console.info('This is a console.info message');
console.warn('This is a console.warn message');
console.error('This is a console.error message');
console.dir({bar: 'This is a console.dir message'});

Result:

[26/06/2015 12:44:31.777] [LOG]   This is a console.log message
[26/06/2015 12:44:31.777] [INFO]  This is a console.info message
[26/06/2015 12:44:31.779] [WARN]  This is a console.warn message
[26/06/2015 12:44:31.779] [ERROR] This is a console.error message
[26/06/2015 12:44:31.779] [DIR]   { bar: 'This is a console.dir message' }

and

require('console-stamp')(console, {
    metadata: function () {
        return ('[' + process.memoryUsage().rss + ']');
    },
    colors: {
        stamp: 'yellow',
        label: 'white',
        metadata: 'green'
    }
});

console.log('This is a console.log message');
console.info('This is a console.info message');
console.warn('This is a console.warn message');
console.error('This is a console.error message');
console.dir({bar: 'This is a console.dir message'});

Result:

Console

Custom Console [v0.2.4+]

As of version 0.2.4 you can also create a custom console with its own stdout and stderr like this:

const fs = require('fs');
const output = fs.createWriteStream('./stdout.log');
const errorOutput = fs.createWriteStream('./stderr.log');
const logger = new console.Console(output, errorOutput);

console_stamp(logger, {
    stdout: output,
    stderr: errorOutput
});

Everything is then written to the files.

NOTE: If stderr isn't passed, warning and error output will be sent to the given stdout.

Custom Formatter Example

Custom formatter using moment.js

const moment = require('moment');
moment.locale('ja');

require('console-stamp')(console, {
    formatter: function() {
        return moment().format('LLLL');
    }
});

console.log('This is a console.log message');
console.info('This is a console.info message');
console.warn('This is a console.warn message');
console.error('This is a console.error message');
console.dir({bar: 'This is a console.dir message'});

Result:

[2016年5月12日午前11時10分 木曜日] [LOG]   This is a console.log message
[2016年5月12日午前11時10分 木曜日] [INFO]  This is a console.info message
[2016年5月12日午前11時10分 木曜日] [WARN]  This is a console.warn message
[2016年5月12日午前11時10分 木曜日] [ERROR] This is a console.error message
[2016年5月12日午前11時10分 木曜日] [DIR]   { bar: 'This is a console.dir message' }

Custom Methods

The option.extend option enables the extension or modification of the logging methods and their associated log levels:

The default logging methods and their log levels are as follows:

const levelPriorities = {
    log: 4,
    info: 3,
    warn: 2,
    error: 1,
    assert: 2,
    dir: 4
};

Combined with the include option, the extend option enables the usage of custom console logging methods to be used with this module, for example:

// Extending the console object with custom methods
console.debug = function(msg) {
    console.log(msg);
}
console.fatal = function(msg) {
    console.log(msg);
    process.exit(1);
}

// Initialising the output formatter
require('console-stamp')(console, {
    pattern: 'HH:MM:ss',
    extend: {
        debug: 5,
        fatal: 0,
    },
    include: ['debug', 'info', 'warn', 'error', 'fatal'],
    level: 'debug',
});

Note how the log method is omitted from the include list. Because the custom functions call console.log internally, including the log method would print double-formatted output.

Adding Metadata

Types can be string, object (interpreted with util.inspect), or function. See the test-metadata.js for examples.

String example

require('console-stamp')(console, {
    pattern: 'HH:MM:ss.l',
    metadata: '[' + process.pid + ']'
});

console.log('Metadata applied.');

Result:

[26/06/2015 12:44:31.779] [LOG] [7785] Metadata applied.

Function example

const util = require('util');

require('console-stamp')(console, {
    pattern: 'HH:MM:ss.l',
    metadata: function(){ return '[' + (process.memoryUsage().rss) + ']'; });

console.log('Metadata applied.');

Result:

[18:10:30.875] [LOG] [14503936] Metadata applied.

Custom prefix and suffix example

If you don't want to use the default brackets, you can also define your own custom pre- and suffixes like so:

require('console-stamp')(console, {
    datePrefix: '####',
    dateSuffix: '####',
    labelPrefix: '{',
    labelSuffix: '}'
});

console.log('Custom pre- and suffixed log');

Result:

####Fri Sep 15 2017 16:58:29#### {LOG} Custom pre- and suffixed log                                                            

node-console-stamp's People

Contributors

starak avatar jotham avatar steffandonal avatar leonluc-dev avatar omgimalexis avatar christiaanwesterbeek avatar

Watchers

James Cloos 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.