AI 考拉技术分享-Node 基础架构专题(二)

2019-02-12 14:49:50 +08:00
 kaolalicai

klg-logger

前言

今天给大家介绍 logger 日志工具的使用,用 dev 的小哥哥的话说:log 工具,基于 tracer,简单,可以显示 log 的位置。
详情请看以下配置方法,有问题欢迎在评论区提出!

配置

3.0 版本开始,将不对 tracer 做任何封装,直接使用 Tracer

详细用法见 tracer 文档https://www.npmjs.com/package/tracer

基本用法

简单版本

默认版本是把 log 输出到 console

js
import { Logger } from 'klg-logger'

const logger = Logger({
  level: config.get('log.level'),
  dateformat: 'yyyy-mm-dd HH:MM:ss.L',
  inspectOpt: {
    showHidden: false, // if true then the object's non-enumerable properties will be shown too. Defaults to false
    depth: 5 // tells inspect how many times to recurse while formatting the object. This is useful for inspecting large complicated objects. Defaults to 2. To make it recurse indefinitely pass null.
  }
})

logger.info('hello world')
logger.debug('hello %s', 'world')
logger.error('hello %s', 'error')

export {logger}  

配置项的详细解释:

interface LoggerConfig {
    /**
     * Output format (Using `tinytim` templating)
     *
     * Defaults to: `"{{timestamp}} <{{title}}> {{file}}:{{line}} ({{method}}) {{message}}"`
     *
     * Possible values:
     * - timestamp: current time
     * - title: method name, default is 'log', 'trace', 'debug', 'info', 'warn', 'error','fatal'
     * - level: method level, default is 'log':0, 'trace':1, 'debug':2, 'info':3, 'warn':4, 'error':5, 'fatal':6
     * - message: printf message, support %s string, %d number, %j JSON and auto inspect
     * - file: file name
     * - line: line number
     * - pos: position
     * - path: file's path
     * - method: method name of caller
     * - stack: call stack message
     */
    format?: string | [string, LevelOption<string>];
    /**
     * Datetime format (Using `Date Format`)
     */
    dateformat?: string;
    filters?: FilterFunction[] | LevelOption<FilterFunction> | Array<FilterFunction | LevelOption<FilterFunction | FilterFunction[]>>;
    /**
     * Output the log, if level of log larger than or equal to `level`.
     */
    level?: string | number;
    methods?: string[];
    /**
     * Get the specified index of stack as file information. It is useful for development package.
     */
    stackIndex?: number;
    inspectOpt?: {
        /**
         * If true then the object's non-enumerable properties will be shown too. Defaults to false.
         */
        showHidden: boolean,
        /**
         * Tells inspect how many times to recurse while formatting the object.
         * This is useful for inspecting large complicated objects.
         * Defaults to 2. To make it recurse indefinitely pass null.
         */
        depth: number
    };

    /**
     * Pre-process the log object.
     */
    preprocess?(data: LogOutput): void;
    /**
     * Transport function (e.g. console.log)
     */
    transport?: TransportFunction | TransportFunction[];
}

自定义 transport

如果你需要把 log 输出到文件或者发送其他地方,可以自定义 transport function

import { Logger } from 'klg-logger'
const logger = new Logger({
  level: 'log',
  transport: function (data: Tracer.LogOutput) {
    // 写文件
    fs.write(data)
    // 发送其他地址
    tcp.send(data)

    assert(data)
    assert(data.level === 0)
  }
})
logger.log('hello world')

每日分割日志

如果你需要把 log 输出到文件或者发送其他地方,可以自定义 transport function

import { LoggerDaily } from 'klg-logger'
const logger = LoggerDaily({
  root: '/data/app/log',
  maxLogFiles: 10,
  allLogsFileName : true,
  level: 'log'
})
logger.log('hello world')
logger.err = logger.error
logger.err('hello world')

配置字段:

interface DailyFileConfig {
    /**
     * All daily log file's dir, default to: `'.'`.
     */
    root?: string;
    /**
     * Log file path format.
     *
     * Default to: `'{{root}}/{{prefix}}.{{date}}.log'`
     *
     * Possible values:
     * - `root`: all daily log file's dir, default to: `'.'`.
     * - `prefix`: it equal to `allLogsFileName`, if `allLogsFileName` is provided; else it will be the method name.
     * - `date`: today's date.
     */
    logPathFormat?: string;
    /**
     * Datetime format (Using `Date Format`)
     */
    splitFormat?: string;
    /**
     * If `allLogsFileName` is provided then all level logs will be move to one daily log file.
     */
    allLogsFileName?: boolean;
    maxLogFiles?: number;
}

著作权归本文作者所有,未经授权,请勿转载,谢谢。

3164 次点击
所在节点    Node.js
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/534210

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX