日志等级 log level

What Is a Logging Level

log level or log severity is a piece of information telling how important a given log message is. It is a simple, yet very powerful way of distinguishing log events from each other. If the log levels are used properly in your application all you need is to look at the severity first. It will tell you if you can continue sleeping during the on-call night or you need to jump out of bed right away and hit another personal best in running between your bedroom and laptop in the living room.

You can think of the log levels as a way to filter the critical information about your system state and the one that is purely informative. The log levels can help in reducing the information noise and reduce alert fatigue.

The History of Log Levels

Before continuing with the description of the log levels themselves it would be good to know where the log levels come from. It all started with syslog. In the 80s, the Sendmail a mailer daemon project developed by Eric Allman required a logging solution. This is how Syslog was born. It was rapidly adopted by other applications in the Unix-like ecosystem and became a standard. Btw – at Sematext we do support Syslog format with Sematext Logs, our log management tool.

Syslog came with the idea of severity levels, which are now defined in the syslog standard. Syslog comes with the following severity levels:

  • Emergency
  • Alert
  • Critical
  • Error
  • Warning
  • Notice
  • Informational
  • Debug

After the 80s programming languages were evolving and different logging frameworks were introduced. Nowadays each programming language has its own logging framework allowing you to save data in various formats like JSON. In most cases you can ship data to different destinations like text file, syslog or Elasticsearch. But apart from the format and the possible destinations there is one thing that is common to the majority of them – the level of the log event.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
loglevel 是一个轻量级的前端日志库,可以方便地记录日志信息。使用 loglevel 库可以按照不同的日志级别输出日志信息,包括 trace、debug、info、warn 和 error 五个级别。 使用 loglevel 库非常简单,只需要按照以下步骤进行: 1. 安装 loglevel 库 可以使用 npm 或 yarn 进行安装: ```bash npm install loglevel --save # 或 yarn add loglevel ``` 2. 引入 loglevel 库 在需要记录日志的 JavaScript 文件中,需要引入 loglevel 库: ```javascript import log from 'loglevel'; ``` 3. 设置日志级别 可以使用 `setLevel()` 方法设置日志级别,级别分别为:trace、debug、info、warn 和 error。默认级别为 warn。 ```javascript log.setLevel('trace'); ``` 4. 记录日志 使用 loglevel 库可以按照不同的日志级别输出日志信息,例如: ```javascript log.trace('entering foo()'); log.debug('received request'); log.info('processing request'); log.warn('unable to process request'); log.error('error occurred'); ``` 在以上代码中,`trace` 级别是最低级别,`error` 级别是最高级别。当日志级别设置为 `debug` 时,会输出 `debug`、`info`、`warn` 和 `error` 四个级别的日志信息。 5. 使用插件 loglevel 还提供了一些插件,可以方便地扩展其功能。例如,使用 loglevel-plugin-prefix 插件可以为日志信息添加前缀: ```javascript import log from 'loglevel'; import prefix from 'loglevel-plugin-prefix'; prefix.reg(log); log.enableAll(); prefix.apply(log, { format(level, name, timestamp) { return `${timestamp} [${name}] ${level}:`; }, }); ``` 以上代码使用 loglevel-plugin-prefix 插件为日志信息添加前缀,并设置前缀格式为时间、日志名称和日志级别。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值