diff --git a/README.md b/README.md index 502753f..4b1d10d 100644 --- a/README.md +++ b/README.md @@ -1,51 +1,32 @@ # log4js-node [![Build Status](https://secure.travis-ci.org/nomiddlename/log4js-node.png?branch=master)](http://travis-ci.org/nomiddlename/log4js-node) [![NPM](https://nodei.co/npm/log4js.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/log4js/) +[![codecov](https://codecov.io/gh/nomiddlename/log4js-node/branch/master/graph/badge.svg)](https://codecov.io/gh/nomiddlename/log4js-node) + This is a conversion of the [log4js](https://github.com/stritti/log4js) -framework to work with [node](http://nodejs.org). I've mainly stripped out the browser-specific code and tidied up some of the javascript. +framework to work with [node](http://nodejs.org). I've mainly stripped out the browser-specific code and tidied up some of the javascript. Although it's got a similar name to the Java library [log4j](https://logging.apache.org/log4j/2.x/), thinking that it will behave the same way will only bring you sorrow and confusion. Out of the box it supports the following features: * coloured console logging to stdout or stderr -* replacement of node's console.log functions (optional) * file appender, with configurable log rolling based on file size or date * SMTP appender * GELF appender * Loggly appender * Logstash UDP appender -* logFaces appender +* logFaces (UDP and HTTP) 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.) -## Important changes in 1.0 - -The default appender has been changed from `console` to `stdout` - this alleviates a memory problem that happens when logging using console. If you're using log4js in a browser (via browserify), then you'll probably need to explicitly configure log4js to use the console appender now (unless browserify handles process.stdout). - -I'm also trying to move away from `vows` for the tests, and use `tape` instead. New tests should be added to `test/tape`, not the vows ones. - -log4js also no longer supports node versions below 0.12.x. - -NOTE: from log4js 0.5 onwards you'll need to explicitly enable replacement of node's console.log functions. Do this either by calling `log4js.replaceConsole()` or configuring with an object or json file like this: - -```javascript -{ - appenders: [ - { type: "console" } - ], - replaceConsole: true -} -``` - ## installation ```bash npm install log4js ``` - ## usage Minimalist version: @@ -60,14 +41,13 @@ By default, log4js outputs to stdout with the coloured layout (thanks to [masylu ``` See example.js for a full example, but here's a snippet (also in fromreadme.js): ```javascript -var log4js = require('log4js'); -//console log is loaded by default, so you won't normally need to do this -//log4js.loadAppender('console'); -log4js.loadAppender('file'); -//log4js.addAppender(log4js.appenders.console()); -log4js.addAppender(log4js.appenders.file('logs/cheese.log'), 'cheese'); +const log4js = require('log4js'); +log4js.configure({ + appenders: { cheese: { type: 'file', filename: 'cheese.log' } }, + categories: { default: { appenders: ['cheese'], level: 'error' } } +}); -var logger = log4js.getLogger('cheese'); +const logger = log4js.getLogger('cheese'); logger.setLevel('ERROR'); logger.trace('Entering cheese testing'); @@ -82,16 +62,6 @@ Output: [2010-01-17 11:43:37.987] [ERROR] cheese - Cheese is too ripe! [2010-01-17 11:43:37.990] [FATAL] cheese - Cheese was breeding ground for listeria. ``` -The first 5 lines of the code above could also be written as: -```javascript -var log4js = require('log4js'); -log4js.configure({ - appenders: [ - { type: 'console' }, - { type: 'file', filename: 'logs/cheese.log', category: 'cheese' } - ] -}); -``` ## configuration diff --git a/examples/example.js b/examples/example.js index 8879a23..74070c7 100644 --- a/examples/example.js +++ b/examples/example.js @@ -1,58 +1,47 @@ -"use strict"; -var log4js = require('../lib/log4js'); -//log the cheese logger messages to a file, and the console ones as well. +'use strict'; + +const log4js = require('../lib/log4js'); +// log the cheese logger messages to a file, and the console ones as well. log4js.configure({ - appenders: [ - { - type: "file", - filename: "cheese.log", - category: [ 'cheese','console' ] - }, - { - type: "console" - } - ], - replaceConsole: true + appenders: { + cheeseLogs: { type: 'file', filename: 'cheese.log' }, + console: { type: 'console' } + }, + categories: { + cheese: { appenders: ['cheeseLogs'], level: 'error' }, + another: { appenders: ['console'], level: 'trace' }, + default: { appenders: ['console', 'cheeseLogs'], level: 'trace' } + } }); -//to add an appender programmatically, and without clearing other appenders -//loadAppender is only necessary if you haven't already configured an appender of this type -log4js.loadAppender('file'); -log4js.addAppender(log4js.appenders.file('pants.log'), 'pants'); -//a custom logger outside of the log4js/lib/appenders directory can be accessed like so -//log4js.loadAppender('what/you/would/put/in/require'); -//log4js.addAppender(log4js.appenders['what/you/would/put/in/require'](args)); -//or through configure as: -//log4js.configure({ -// appenders: [ { type: 'what/you/would/put/in/require', otherArgs: 'blah' } ] -//}); +// a custom logger outside of the log4js/lib/appenders directory can be accessed like so +// log4js.configure({ +// appenders: { outside: { type: 'what/you/would/put/in/require', otherArgs: 'blah' } } +// ... +// }); -var logger = log4js.getLogger('cheese'); -//only errors and above get logged. -//you can also set this log level in the config object -//via the levels field. -logger.setLevel('ERROR'); +const logger = log4js.getLogger('cheese'); +// only errors and above get logged. +const otherLogger = log4js.getLogger(); -//console logging methods have been replaced with log4js ones. -//so this will get coloured output on console, and appear in cheese.log -console.error("AAArgh! Something went wrong", { some: "otherObject", useful_for: "debug purposes" }); -console.log("This should appear as info output"); +// this will get coloured output on console, and appear in cheese.log +otherLogger.error('AAArgh! Something went wrong', { some: 'otherObject', useful_for: 'debug purposes' }); +otherLogger.log('This should appear as info output'); -//these will not appear (logging level beneath error) +// these will not appear (logging level beneath error) logger.trace('Entering cheese testing'); logger.debug('Got cheese.'); logger.info('Cheese is Gouda.'); logger.log('Something funny about cheese.'); logger.warn('Cheese is quite smelly.'); -//these end up on the console and in cheese.log -logger.error('Cheese %s is too ripe!', "gouda"); +// these end up only in cheese.log +logger.error('Cheese %s is too ripe!', 'gouda'); logger.fatal('Cheese was breeding ground for listeria.'); -//these don't end up in cheese.log, but will appear on the console -var anotherLogger = log4js.getLogger('another'); -anotherLogger.debug("Just checking"); +// these don't end up in cheese.log, but will appear on the console +const anotherLogger = log4js.getLogger('another'); +anotherLogger.debug('Just checking'); -//one for pants.log -//will also go to console, since that's configured for all categories -var pantsLog = log4js.getLogger('pants'); -pantsLog.debug("Something for pants"); +// will also go to console and cheese.log, since that's configured for all categories +const pantsLog = log4js.getLogger('pants'); +pantsLog.debug('Something for pants'); diff --git a/examples/flush-on-exit.js b/examples/flush-on-exit.js index 19c661c..c4c5520 100644 --- a/examples/flush-on-exit.js +++ b/examples/flush-on-exit.js @@ -2,26 +2,27 @@ * run this, then "ab -c 10 -n 100 localhost:4444/" to test (in * another shell) */ -var log4js = require('../lib/log4js'); +const log4js = require('../lib/log4js'); + log4js.configure({ - appenders: [ - { type: 'file', filename: 'cheese.log', category: 'cheese' }, - { type: 'console'} - ] + appenders: { + cheese: { type: 'file', filename: 'cheese.log' } + }, + categories: { + default: { appenders: ['cheese'], level: 'debug' } + } }); -var logger = log4js.getLogger('cheese'); -logger.setLevel('INFO'); +const logger = log4js.getLogger('cheese'); +const http = require('http'); -var http=require('http'); - -var server = http.createServer(function(request, response){ - response.writeHead(200, {'Content-Type': 'text/plain'}); - var rd = Math.random() * 50; - logger.info("hello " + rd); - response.write('hello '); - if (Math.floor(rd) == 30){ - log4js.shutdown(function() { process.exit(1); }); - } - response.end(); +http.createServer((request, response) => { + response.writeHead(200, { 'Content-Type': 'text/plain' }); + const rd = Math.random() * 50; + logger.info(`hello ${rd}`); + response.write('hello '); + if (Math.floor(rd) === 30) { + log4js.shutdown(() => { process.exit(1); }); + } + response.end(); }).listen(4444); diff --git a/examples/fromreadme.js b/examples/fromreadme.js index 8d837f4..670e988 100644 --- a/examples/fromreadme.js +++ b/examples/fromreadme.js @@ -1,14 +1,12 @@ -//remember to change the require to just 'log4js' if you've npm install'ed it -var log4js = require('../lib/log4js'); -//by default the console appender is loaded -//log4js.loadAppender('console'); -//you'd only need to add the console appender if you -//had previously called log4js.clearAppenders(); -//log4js.addAppender(log4js.appenders.console()); -log4js.loadAppender('file'); -log4js.addAppender(log4js.appenders.file('cheese.log'), 'cheese'); +// remember to change the require to just 'log4js' if you've npm install'ed it +const log4js = require('../lib/log4js'); -var logger = log4js.getLogger('cheese'); +log4js.configure({ + appenders: { cheese: { type: 'file', filename: 'cheese.log' } }, + categories: { default: { appenders: ['cheese'], level: 'error' } } +}); + +const logger = log4js.getLogger('cheese'); logger.setLevel('ERROR'); logger.trace('Entering cheese testing');