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

klg-logger

v3.0.1

Published

log 工具,基于 tracer,简单,可以显示 log 的位置

Downloads

193

Readme

klg-logger

log 工具,基于 tracer,简单,可以显示 log 的位置

配置

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

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

基本用法

简单版本

默认版本是把 log 输出到 console

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;
}