Express 日志模块
Node.js 教程
收录了这篇文章

安装

npm install --save-dev morgan

使用

var express = require('express');
var app 	= express();

// 设置日志
var fs = require("fs");
var path = require('path');
var morgan = require('morgan');

// create a write stream (in append mode)
var accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'), { flags: 'a' })
// setup the logger
app.use(morgan('short', { stream: accessLogStream }))

 

生成日志

 

 

 

预定义格式

combined
Standard Apache combined log output.
:remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"

common
Standard Apache common log output.
:remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length]

dev
Concise output colored by response status for development use. The :status token will be colored red for server error codes, yellow for client error codes, cyan for redirection codes, and uncolored for all other codes.
:method :url :status :response-time ms - :res[content-length]

short
Shorter than default, also including response time.
:remote-addr :remote-user :method :url HTTP/:http-version :status :res[content-length] - :response-time ms

tiny
The minimal output.
:method :url :status :res[content-length] - :response-time ms

 

更多用法:https://github.com/expressjs/morgan

 

 

下表列出了 Express 3 中间件及其在 Express 4 中的对应组件。Express5 alpha 前阵子也已经出了

 

Express.js 中间件 https://github.com/senchalabs/connect#middleware

 

修改时间 2024-05-29

声明:本站所有文章和图片,如无特殊说明,均为原创发布。商业转载请联系作者获得授权,非商业转载请注明出处。
随机推荐
WordPress 插入文章函数 wp_insert_post()
WordPress 函数 add_option()、get_option() 和 update_option()
WordPress 添加 Favicon 图标的方法
Node.js test 模块
如何使用命令修改 MySQL 数据库名称
JavaScript document 对象
WordPress 密码生成和密码验证
Notifications API