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

logtube

v2.4.0

Published

Logtube Node.js SDK

Downloads

7

Readme

logtube-node

Logtube Node.js SDK

设计

每一条日志都包含如下几个要素

  1. 时间
  2. 项目名
  3. 环境名
  4. 主题,传统主题为 info, debug, warn, err,也可以按需要自定义主题
  5. CRID,一个随机字符串,微服务互相调用的时候,会通过 X-Correlation-ID 头传递这个字符串,并在日志中输出,用来追踪整个调用链
  6. 关键字,为了节约日志系统资源,只有关键字字段内的文本会被索引,并可以查询
  7. 日志内容,纯文本内容
  8. 额外字段,可以将多个JSON字段附加到这条日志上,用来进行更精确的索引

使用方法

  1. 安装 npm install --save logtube

  2. 尽早执行 setup 方法

    import * as logtube from "logtube"
    
    logtube.setup({
      project: "demoproject",   // 设置项目名,只允许数字,- 和 _
      env: "test",              // 设置项目环境名,一般为 dev, test, uat/staging, prod
      console: {                // 命令行输出
        topics: ["*", "-debug"],// 设置要通过命令行输出的主题,"*" 代表全部主题,-debug 代表除了 debug 主题
      },
      file: {                    // 日志文件输出
        topics: ["*", "-debug"], // 设置要通过日志文件输出的主题, "*" 代表全部主题,-debug 代表了除了 debug 主题
        dir: "logs",             // 设置日志输出的跟目录
        subdirs: {
            xlog: ["*", "-debug"] // 非调试日志输出到 `xlog` 子目录内,便于收集
        }
      }
    })
  3. 使用常规输出

    第一个参数为关键字,逗号或空格分割

    只有关键字可以在日志系统检索,第二个参数为日志内容

    logtube.info("USER12345678", "order created with id 12345678")
    logtube.warn("USER12345678", "order created with id 12345678")
    logtube.err("USER12345678", "order created with id 12345678")
    logtube.debug("USER12345678", "order created with id 12345678")
  4. 使用 Express 中间件

    Express 中间件会捕获 CRID,并创建请求专用的 Logger

    const app = express()
      
    // 添加 express 中间件,自动为 ctx.locals 添加 log 和 crid
    app.use(logtube.express());
     
    // 添加 expressAccess 中间件,自动输出符合标准的 x-access 日志
    app.use(logtube.expressAccess())
       
    app.use("/", function(req, res) {
      res.locals.log //  此变量为当前 request 专用的 Logger
      res.locals.log.info("hello,world", "this is a hello world message")
      res.locals.crid // 此变量保存了 CRID 字符串,用于跨多个服务追踪请求,进行下一级服务调用,需要将此值设置到 Header X-Correlation-ID
      res.send("hello world")
    })
  5. 使用 Koa 中间件

    Koa 中间件会捕获 CRID,并创建请求专用的 Logger

    const app = Koa();
      
    // 添加 koa 中间件,自动为 ctx.state 添加 log 和 crid
    app.use(logtube.koa());
      
    // 添加 koaAccess 中间件,自动输出符合标准的 x-access 日志
    app.use(logtube.koaAccess())
      
    app.use((ctx) => {
       ctx.state.log //  此变量为当前 request 专用的 Logger
       ctx.state.log.info("hello world", "hello, world info");
       ctx.state.crid // 此变量保存了 CRID 字符串,用于跨多个服务追踪请求,进行下一级服务调用,需要将此值设置到 Header X-Correlation-ID
       ctx.body = 'hello word';
    });
  6. 为 Logger 设置默认的 extras

    // express
    app.use("/", function(req, res) {
      // 这一行会让所有该 Logger 生成的日志事件追加 x_module = "my_module" 字段
      res.locals.log.extras.module = "my_module";
    })
    
    // koa
    app.use((ctx) => {
      // 这一行会让所有该 Logger 生成的日志事件追加 x_module = "my_module" 字段
      ctx.state.log.extras.module = "my_module";
    })
  7. 传导 CRID 和 CRSRC 到下一级项目(以 requests 包为例)

    // express
    app.use("/", function(req, res) {
      requests("https://httpbin.org/get", {
        headers: res.locals.logtubeHeaders
      }).then((res) => {
      })
    })
    // koa
    app.use((ctx)=>{
      await requests("https://httpbin.org/get", {
        headers: ctx.state.logtubeHeaders
      })
    })
  8. 性能日志

let perf = logger.perf()
.className("MyClass")
.methodName("MyMethod")
.action("some_action")
.actionDetail("hello world");

setTimeout(function() {
    // 可以记录一条任意整数
    // 提交时,会进行计时,报告 x_duration = 2000
    perf.valueInteger(999).commit(); 
}, 2000)
  1. 审计日志
logger.audit()
.userCode("10086")
.userName("郭麒麟")
.action("some_action")
.actionDetail("详细信息")
// ...设置其他值
.commit();

Credits

Guo Y.K., MIT License