Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加http日志和调试日志功能,另给404页面增加状态码 #13

Merged
merged 2 commits into from
Nov 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ npm-debug.log
.idea
*.iml
config.js
.DS_Store
.DS_Store
/logs
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ akismet 可以帮助你拒绝绝大部分的 spam 评论,建议开启。你需
1. 更新mongoskin版本为1.3.20,不然会报错,因为mongodb的接口已经修改
2. 修改`db.ObjectID.createFromHexString`为`mongoskin.helper.toObjectID`以适应新版本

####2014-11-14

1. 添加log4js调试模块
2. 添加http请求记录到文件的模块
3. 给404页面添加404状态码

##License

Expand Down
6 changes: 4 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
*/

var express = require('express');
var http = require('http');
var routes = require('./routes');
var config = require('./config.js').config;
var partials = require('express-partials');
var util = require('./lib/util');

var app = express();
var static_dir = __dirname + '/public';
Expand All @@ -20,7 +20,9 @@ app.configure(function () {
app.use(express.compress());
app.use(partials());
app.use(express.favicon(__dirname + '/public/favicon.ico'));
app.use(express.logger('dev'));
//app.use(express.logger('dev'));
// 采用morgan
app.use(util.httplogger);
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ secret: config.session_secret }));
Expand Down
87 changes: 87 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
var crypto = require('crypto');
var log4js = require('log4js');
var morgan = require('morgan');
var fs = require('fs');

/**
* Created with IntelliJ IDEA.
Expand Down Expand Up @@ -85,3 +88,87 @@ exports.decrypt = function decrypt(str, secret) {
dec += decipher.final('utf8');
return dec;
};


/**
* log4js调试
*/

/**
* 新建日志目录
*/
try {
require('fs').mkdirSync(__dirname+'/../logs');
} catch (e) {
if (e.code != 'EEXIST') {
console.error("不能创建log目录,错误: ", e);
process.exit(1);
}
}

// 定义三种logLevelFilter日志类型
var logConfig = {
appenders: [
{type: 'console'},
{
type: 'logLevelFilter',
level: 'ERROR',
category: 'error',
appender:
{
type: 'file',
filename: __dirname + '/../logs/error.log'
}
},
{
type: 'logLevelFilter',
level: 'INFO',
category: 'info',
appender:
{
type: 'file',
filename: __dirname + '/../logs/info.log'
}
},
{
type: 'logLevelFilter',
level: 'DEBUG',
category: 'debug',
appender:
{
type: 'file',
filename: __dirname + '/../logs/debug.log'
}
}
]
};

log4js.configure(logConfig);
/**
* info等级的调试函数,调试信息输出到console同时保存到相应文件中
* @returns 返回log4js对象
*/
exports.infologger = log4js.getLogger('info');
/**
* error等级的调试函数,调试信息输出到console同时保存到相应文件中
* @returns 返回log4js对象
*/
exports.errorlogger = log4js.getLogger('error');
/**
* debug等级的调试函数,调试信息输出到console同时保存到相应文件中
* @returns 返回log4js对象
*/
exports.debuglogger = log4js.getLogger('debug');


// 创建一个附加模式的写入流
var accessLogStream = fs.createWriteStream(__dirname + '/../logs/access.log', {flags: 'a'});
/**
* http请求记录器,写入到access.log文件中,只记录错误和跳转响应
*/
exports.httplogger = morgan('short', {
stream: accessLogStream,
skip: function(req, res){ // 只记录错误和跳转响应
return res.statusCode < 300 || res.statusCode == 304;
}
});
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
"npm": "1.1.43"
},
"dependencies": {
"express": "3.4.0",
"data2xml": "0.9.0",
"mongoskin": "1.3.20",
"marked": "0.2.9",
"akismet": "0.0.6",
"gravatar": "1.0.6",
"data2xml": "0.9.0",
"ejs": "0.8.4",
"express": "3.4.0",
"express-partials": "~0.1.1",
"gravatar": "1.0.6",
"log4js": "^0.6.21",
"marked": "0.2.9",
"moment": "~2.2.1",
"express-partials": "~0.1.1"
"mongoskin": "1.3.20",
"morgan": "^1.5.0"
}
}
1 change: 1 addition & 0 deletions routes/blog.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ exports.archives = function (req, res) {
// URL: /404
exports.pageNotFound = function (req, res) {
console.log('404 handler, URL' + req.originalUrl);
res.status(404);
res.render('theme/' + config.theme + '/404', {
layout: false,
status: 404,
Expand Down