├── .gitignore ├── LICENSE ├── README.md ├── app.js ├── bin └── www ├── config └── log4js.json ├── package.json ├── public └── stylesheets │ └── style.css ├── routes ├── index.js └── users.js └── views ├── error.pug ├── index.pug └── layout.pug /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Deployed apps should consider commenting this line out: 24 | # see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git 25 | node_modules 26 | package-lock.json 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Gareth Jones 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | log4js-example 2 | ============== 3 | 4 | A full express application that demonstrates how to configure log4js for the most common use-case. 5 | 6 | To run: 7 | ``` 8 | npm install 9 | npm start 10 | ``` 11 | 12 | Log config 13 | ========== 14 | Config file is in `config/log4js.json`. It defines three appenders that are children of the clustered appender. The clustered appender makes sure that only the master process writes to the log files, otherwise having multiple processes try to write at the same time will cause problems. Three log files are defined: `log/app.log` gets all the log messages and is configured to rotate when the file gets to 10Mb in size, keeping 3 backups of the file; `log/errors.log` uses the logLevelFilter to only get ERROR messages; `log/access.log` contains only the http request logs, using the connect-logger, and is configured to rotate every day. 15 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | var log4js = require('log4js'); 2 | var express = require('express'); 3 | var path = require('path'); 4 | var favicon = require('static-favicon'); 5 | var cookieParser = require('cookie-parser'); 6 | var bodyParser = require('body-parser'); 7 | 8 | //We won't need this. 9 | //var logger = require('morgan'); 10 | var log = log4js.getLogger("app"); 11 | 12 | var routes = require('./routes/index'); 13 | var users = require('./routes/users'); 14 | 15 | var app = express(); 16 | 17 | // view engine setup 18 | app.set('views', path.join(__dirname, 'views')); 19 | app.set('view engine', 'pug'); 20 | 21 | app.use(favicon()); 22 | 23 | // replace this with the log4js connect-logger 24 | // app.use(logger('dev')); 25 | app.use(log4js.connectLogger(log4js.getLogger("http"), { level: 'auto' })); 26 | 27 | app.use(bodyParser.json()); 28 | app.use(bodyParser.urlencoded({ extended: false })); 29 | app.use(cookieParser()); 30 | app.use(express.static(path.join(__dirname, 'public'))); 31 | 32 | app.use('/', routes); 33 | app.use('/users', users); 34 | 35 | /// catch 404 and forward to error handler 36 | app.use(function(req, res, next) { 37 | var err = new Error('Not Found'); 38 | err.status = 404; 39 | next(err); 40 | }); 41 | 42 | /// error handlers 43 | 44 | // development error handler 45 | // will print stacktrace 46 | if (app.get('env') === 'development') { 47 | app.use(function(err, req, res, next) { 48 | log.error("Something went wrong:", err); 49 | res.status(err.status || 500); 50 | res.render('error', { 51 | message: err.message, 52 | error: err 53 | }); 54 | }); 55 | } 56 | 57 | // production error handler 58 | // no stacktraces leaked to user 59 | app.use(function(err, req, res, next) { 60 | log.error("Something went wrong:", err); 61 | res.status(err.status || 500); 62 | res.render('error', { 63 | message: err.message, 64 | error: {} 65 | }); 66 | }); 67 | 68 | 69 | module.exports = app; 70 | -------------------------------------------------------------------------------- /bin/www: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | var debug = require('debug')('log4js-example'); 3 | var cluster = require('express-cluster'); 4 | 5 | /** 6 | * make a log directory, just in case it isn't there. 7 | */ 8 | try { 9 | require('fs').mkdirSync('./log'); 10 | } catch (e) { 11 | if (e.code != 'EEXIST') { 12 | console.error("Could not set up log directory, error was: ", e); 13 | process.exit(1); 14 | } 15 | } 16 | 17 | /** 18 | * Initialise log4js first, so we don't miss any log messages 19 | */ 20 | var log4js = require('log4js'); 21 | log4js.configure('./config/log4js.json'); 22 | 23 | var log = log4js.getLogger("startup"); 24 | 25 | //let's start up a cluster 26 | cluster(function() { 27 | var app = require('../app'); 28 | 29 | app.set('port', process.env.PORT || 3000); 30 | 31 | var server = app.listen(app.get('port'), function() { 32 | log.info('Express server listening on port ', server.address().port, " with pid ", process.pid ); 33 | }); 34 | }); 35 | -------------------------------------------------------------------------------- /config/log4js.json: -------------------------------------------------------------------------------- 1 | { 2 | "appenders": { 3 | "access": { 4 | "type": "dateFile", 5 | "filename": "log/access.log", 6 | "pattern": "-yyyy-MM-dd", 7 | "category": "http" 8 | }, 9 | "app": { 10 | "type": "file", 11 | "filename": "log/app.log", 12 | "maxLogSize": 10485760, 13 | "numBackups": 3 14 | }, 15 | "errorFile": { 16 | "type": "file", 17 | "filename": "log/errors.log" 18 | }, 19 | "errors": { 20 | "type": "logLevelFilter", 21 | "level": "ERROR", 22 | "appender": "errorFile" 23 | } 24 | }, 25 | "categories": { 26 | "default": { "appenders": [ "app", "errors" ], "level": "DEBUG" }, 27 | "http": { "appenders": [ "access"], "level": "DEBUG" } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "log4js-example", 3 | "version": "0.0.1", 4 | "private": false, 5 | "scripts": { 6 | "start": "node ./bin/www" 7 | }, 8 | "dependencies": { 9 | "body-parser": "^1.18.3", 10 | "cookie-parser": "^1.4.3", 11 | "debug": "^3.2.6", 12 | "express": "^4.16.4", 13 | "express-cluster": "^0.0.5", 14 | "log4js": "^4.0.2", 15 | "pug": "^2.0.3", 16 | "static-favicon": "^1.0.0" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /public/stylesheets/style.css: -------------------------------------------------------------------------------- 1 | body { 2 | padding: 50px; 3 | font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; 4 | } 5 | 6 | a { 7 | color: #00B7FF; 8 | } -------------------------------------------------------------------------------- /routes/index.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | var log = require('log4js').getLogger("index"); 4 | 5 | /* GET home page. */ 6 | router.get('/', function(req, res) { 7 | log.debug("This is in the index module"); 8 | res.render('index', { title: 'Express' }); 9 | }); 10 | 11 | module.exports = router; 12 | -------------------------------------------------------------------------------- /routes/users.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | var log = require('log4js').getLogger("users"); 4 | 5 | /* GET users listing. */ 6 | router.get('/', function(req, res) { 7 | log.debug("I'm in the users module."); 8 | res.send('respond with a resource'); 9 | }); 10 | 11 | /* GET something that just errors */ 12 | router.get('/broken', function(req, res) { 13 | log.error("Oh noes, something has gone terribly wrong"); 14 | res.error("aargh"); 15 | }); 16 | 17 | module.exports = router; 18 | -------------------------------------------------------------------------------- /views/error.pug: -------------------------------------------------------------------------------- 1 | extends layout 2 | 3 | block content 4 | h1= message 5 | h2= error.status 6 | pre #{error.stack} 7 | -------------------------------------------------------------------------------- /views/index.pug: -------------------------------------------------------------------------------- 1 | extends layout 2 | 3 | block content 4 | h1= title 5 | p Welcome to #{title} 6 | -------------------------------------------------------------------------------- /views/layout.pug: -------------------------------------------------------------------------------- 1 | doctype html 2 | html 3 | head 4 | title= title 5 | link(rel='stylesheet', href='/stylesheets/style.css') 6 | body 7 | block content --------------------------------------------------------------------------------