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

loggercc

v1.3.2

Published

简单、高性能的node.js日志显示、存档工具

Downloads

23

Readme

loggercc

集成日志显示、存档、作用域、debug多功能一体,开箱即用的日志模块。

特性

  • 没有日志等级概念,可以灵活的配置每个日志类型

  • 支持日志作用域,默认全局匹配,通过环境变量logger限定作用域范围

  • 可定制存储格式,默认使用text格式日志

  • 日志定期存盘,默认间隔3s

  • 日志拆分,文件按天保存到logger目录下

  • cluster多进程日志,建议通过agent运行

  • 动态切换日志配置,如在生产环境下临时查看log

  • 自定义cli显示内容格式及文件保存格式

  • 显示格式化的彩色文本、图标日志

  • 可扩展自定义日志类型

  • 配置简单,易定制

  • 轻量级、高性能,生产环境与测试环境代码分离,剔除所有与生产环境无关联的代码,使用最优惰性配置

示例

const logger = require('loggercc');

if (process.env.NODE === 'production') {

   logger.production()

}

logger.log('hellow log')

logger.success('hellow success')

logger.warn({ a: 1 })

logger.error(new Error(''))

logger.success('888')

Install

npm install loggercc

开发环境配置

开发环境下默认仅显示日志,不存盘。通过logger.development()配置覆盖默认行为

logger.development(options)

  • options Object 自定义配置选项,可选

    • $type Object 类型配置选项,$type对应类型名称

      • show Boolean, Function 在cli中显示log,当值为函数时表示使用自定义显示格式

      • save Boolean, Function 是否保存至文件,当值为函数时表示使用自定义存储格式,默认为false

      • filename String 保存文件名

      • action Function 覆盖默认logger的处理逻辑,或为自定义logger添加处理逻辑(!谨慎操作)

默认配置
logger.development({
   "log": {
      "show": true,
      "save": false,
      "filename": "log.log"
   },
   "success": {
      "show": true,
      "save": false,
      "filename": "success.log"
   },
   "warn": {
      "show": true,
      "save": false,
      "filename": "warning.log"
   },
   "error": {
      "show": true,
      "save": false,
      "filename": "error.log"
   }
})

生产环境配置

生产环境下默认不显示日志,也不存盘。通过logger.production()配置开启日志存盘功能。

logger.production(options)

  • options Object 自定义配置选项,可选

    • $type Object 类型配置选项,$type对应类型名称

      • save Boolean, Function 是否保存至文件,当值为函数时表示使用自定义存储格式,默认为false

      • filename String 保存文件名

      • action Function 覆盖默认logger的处理逻辑,或为自定义logger添加处理逻辑(!谨慎操作)

默认配置
logger.production({
   "log": {
      "save": false,
      "filename": "log.log"
   },
   "success": {
      "save": false,
      "filename": "success.log"
   },
   "warn": {
      "save": true,
      "filename": "warning.log"
   },
   "error": {
      "save": true,
      "filename": "error.log"
   },
})

自定义显示和存储格式

在loggercc中可以很方便的定制日志显示格式和存储格式,在自定义配置函数中绑定了一些常用日志属性,帮助快速合成日志格式。

可用日志属性如下:

  • this.getDate() Function 生成日志时间函数

  • this.parameter(argv) Function argv参数处理

  • this.pid String 进程id

  • this.hostname String 主机名称

  • this.type String 日志类型

logger.development({
   "success": {
      "save": true,
      show(argv) {
         // 显示格式
         console.log(`\x1b[32m😊  ${this.type} [${this.getDate()}] \x1b[39m`, ...argv)
      }
   },
   "warn": {
      show(argv) {
         console.log(`\x1b[32m🐷  ${this.type} [${this.getDate()}] \x1b[39m`, ...argv)
      },
      save(argv) {
         // 保存为JSON格式
         return JSON.stringify({
            date: this.getDate(),
            type: this.type,
            pid: this.pid,
            hostname: this.hostname,
            argv
         })
      }
   },
   "error": {
      save(argv) {
         // 保存为TEXT格式
         return `[${this.getDate()}] ${this.type} ${this.pid} ${this.hostname} ${argv}`
      }
   }
})

日志作用域

日志作用域的使用方法与debug模块类似,区别如下:

  • debug默认不显示log,loggercc默认显示log,通过logger.production()切换到生产环境后关闭日志。

  • debug使用“debug”作为环境变量名,loggercc使用“logger”作为环境变量名。

  • debug的环境变量值支持模糊匹配,loggercc不支持模糊匹配,但支持精确匹配多个作用域。

const app = require('loggercc')('app');

app.success('ok')