@4lch4/logger 中文文档教程
@4lch4/logger
此模块是一个小型实用程序模块,用于记录到标准输出,如果需要,还可以记录到文件。
Installation
该模块已发布到 NPM 注册表,因此您可以像安装任何其他模块一样安装它:
# Using NPM
npm install --save @4lch4/logger
# Using Yarn
yarn add @4lch4/logger
# Using PNPM
pnpm install @4lch4/logger
Usage
使用记录器非常简单。 导入 Logger,创建一个具有一些可选属性的新实例,然后在整个应用程序中使用该实例:
注意:您还可以在整个应用程序中创建新实例,它将记录到相同的文件/位置。< /em>
import { Logger } from '@4lch4/logger'
const logger = new Logger({
// optional properties
})
logger.info('Hello, world!')
logger.error('Uh oh, something broke...')
Logging Levels
它有 5 个“级别”的日志记录,每个级别都有一个颜色,如下所示(排名不分先后):
- debug - Cyan
- error - Red
- info - Gray
- success - Green
- warn - Yellow
如果在实例化 Logger 类时提供 logDir,则每个级别都有一个文件。 例如,调试将是 2021.03.17-debug.log
,而错误将是 2021.03.17-error.log
。
每个级别都有一个函数被调用以在该级别进行日志记录。 例如,如果您想写入调试级别,您可以执行 logger.debug('Debug message')
。
@4lch4/logger
This module is a small utility module for logging to stdout and, if desired, to also log to files.
Installation
The module is published to the NPM registry so you're able to install it as you would any other module:
# Using NPM
npm install --save @4lch4/logger
# Using Yarn
yarn add @4lch4/logger
# Using PNPM
pnpm install @4lch4/logger
Usage
Using the logger is fairly straight forward. Import the Logger, create a new instance with some optional properties, then use that instance throughout your application:
NOTE: You can also create new instances throughout your app and it will log to the same files/locations.
import { Logger } from '@4lch4/logger'
const logger = new Logger({
// optional properties
})
logger.info('Hello, world!')
logger.error('Uh oh, something broke...')
Logging Levels
It has 5 "levels" of logging with a color for each, which are as follows (in no particular order):
- debug - Cyan
- error - Red
- info - Gray
- success - Green
- warn - Yellow
If you provide a logDir when instantiating the Logger class, each level will have a file. For example, debug would be 2021.03.17-debug.log
whereas error would be 2021.03.17-error.log
.
Each level has a function to be called for logging at that level. For example, if you want to write to the debug level, you'd do logger.debug('Debug message')
.