8000 fix(docs): updating readme, and a couple of examples · commontime/log4js-node@1416087 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1416087

Browse files
author
Gareth Jones
committed
fix(docs): updating readme, and a couple of examples
1 parent cb142eb commit 1416087

File tree

4 files changed

+70
-112
lines changed

4 files changed

+70
-112
lines changed

README.md

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,32 @@
11
# log4js-node [![Build Status](https://secure.travis-ci.org/nomiddlename/log4js-node.png?branch=master)](http://travis-ci.org/nomiddlename/log4js-node)
22

33
[![NPM](https://nodei.co/npm/log4js.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/log4js/)
4+
[![codecov](https://codecov.io/gh/nomiddlename/log4js-node/branch/master/graph/badge.svg)](https://codecov.io/gh/nomiddlename/log4js-node)
5+
46

57
This is a conversion of the [log4js](https://github.com/stritti/log4js)
6-
framework to work with [node](http://nodejs.org). I've mainly stripped out the browser-specific code and tidied up some of the javascript.
8+
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.
79

810
Out of the box it supports the following features:
911

1012
* coloured console logging to stdout or stderr
11-
* replacement of node's console.log functions (optional)
1213
* file appender, with configurable log rolling based on file size or date
1314
* SMTP appender
1415
* GELF appender
1516
* Loggly appender
1617
* Logstash UDP appender
17-
* logFaces appender
18+
* logFaces (UDP and HTTP) appender
1819
* multiprocess appender (useful when you've got worker processes)
1920
* a logger for connect/express servers
2021
* configurable log message layout/patterns
2122
* different log levels for different log categories (make some parts of your app log as DEBUG, others only ERRORS, etc.)
2223

23-
## Important changes in 1.0
24-
25-
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).
26-
27-
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.
28-
29-
log4js also no longer supports node versions below 0.12.x.
30-
31-
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:
32-
33-
```javascript
34-
{
35-
appenders: [
36-
{ type: "console" }
37-
],
38-
replaceConsole: true
39-
}
40-
```
41-
4224
## installation
4325

4426
```bash
4527
npm install log4js
4628
```
4729

48-
4930
## usage
5031

5132
Minimalist version:
@@ -60,14 +41,13 @@ By default, log4js outputs to stdout with the coloured layout (thanks to [masylu
6041
```
6142
See example.js for a full example, but here's a snippet (also in fromreadme.js):
6243
```javascript
63-
var log4js = require('log4js');
64-
//console log is loaded by default, so you won't normally need to do this
65-
//log4js.loadAppender('console');
66-
log4js.loadAppender('file');
67-
//log4js.addAppender(log4js.appenders.console());
68-
log4js.addAppender(log4js.appenders.file('logs/cheese.log'), 'cheese');
44+
const log4js = require('log4js');
45+
log4js.configure({
46+
appenders: { cheese: { type: 'file', filename: 'cheese.log' } },
47+
categories: { default: { appenders: ['cheese'], level: 'error' } }
48+
});
6949

70-
var logger = log4js.getLogger('cheese');
50+
const logger = log4js.getLogger('cheese');
7151
logger.setLevel('ERROR');
7252

7353
logger.trace('Entering cheese testing');
@@ -82,16 +62,6 @@ Output:
8262
[2010-01-17 11:43:37.987] [ERROR] cheese - Cheese is too ripe!
8363
[2010-01-17 11:43:37.990] [FATAL] cheese - Cheese was breeding ground for listeria.
8464
```
85-
The first 5 lines of the code above could also be written as:
86-
```javascript
87-
var log4js = require('log4js');
88-
log4js.configure({
89-
appenders: [
90-
{ type: 'console' },
91-
{ type: 'file', filename: 'logs/cheese.log', category: 'cheese' }
92-
]
93-
});
94-
```
9565

9666
## configuration
9767

examples/example.js

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,47 @@
1-
"use strict";
2-
var log4js = require('../lib/log4js');
3-
//log the cheese logger messages to a file, and the console ones as well.
1+
'use strict';
2+
3+
const log4js = require('../lib/log4js');
4+
// log the cheese logger messages to a file, and the console ones as well.
45
log4js.configure({
5-
appenders: [
6-
{
7-
type: "file",
8-
filename: "cheese.log",
9-
category: [ 'cheese','console' ]
10-
},
11-
{
12-
type: "console"
13-
}
14-
],
15-
replaceConsole: true
6+
appenders: {
7+
cheeseLogs: { type: 'file', filename: 'cheese.log' },
8+
console: { type: 'console' }
9+
},
10+
categories: {
11+
cheese: { appenders: ['cheeseLogs'], level: 'error' },
12+
another: { appenders: ['console'], level: 'trace' },
13+
default: { appenders: ['console', 'cheeseLogs'], level: 'trace' }
14+
}
1615
});
1716

18-
//to add an appender programmatically, and without clearing other appenders
19-
//loadAppender is only necessary if you haven't already configured an appender of this type
20-
log4js.loadAppender('file');
21-
log4js.addAppender(log4js.appenders.file('pants.log'), 'pants');
22-
//a custom logger outside of the log4js/lib/appenders directory can be accessed like so
23-
//log4js.loadAppender('what/you/would/put/in/require');
24-
//log4js.addAppender(log4js.appenders['what/you/would/put/in/require'](args));
25-
//or through configure as:
26-
//log4js.configure({
27-
// appenders: [ { type: 'what/you/would/put/in/require', otherArgs: 'blah' } ]
28-
//});
17+
// a custom logger outside of the log4js/lib/appenders directory can be accessed like so
18+
// log4js.configure({
19+
// appenders: { outside: { type: 'what/you/would/put/in/require', otherArgs: 'blah' } }
20+
// ...
21+
// });
2922

30-
var logger = log4js.getLogger('cheese');
31-
//only errors and above get logged.
32-
//you can also set this log level in the config object
33-
//via the levels field.
34-
logger.setLevel('ERROR');
23+
const logger = log4js.getLogger('cheese');
24+
// only errors and above get logged.
25+
const otherLogger = log4js.getLogger();
3526

36-
//console logging methods have been replaced with log4js ones.
37-
//so this will get coloured output on console, and appear in cheese.log
38-
console.error("AAArgh! Something went wrong", { some: "otherObject", useful_for: "debug purposes" });
39-
console.log("This should appear as info output");
27+
// this will get coloured output on console, and appear in cheese.log
28+
otherLogger.error('AAArgh! Something went wrong', { some: 'otherObject', useful_for: 'debug purposes' });
29+
otherLogger.log('This should appear as info output');
4030

41-
//these will not appear (logging level beneath error)
31+
// these will not appear (logging level beneath error)
4232
logger.trace('Entering cheese testing');
4333
logger.debug('Got cheese.');
4434
logger.info('Cheese is Gouda.');
4535
logger.log('Something funny about cheese.');
4636
logger.warn('Cheese is quite smelly.');
47-
//these end up on the console and in cheese.log
48-
logger.error('Cheese %s is too ripe!', "gouda");
37+
// these end up only in cheese.log
38+
logger.error('Cheese %s is too ripe!', 'gouda');
4939
logger.fatal('Cheese was breeding ground for listeria.');
5040

51-
//these don't end up in cheese.log, but will appear on the console
52-
var anotherLogger = log4js.getLogger('another');
53-
anotherLogger.debug("Just checking");
41+
// these don't end up in cheese.log, but will appear on the console
42+
const anotherLogger = log4js.getLogger('another');
43+
anotherLogger.debug('Just checking');
5444

55-
//one for pants.log
56-
//will also go to console, since that's configured for all categories
57-
var pantsLog = log4js.getLogger('pants');
58-
pantsLog.debug("Something for pants");
45+
// will also go to console and cheese.log, since that's configured for all categories
46+
const pantsLog = log4js.getLogger('pants');
47+
pantsLog.debug('Something for pants');

examples/flush-on-exit.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,27 @@
22
* run this, then "ab -c 10 -n 100 localhost:4444/" to test (in
33
* another shell)
44
*/
5-
var log4js = require('../lib/log4js');
5+
const log4js = require('../lib/log4js');
6+
67
log4js.configure({
7-
appenders: [
8-
{ type: 'file', filename: 'cheese.log', category: 'cheese' },
9-
{ type: 'console'}
10-
]
8+
appenders: {
9+
cheese: { type: 'file', filename: 'cheese.log' }
10+
},
11+
categories: {
12+
default: { appenders: ['cheese'], level: 'debug' }
13+
}
1114
});
1215

13-
var logger = log4js.getLogger('cheese');
14-
logger.setLevel('INFO');
15-
16-
var http=require('http');
16+
const logger = log4js.getLogger('cheese');
17+
const http = require('http');
1718

18-
var server = http.createServer(function(request, response){
19-
response.writeHead(200, {'Content-Type': 'text/plain'});
20-
var rd = Math.random() * 50;
21-
logger.info("hello " + rd);
22-
response.write('hello ');
23-
if (Math.floor(rd) == 30){
24-
log4js.shutdown(function() { process.exit(1); });
25-
}
26-
response.end();
19+
http.createServer((request, response) => {
20+
response.writeHead(200, { 'Content-Type': 'text/plain' });
21+
const rd = Math.random() * 50;
22+
logger.info(`hello ${rd}`);
23+
response.write('hello ');
24+
if (Math.floor(rd) === 30) {
25+
log4js.shutdown(() => { process.exit(1); });
26+
}
27+
response.end();
2728
}).listen(4444);

examples/fromreadme.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
//remember to change the require to just 'log4js' if you've npm install'ed it
2-
var log4js = require('../lib/log4js');
3-
//by default the console appender is loaded
4-
//log4js.loadAppender('console');
5-
//you'd only need to add the console appender if you
6-
//had previously called log4js.clearAppenders();
7-
//log4js.addAppender(log4js.appenders.console());
8-
log4js.loadAppender('file');
9-
log4js.addAppender(log4js.appenders.file('cheese.log'), 'cheese');
1+
// remember to change the require to just 'log4js' if you've npm install'ed it
2+
const log4js = require('../lib/log4js');
103

11-
var logger = log4js.getLogger('cheese');
4+
log4js.configure({
5+
appenders: { cheese: { type: 'file', filename: 'cheese.log' } },
6+
categories: { default: { appenders: ['cheese'], level: 'error' } }
7+
});
8+
9+
const logger = log4js.getLogger('cheese');
1210
logger.setLevel('ERROR');
1311

1412
logger.trace('Entering cheese testing');

0 commit comments

Comments
 (0)
0