• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

dominhhai/koa-log4js: log4js-node supports Koa-middleware

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

dominhhai/koa-log4js

开源软件地址:

https://github.com/dominhhai/koa-log4js

开源编程语言:

JavaScript 100.0%

开源软件介绍:

koa-log4js

A wrapper for log4js-node which support Koa logger middleware. Log message is forked from Express (Connect) logger file.

Note

This branch is use to Koa v2.x. To use Koa v0.x & v1.x, please check the master branch.

Installation

for koa v0.x & v1.x

$ npm i --save koa-log4@1

for koa v2.x

$ npm i --save koa-log4@2

The default logger is for koa v2.x

$ npm i --save koa-log4

Usage

Config koa-log4js is same as the original log4js-node.

Normal log4js way

This way is same as the original log4js-node.

const log4js = require('koa-log4')

const log = log4js.getLogger('index')
log.info('index do some awesome things')

Koa-middleware way

Similar to use Express (Connect) logger middleware.

const log4js = require('koa-log4')
app.use(log4js.koaLogger(log4js.getLogger("http"), { level: 'auto' }))

There are more configuration options:

const log4js = require("koa-log4")
app.use(log4js.koaLogger(
    // the logger to log to
    log4js.getLogger("http"),
    // the options object
    {
        // select the level all access logs will be set to, or use "auto" to choose depending on the status code (see next option)
        level: "auto",
        // if `level` is set to "auto" the default rule will map 200-299 to INFO, 300-399 to WARN and 400-599 to ERROR.
        // you can override this behavior by setting your own custom levelMapper.
        // (the example is the default implementation, do not copy unless you want to modify it)
        levelMapper: function(statusCode) {
            if (statusCode >= 400)
                return levels.ERROR
            if (statusCode >= 300)
                return levels.WARN
            return levels.INFO
        }
    }
))

Full Example

Check this repo for full example with Koa v2.

Others

See here for more info.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
cjudd/groovy_koans: A set of Koans to teach the Groovy language发布时间:2022-06-24
下一篇:
cobbler/koan: koan - kickstart over a network发布时间:2022-06-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap