Log
3 分钟阅读
📃 Log
We can use logs to observe program behavior, diagnose problems, or configure corresponding alarms. And defining a well structured log can improve search efficiency and facilitate handling of problems.
我们可以使用日志来观察程序行为、诊断问题或配置相应的告警。并且定义一个结构良好的日志可以提高搜索效率,方便问题的处理。
Fiber provides a default way to print logs in the standard output. It also provides several global functions, such as log.Info
, log.Errorf
, log.Warnw
, etc.
Fiber 提供了一种在标准输出中打印日志的默认方式。它还提供了一些全局函数,例如 log.Info
、 log.Errorf
、 log.Warnw
等。
Log levels 日志级别
|
|
Custom log 自定义日志
Fiber provides the AllLogger
interface for adapting the various log libraries.
Fiber 提供了 AllLogger
接口,用于适配各种日志库。
|
|
Print log 打印日志
Note: The method of calling the Fatal level will interrupt the program running after printing the log, please use it with caution. Directly print logs of different levels, which will be entered into messageKey, the default is msg.
注意:调用 Fatal 级别的方法会在打印日志后中断程序运行,请谨慎使用。直接打印不同级别的日志,会进入 messageKey,默认是 msg。
|
|
Format and print logs of different levels, all methods end with f
格式化并打印不同级别的日志,所有方法都以 f 结尾
|
|
Print a message with the key and value, or KEYVALS UNPAIRED
if the key and value are not a pair.
打印带有键和值的消息,或者如果键和值不是一对,则打印 KEYVALS UNPAIRED
。
|
|
Global log 全局日志
If you are in a project and just want to use a simple log function that can be printed at any time in the global, we provide a global log.
如果您在一个项目中,只想使用一个可以在全局随时打印的简单日志功能,我们提供了一个全局日志。
|
|
The above is using the default log.DefaultLogger
standard output. You can also find an already implemented adaptation under contrib, or use your own implemented Logger and use log.SetLogger
to set the global log logger.
以上使用默认 log.DefaultLogger
标准输出。您还可以在 contrib 下找到已实现的改编,或使用您自己实现的 Logger 并使用 log.SetLogger
设置全局日志记录器。
|
|
Set Level 设置级别
log.SetLevel
sets the level of logs below which logs will not be output. The default logger is LevelTrace.
log.SetLevel
设置日志级别,低于该级别的日志将不会输出。默认记录器是 LevelTrace。
Note that this method is not concurrent-safe.
请注意,此方法不是并发安全的。
|
|
Set output 设置输出
log.SetOutput
sets the output destination of the logger. The default logger types the log in the console.
log.SetOutput
设置记录器的输出目标。默认记录器在控制台中键入日志。
|
|
Set the output destination to the file.
将输出目标设置为文件。
|
|
Set the output destination to the console and file.
将输出目标设置为控制台和文件。
|
|
Bind context 绑定上下文
Set the context, using the following method will return a CommonLogger
instance bound to the specified context
设置上下文,使用以下方法将返回一个绑定到指定上下文的 CommonLogger
实例
|
|