1.0.0 • Published 8 years ago

log-with-console v1.0.0

Weekly downloads
1
License
ISC
Repository
github
Last release
8 years ago

log-with-console

Table of Contents

Installing

$ npm install log-with-console

Simple Logging

This plugin adds more debugging information to log lines generated by standard console logging methods.

Its simple to use, as it overrides console methods but adds more helpful details to log lines like datetimestamp, file name with line of execution, etc. Template/Pattern in generated log lines are configurable using setPattern method.

Usage

Load the package once in your source code (perfect place would be on entry point of your app). All native functionality support provided in global console object can be used.

Example

const logger=require("log-with-console");

console.log("Hello World!");
console.info("This is an info");
console.error("this is an error",new Error("Error occurred"));

Output:

[Tue Oct 11 2016 13:31:26 GMT+0530 (IST)] [MASTER] [app.js:3:9] LOG Hello World!
[Tue Oct 11 2016 13:31:26 GMT+0530 (IST)] [MASTER] [app.js:4:9] INFO This is an info
[Tue Oct 11 2016 13:31:26 GMT+0530 (IST)] [MASTER] [app.js:5:9] ERROR this is an error [Error: Error occurred]

Configuring Pattern

Pattern/Template layout on the log lines can be changed using setPattern method

logger.setPattern(patternList)

Available Patterns

  • date - datetimestamp
  • processname - name of the process running. Will be helpful in case of clustering! - identifies Master & Worker processes
  • file - filename with line number of execution

Example

const logger=require("log-with-console");

logger.setPattern("date","file");

console.log("Hello World!");
console.info("This is an info");
console.error("this is an error",new Error("Error occurred"));
[Tue Oct 11 2016 13:56:23 GMT+0530 (IST)] [app.js:6:9] LOG Hello World!
[Tue Oct 11 2016 13:56:23 GMT+0530 (IST)] [app.js:7:9] INFO This is an info
[Tue Oct 11 2016 13:56:23 GMT+0530 (IST)] [app.js:8:9] ERROR this is an error [Error: Error occurred]
const logger=require("log-with-console");

logger.setPattern("file");

console.log("Hello World!");
console.info("This is an info");
console.error("this is an error",new Error("Error occurred"));
[app.js:6:9] LOG Hello World!
[app.js:7:9] INFO This is an info
[app.js:8:9] ERROR this is an error [Error: Error occurred]

License

Released under MIT License