当前位置:网站首页>express日志模块Morgan
express日志模块Morgan
2022-08-05 12:17:00 【帮助文档】
Morgan简单的express模块
一、Morgan有什么特点
由express官方团队维护
- morgan配置非常简单
- 支持自定义日志格式
- 支持日志压缩
- 支持分文件存储
二、安装
npm install morgan --save
三、使用入门
const express=require('express');
const morgan=require('morgan');
const port=3721;
const app=express();
app.use(morgan());
app.get('/',(req,res)=>{
res.end('hello world');
});
app.listen(port,()=>{
console.log(`server listen ${port}`);
});
四、优化配置
- 配置日志格式
(1) :url 请求url,req.originalUrl或者req.url
(2) :total-time[digits] 处理当期请求所用时间
(3) :status 处理返回的状态码
(4) :date[format] 处理的时间 format支持iso(2000-10-10T13:55:36.000Z) clf(10/Oct/2000:13:55:36 +0000)web(Tue, 10 Oct 2000 13:55:36 GMT)
(5) :remote-addr 访问者ip - 筛选只输出部分日志
skip:function(req,res){
retrun res.statusCode<400;
} - 保存到文件
const express=require('express');
const morgan=require('morgan');
const rfs = require('rotating-file-stream') // version 2.x
const fs=require('fs');
const path=require('path');
const port=3721;
const app=express();
var accessLogStream = rfs.createStream('access.log', {
interval: '1d', // rotate daily
path: path.join(__dirname, 'log')
})
app.use(morgan('[:date[iso]] [:remote-addr] :url - :status - :total-time[2] ',{
skip:function(req,res){
return res.statusCode<400;
},
stream: accessLogStream
}));
app.get('/',(req,res)=>{
res.status(400).end('hello world');
});
app.listen(port,()=>{
console.log(`server listen ${port}`);
});
边栏推荐
- NFT card game system dapp develops NFT chain game technology
- Simple clock animation
- 食品饮料行业B2B商城系统:加速行业数字化转型,提升B2B平台交易效率
- For high performance, large scale model training, this combination "career"
- [Supply Chain·Case] What did the top ten retailers in the world do under the influence of the epidemic?
- 795. Prefix and
- 2022华数杯C:插层熔喷非织造材料的性能控制研究
- 一次多表关联顺序的慢查询——TiDB 关联特性
- The fourth SQL general command (DML)
- treeselect常用功能记录(有清除选项的回调函数)
猜你喜欢
二:OpenCV图片叠加逻辑运算
该不该将单体架构迁移到微服务?
Regular expressions in action
我和 TiDB 的故事 | 学tidb半年,社区治好了我的精神内耗
NFT card game system dapp develops NFT chain game technology
内存问题难定位,那是因为你没用ASAN
STM32H743IIT6学习笔记02——USART
hello world、hello 计科人
MySQL之InnoDB线程模型
Huawei Analysis & Intermodal Activities to Help You Improve Overall Game Payments
随机推荐
Sentinel introduction and use
How to use the timetable applet
Face the summary - club resort - 6 years
MySQL check constraint
我和 TiDB 的故事 | 学tidb半年,社区治好了我的精神内耗
MySQL之InnoDB存储结构
MySQL之InnoDB和MyISAM区别
2022.08.04_每日一题
2022.08.03_每日一题
2022.08.02_每日一题
Memory problems difficult to locate, it is because you do not use ASAN
Shang Silicon Valley-JVM-Performance Monitoring and Tuning (P302~P381)
对于聚合物聚乙二醇PEG大家了解多少了?以及在生活中的应用
The Open-Closed Principle (OCP) of the Seven Principles of Software Design
Monthly observation of Internet medical field in June 2022
Cesium.js点线面绘制
银行交易系统怎么保证数据交易强一致性?通过数据库组件?怎么保证高并发下数据库交易数据正常一致性?
2022.08.03_每日一题
[Supply Chain·Case] What did the top ten retailers in the world do under the influence of the epidemic?
安装tldr