npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dj-koa-log4js

v1.1.1

Published

到家Koajslog4js 中间件模块

Downloads

2

Readme

dj-koa-log4js

基于log4js的日志打印模块封装,封装了KoaJs的打印日志中间件,和基于PM2的实时切换日志级别的方法。

方法列表

  • initLog 初始化方法,一定要在入口js的最开始的地方调用
  • loggerMid KoaJs的打印日志中间件
  • getLogger 非ctx上下文获取日志实例的方法
  • toggleLog 基于PM2的实时切换日志级别的方法,在TRACE和初始化logger时配置的logLevel切换

log4js的日志级别

  ALL: { value: Number.MIN_VALUE, colour: 'grey' },
  TRACE: { value: 5000, colour: 'blue' },
  DEBUG: { value: 10000, colour: 'cyan' },
  INFO: { value: 20000, colour: 'green' },
  WARN: { value: 30000, colour: 'yellow' },
  ERROR: { value: 40000, colour: 'red' },
  FATAL: { value: 50000, colour: 'magenta' },
  MARK: { value: 9007199254740992, colour: 'grey' }, // 2^53
  OFF: { value: Number.MAX_VALUE, colour: 'grey' }

日志级别从All->OFF 逐级增加,log4js只会输出大于等于当前日志级别的日志

使用方法

初始化方法

const {initLog, loggerMid, getLogger, toggleLog} = require('dj-koa-log4js')
//参数中logLevel 可以不传,
initLog({
    logLevel: 'INFO',// 初始化日志级别
    appName: 'xxx', // 业务名字,日志每一条记录会显示业务线名字
    funLogHead (ctx) { // 可以自定义在输出日志之前插入的内容,例如traceid,uid等等,返回一个数组
        return [ctx.feTraceid || '', head1, head22, ...]
    },
    funLogTail(ctx) {// 可以自定义在输出日志之后插入的内容,例如'over'等等,返回一个数组
        return []
    }

})

使用中间件

app.use(loggerMid)

//后面就可以直接使用ctx.logger来打印日志了
ctx.logger.info('日志内容')

最终输出的日志内容为:

2020-06-15 11:21:58.840 [INFO] 业务名称 - logHead1 logHead2 日志内容 logTail1 logTail2 ...

没有ctx上下文时使用

公共方法没有ctx时使用,全局公用一个logger实例

const logger = getLogger()

// 调用方需要把ctx传进来,打印日志时把ctx作为打印日志的第一个参数
logger.info(ctx, '日志内容')

// 如果没有ctx属性,直接打印,这样最终打印的日志里没有定制的日志头和日志尾信息
logger.info('日志内容')

运行时切换日志级别

PM2启动服务时才支持,需要在入口页面添加

// 在入口js中添加
const logger = getLogger()
process.on('SIGUSR2', (msg) => {
    // log级别在TRACE和初始化logger时配置的logLevel切换
    toggleLog()
})

不启动服务,不修改代码,实时切换日志打印level的方法,实现是toggle的方法,切两次就会恢复默认级别

# 业务名称通过pm2 ls查看

# 切换日志levle为trace级别
pm2 sendSignal SIGUSR2 业务的名称

# 切换日志level为初始级别
pm2 sendSignal SIGUSR2 业务的名称