【Express】创建输出日志中间件(log4js)

js 复制代码
const log4js = require('log4js')
//日志输出 控制台输出,文件也要输出
log4js.configure({
    appenders: {
        // 控制台输出
        out: {
            type: 'stdout',
            layout: {
                type: 'colored'
            }
        },
        //文件输出
        file: {
            filename: 'logs/server.log',
            type: 'file'
        }
    },
    categories: {
        default: {
            appenders: ['out', 'file'],
            level: 'debug'
        }
    }
})
const logger = log4js.getLogger('default')
const LoggerMiddleware = (req, res, next) => {
    logger.debug(`[${req.method}] -- ${req.url}`)
    next()
}

module.exports = LoggerMiddleware
js 复制代码
const LoggerMiddleware = require('./middleware/logger.js')
app.use(LoggerMiddleware)


相关推荐
jc062014 小时前
4.1-中间件之Redis
数据库·redis·中间件
山楂树下懒猴子1 天前
ChatAI项目-ChatGPT-SDK组件工程
人工智能·chatgpt·junit·https·log4j·intellij-idea·mybatis
Mr_Xuhhh1 天前
gtest全局套件的测试使用
log4j
索迪迈科技1 天前
Protobuf 新版“调试表示为什么有链接?为什么会打码?我该怎么改代码?
java·log4j·apache
lingggggaaaa1 天前
小迪安全v2023学习笔记(八十讲)—— 中间件安全&WPS分析&Weblogic&Jenkins&Jetty&CVE
笔记·学习·安全·web安全·网络安全·中间件·wps
MySGDLife1 天前
中间件八股
中间件
凯子坚持 c2 天前
Redis核心通用命令深度解析:结合C++ redis-plus-plus 实战指南
c++·redis·log4j
Mr_hwt_1232 天前
基于MyCat 中间件实现mysql集群读写分离与从库负载均衡教程(详细案例教程)
数据库·mysql·中间件·mysql集群
echoyu.2 天前
消息队列-初识kafka
java·分布式·后端·spring cloud·中间件·架构·kafka
Ray Song2 天前
【FastDDS】XML profiles
xml·中间件·自动驾驶·dds·fastdds