【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)


相关推荐
EndingCoder20 小时前
测试 Next.js 应用:工具与策略
开发语言·前端·javascript·log4j·测试·全栈·next.js
墨鸦_Cormorant2 天前
MQTT(轻量级消息中间件)基本使用指南
mqtt·中间件·消息中间件
一枚小小程序员哈2 天前
基于Vue + Node能源采购系统的设计与实现/基于express的能源管理系统#node.js
vue.js·node.js·express
一枚小小程序员哈3 天前
基于Vue的个人博客网站的设计与实现/基于node.js的博客系统的设计与实现#express框架、vscode
vue.js·node.js·express
zru_96024 天前
Spring Boot 单元测试:@SpyBean 使用教程
spring boot·单元测试·log4j
Python私教4 天前
从“Hello World”到“高并发中间件”:Go 语言 2025 系统学习路线图
学习·中间件·golang
UrSpecial4 天前
进程间通信:消息队列
中间件
EndingCoder7 天前
Next.js 中间件:自定义请求处理
开发语言·前端·javascript·react.js·中间件·全栈·next.js
十五年专注C++开发7 天前
通信中间件 Fast DDS(一) :编译、安装和测试
linux·c++·windows·中间件·cmake·vcpkg
在未来等你9 天前
RabbitMQ面试精讲 Day 17:消费者调优与并发消费
中间件·面试·消息队列·rabbitmq