logplease
Simple Javascript logger for Node.js and Browsers
DEMO Open the dev tools to see the log output
logplease does two simple things: output log messages to the console and/or to a file (Node.js only) and display the log messages with nice colors. Inspired by log4js and debug.
Features
- Log messages to stdout or a file
- Customize the log messages
- Log levels
- Colors!
- Work in Node.js and Browsers
Install
npm install logplease
Examples
Node.js
npm run example
Browser
Open example/index.html
in your browser.
Usage
Node.js / Webpack
See example/example.js for details.
const Logger = ;const logger = Logger;logger;logger; // alias for debug()logger;logger;logger;
Default log level is DEBUG
. You can set the log level with LOG
environment variable, eg. LOG=debug node example/example.js
. See Log levels for available options.
There's an ES5 build in es5/
which you can include if you need ES5 compatibility, eg. with Webpack.
Browser
Copy dist/logplease.min.js
to your javascripts directory and include it in your html. See example/index.html for details.
Options
You can customize your logger to not show the timestamp or the log level, disable colors or specify, if using a log file, to overwrite the log file at start instead of appending to it.
const Logger = ;const logger = Logger;
Available options and defaults:
const options = useColors: true // Enable colors color: ColorsWhite // Set the color of the logger showTimestamp: true // Display timestamp in the log message useLocalTime: false // Display timestamp in local timezone showLevel: true // Display log level in the log message filename: null // Set file path to log to a file appendFile: true // Append logfile instead of overwriting;
Log levels
DEBUG
INFO
WARN
ERROR
NONE
Default log level is DEBUG
. To display errors only, use ERROR
. To turn off all logging, use NONE
.
Global log level
You can set a global log level to display only the wanted log messages.
const Logger = ;Logger // Show only ERROR messages// orLogger
You can mute all loggers with log level NONE:
Logger // output nothing
Global log file
You can set a global log file to which all loggers write to.
const Logger = ;const logger1 = Logger;const logger2 = Logger;Logger;logger1;logger2;// ==> 'debug.log' contains both log messages
Log file
You can set a log file per logger.
const Logger = ;const logger1 = Logger;const logger2 = Logger;logger1; // writes to 'debug.log'logger2; // doesn't write to 'debug.log'
Colors
You can set a color per logger. Default color in Node.js is White and in the browser Black.
const Logger = ;const logger = Logger;
Colors:
Black, Red, Green, Yellow, Blue, Magenta, Cyan, Grey, White
Tests
Run tests with:
npm test
Build
Install build dependencies:
npm install
Build the browser distributable and examples:
npm run build
Build the browser distributable only:
npm run build:dist
The distributable file will be located in dist/logplease.min.js
Build the browser example:
npm run build:examples