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

baidu-log-parser

v1.0.2

Published

Parse the log of baidu server

Downloads

3

Readme

baidu-log-parser

用于解析百度后端的日志。类似于这样的:

[2018-12-30 18:00:01.028] [INFO] swan - logid:229c391a-c0e7-4993-8963-a68bacb19866_DCSjv4fohve16_0 pool_info:{"spareResourceCapacity":0,"size":4,"available":4,"borrowed":0,"pending":0,"max":4,"min":2} request:{"renderInfo":{"clientName":"puffer","moduleName":"lottery_views","onlyData":true,"needCommands":true}}

如果你们服务器的日志格式和这个差不多,但是此仓库却无法解析,请联系 [email protected]

Quick Start

npm install -g baidu-log-parser
cat node.log.2018123018 | parse-baidu-log

解析单行

把单行 Log 解析成一个JS对象。

const LineParser = require('baidu-log-parser/line-parser');

new LineParser(
    '[2018-12-30 18:00:01.027] [INFO] swan - [Perf][before ssr run .3][pid:14232],times: 1546164001027'
).parse();
/* 输出: { 
    date: 2018-12-30T10:00:01.027Z,
    level: 'INFO',
    name: 'swan',
    fields: {},
    text: '[Perf][before ssr run .3][pid:14232],times: 1546164001027' 
} */


new LineParser(
    `[2018-12-30 18:00:01.028] [INFO] swan - logid:229c391a-c0e7-4993-8963-a68bacb19866_DCSjv4fohve16_0 pool_info:{"spareResourceCapacity":0,"size":4,"available":4,"borrowed":0,"pending":0,"max":4,"min":2} request:{"renderInfo":{"clientName":"puffer","moduleName":"lottery_views","onlyData":true,"needCommands":true}}`
).parse();
/* 输出: { 
   date: 2018-12-30T10:00:01.028Z,
   level: 'INFO',
   name: 'swan',
   fields: { 
       logid: '229c391a-c0e7-4993-8963-a68bacb19866_DCSjv4fohve16_0',
       pool_info: { 
           spareResourceCapacity: 0,
           size: 4,
           available: 4,
           borrowed: 0,
           pending: 0,
           max: 4,
           min: 2 
       },
       request: { renderInfo: [Object] } 
   },
   text: '' 
}*/

解析文件(或任意一个可读流)

从一个日志文件中逐行读取出JS对象。

const Fs = require('fs');
const StreamParser = require('baidu-log-parser/stream-parser');

const log = new StreamParser(
    Fs.createReadStream('./node.log.2018123018')
);

(async function (){
    let logInfo = null;
    while (logInfo = await log.parseLine()) {
        // ...
    }
})();

在命令行中快速使用

可以在命令行中方便的将 stdin 的内容转换为JSON后 stdout 输出。

# 全局安装
npm install -g baidu-log-parser

# 将log文件按JSON格式输出
cat node.log.2018123018 | parse-baidu-log
cat node.log.2018123018 | grep '9031bd62-1340-4264-930f-3e9e903067c8_DCSiunsqxt09j_0' | parse-baidu-log
tail -f node.log.2018123018 | parse-baidu-log

该命令实际上就是将 process.stdin 传入 StreamParser,源代码差不多是这样的:

#!/usr/bin/env node

const StreamParser = require('../stream-parser');

const log = new StreamParser(process.stdin);

(async function () {
    let logInfo = null;
    while (logInfo = await log.parseLine()) {
        console.log(JSON.stringify(logInfo, null, '    '), '\n', '\n');
    }
})();