Express.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

 

修改时间 2019-07-02

真诚赞赏,手留余香
赞赏
随机推荐
Node.js 断言模块 Assert 模块
Android Studio卡在refreshing gradle project的原因和快速解决办法
DedeCMS 如何使用文章列表序号
Apache ModSecurity 基础介绍
一个模板解析函数的bug
Adobe Premiere 做慢动作视频“升格视频”
npm warn package.json @1.0.0 no repository field
Javascript和jQuery获取checkbox的值
MAC重装PHP
Serverless 笔记111