2b-logger 中文文档教程

发布于 7年前 浏览 49 项目主页 更新于 3年前

2b-logger

一个简单易用的 JS 记录器

Installation

    npm install 2b-logger

如果要将安装保存在 package.json 中:

    npm install --save 2b-logger

或者

    npm install --save-dev 2b-logger

Basic usage

此记录器的基本用法如下:

    const loggerConstructor = require ( "2b-logger" );
    const logger = loggerConstructor ();
    logger.info("A nice info message");

或者,在简化版本中:

    const logger = require ( "2b-logger" ) ();
    logger.info("A nice info message");

import require ( "2b-logger" ) 将返回一个构造函数,该函数可以选择接受一个配置对象。

默认选项是:

    {
        "level": "info",
        "noColor": false,
        "timestampFormat": "YYYY-MM-DD @ HH:mm:ss:ms"
    }

Options

options 对象可以接收以下配置:

level (string, default: info)

级别指示哪些消息将被记录,哪些将被丢弃。 可接受的值为 tracedebuginfowarnerror

下表显示了哪些消息将被记录,具体取决于配置的级别:

Configured Level
tracedebuginfowarnerror
Message Level:trace✔️
debug✔️️️️️️✔️️️️️
info or success✔️✔️✔️
warn✔️✔️✔️✔️
error✔️✔️✔️✔️✔️️

具有 success 级别的消息被认为是 info 级别的消息,但它们具有不同的呈现样式.

noColor (boolean, default: false)

此布尔选项指示记录器不应向呈现的消息添加颜色。

即使记录的消息是彩色的,如果 noColor 设置为 true,它们也会被移除颜色。

示例:

    const logger = require ( "2b-logger" ) ({ noColor: true });
    const chalk = require ( "chalk" );
    logger.info ( chalk.cyan ( "A cyan message" ) );

在这种情况下,重新显示的消息不会 有颜色。

timestampFormat (string, default: YYYY-MM-DD @ HH:mm:ss:ms)

这是呈现消息时要使用的时间戳格式。 可以在此处查看接受的格式。

API

创建记录器后,可以使用以下方法:

logger.trace ( msg1, msg2, ... )

使用 trace 级别记录消息。

logger.debug ( msg1, msg2, ... )

使用 debug 级别记录消息。

logger.info ( msg1, msg2, ... )

使用 info 级别记录消息。

logger.success ( msg1, msg2, ... )

使用 success 级别记录消息。

logger.warn ( msg1, msg2, ... )

使用 warn 级别记录消息。

logger.error ( msg1, msg2, ... )

使用 error 级别记录消息。

ℹ️ - About log parameters

  • The logger will ignore null parameters.
  • All other parameters will be converted to string, using .toString ( ).
  • If, and only if, the result string is neither null nor empty it will be appended to the message, using a space character as separator.

Example output

以下代码:

const logger = require ( "../src/index.js" ) ({
    level: "trace"
});

logger.trace ( "trace", "msg" );
logger.debug ( "debug", "msg" );
logger.info ( "info", "msg" );
logger.success ( "success", "msg" );
logger.warn ( "warn", "msg" );
logger.error ( "error", "msg" );

将产生以下输出:

sample output

2b-logger

A simple to use JS logger.

Installation

    npm install 2b-logger

If you want to save the install in package.json:

    npm install --save 2b-logger

Or

    npm install --save-dev 2b-logger

Basic usage

The basic usage for this logger is the following:

    const loggerConstructor = require ( "2b-logger" );
    const logger = loggerConstructor ();
    logger.info("A nice info message");

Or, in a simplified version:

    const logger = require ( "2b-logger" ) ();
    logger.info("A nice info message");

The import require ( "2b-logger" ) will return a constructor function that takes, optionally, a configuration object.

The default options are:

    {
        "level": "info",
        "noColor": false,
        "timestampFormat": "YYYY-MM-DD @ HH:mm:ss:ms"
    }

Options

The options object can receive the following configurations:

level (string, default: info)

The level indicates which messages will be logged and which will be discarded. The accepted values are trace, debug, info, warn and error.

The following table displays which messages will be logged, depending on the configured level:

Configured Level
tracedebuginfowarnerror
Message Level:trace✔️
debug✔️️️️️️✔️️️️️
info or success✔️✔️✔️
warn✔️✔️✔️✔️
error✔️✔️✔️✔️✔️️

Messages with the success level are considered to be of info level, but they have a different render style.

noColor (boolean, default: false)

This boolean option indicates the logger that no color should be added to rendered messages.

Even if logged messages are colored, they will have the color removed, if noColor was set as true.

Example:

    const logger = require ( "2b-logger" ) ({ noColor: true });
    const chalk = require ( "chalk" );
    logger.info ( chalk.cyan ( "A cyan message" ) );

In this case, the redered message will not have colors.

timestampFormat (string, default: YYYY-MM-DD @ HH:mm:ss:ms)

This is the timestamp format to be used when rendering messages. The accepted format can be seen here.

API

Once a logger has been created, the following methods are available in it:

logger.trace ( msg1, msg2, ... )

Logs the messsages using trace level.

logger.debug ( msg1, msg2, ... )

Logs the messsages using debug level.

logger.info ( msg1, msg2, ... )

Logs the messsages using info level.

logger.success ( msg1, msg2, ... )

Logs the messsages using success level.

logger.warn ( msg1, msg2, ... )

Logs the messsages using warn level.

logger.error ( msg1, msg2, ... )

Logs the messsages using error level.

ℹ️ - About log parameters

  • The logger will ignore null parameters.
  • All other parameters will be converted to string, using .toString ( ).
  • If, and only if, the result string is neither null nor empty it will be appended to the message, using a space character as separator.

Example output

The following code:

const logger = require ( "../src/index.js" ) ({
    level: "trace"
});

logger.trace ( "trace", "msg" );
logger.debug ( "debug", "msg" );
logger.info ( "info", "msg" );
logger.success ( "success", "msg" );
logger.warn ( "warn", "msg" );
logger.error ( "error", "msg" );

Will produce the following output:

sample output

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文