Node.js Logging
Solution 1:
Winston is a pretty good logging library. You can write logs out to a file using it.
Code would look something like:
var winston = require('winston');
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({ json: false, timestamp: true }),
new winston.transports.File({ filename: __dirname + '/debug.log', json: false })
],
exceptionHandlers: [
new (winston.transports.Console)({ json: false, timestamp: true }),
new winston.transports.File({ filename: __dirname + '/exceptions.log', json: false })
],
exitOnError: false
});
module.exports = logger;
You can then use this like:
var logger = require('./log');
logger.info('log to file');
Solution 2:
Scribe.JS Lightweight Logger
I have looked through many loggers, and I wasn't able to find a lightweight solution - so I decided to make a simple solution that is posted on github.
- Saves the file which are organized by user, date, and level
- Gives you a pretty output (we all love that)
- Easy-to-use HTML interface
I hope this helps you out.
Online Demo
http://bluejamesbond.github.io/Scribe.js/
Secure Web Access to Logs
Prints Pretty Text to Console Too!
Web Access
Github
https://github.com/bluejamesbond/Scribe.js
Solution 3:
Log4js is one of the most popular logging library for nodejs application.
It supports many cool features:
- Coloured console logging
- Replacement of node's console.log functions (optional)
- File appender, with log rolling based on file size
- SMTP, GELF, hook.io, Loggly appender
- Multiprocess appender (useful when you've got worker processes)
- A logger for connect/express servers
- Configurable log message layout/patterns
- Different log levels for different log categories (make some parts of your app log as DEBUG, others only ERRORS, etc.)
Example:
Installation:
npm install log4js
-
Configuration (
./config/log4js.json
):{"appenders": [ { "type": "console", "layout": { "type": "pattern", "pattern": "%m" }, "category": "app" },{ "category": "test-file-appender", "type": "file", "filename": "log_file.log", "maxLogSize": 10240, "backups": 3, "layout": { "type": "pattern", "pattern": "%d{dd/MM hh:mm} %-5p %m" } } ], "replaceConsole": true }
-
Usage:
var log4js = require( "log4js" ); log4js.configure( "./config/log4js.json" ); var logger = log4js.getLogger( "test-file-appender" ); // log4js.getLogger("app") will return logger that prints log to the console logger.debug("Hello log4js");// store log in file