easylogging 的笔记
学习总结
应用在c++的工程里
easylog是一个用于记录日志的工具,其中分出了7种级别:分别是INFO;DEBUG;WARNING;TRACE;VERBOSE;ERROR;FATAL。其中FATAL这个log的输出会导致程序运行的退出:“Very severe error event that will presumably lead the application to abort.”通过配置log.conf文件,可以实现输出日志的格式、日志存放目录、存放方式(按照日志级别多文件存放,还是单文件汇总)。
通过配置log.conf文件,可以实现
输出log的格式、log存放目录、存放方式(按照log级别多文件存放,还是单文件汇总)
学习过程记录
https://www.cnblogs.com/linxmouse/p/9101504.html
发现林哥给的文档中提供了
新发现
//这个可以直接调调试窗口的命令欸,啧啧啧好东西
system(“ls”);
测试例程
按照下面这个例程跑出来的结果,只生成了一个log文件,所有的log都是打印在这里的。
main.cpp
#include "easylogging.h"
INITIALIZE_EASYLOGGINGPP
void easylogginginit()
{
// 加载配置文件,构造一个配置器对象
el::Configurations conf( "../../log.conf" );
// 重新配置一个单一日志记录器
el::Loggers::reconfigureLogger( "default", conf );
// 用配置文件配置所有的日志记录器
el::Loggers::reconfigureAllLoggers( conf );
}
int main()
{
easylogginginit();
LOG(TRACE) << "***** trace log *****";
LOG(DEBUG) << "***** debug log *****";
LOG(ERROR) << "***** error log *****";
LOG(WARNING) << "***** warning log *****";
LOG(INFO) << "***** info log *****";
// LOG(FATAL) << "***** fatal log *****";//这个会导致程序退出运行
LOG(INFO) << "***** info log123 *****";
//这个可以直接调调试窗口的命令欸,啧啧啧好东西
system("ls");
return 0;
}
希望的可以实现是不同类型的log信息分门别类的放在不同的文件夹
这样配置就会在可执行文件当前目录下的log文件夹中生成对应的log文件,前提/log目录已经存在
log.conf配置
* GLOBAL:
FORMAT = "%datetime:[%level] %msg"
ENABLED = true
TO_FILE = true
TO_STANDARD_OUTPUT = false
PERFORMANCE_TRACKING = false
FILENAME = "log/log_%datetime{%Y%M%d}.log"
MAX_LOG_FILE_SIZE = 209715200 ## Throw log files away after 2097152 2MB / 209715200 200MB / 4398046511104 1GB
* INFO:
FORMAT = "%datetime:[%level] %msg"
FILENAME = "log/debug_%datetime{%Y%M%d%H}.log"
* DEBUG:
FILENAME = "log/debug_log_%datetime{%Y%M%d}.log"
* WARNING:
FORMAT = "%datetime:[%level] %msg"
FILENAME = "log/warn_%datetime{%Y%M%d%H}.log"
* TRACE:
FILENAME = "log/trace_log_%datetime{%Y%M%d}.log"
* VERBOSE:
FORMAT = "%datetime{%d/%M/%y} | %level-%vlevel | %msg"
* ERROR:
FILENAME = "log/error_%datetime{%Y%M%d%H}.log"
#出现这个log会在终端上输出打印出来
TO_STANDARD_OUTPUT = true
* FATAL:
FILENAME = "log/fatal_%datetime{%Y%M%d%H}.log"
TO_STANDARD_OUTPUT = true
屏蔽 GLOBAL:的这句话,FILENAME那么全部log都会打印到myeasylog.log的文件夹中*
#出现这个log会在终端上输出打印出来
TO_STANDARD_OUTPUT = true
error 的log会在cli窗口额外输出
附上一段官方文档配置
Level级别
In order to start configuring your logging library, you must understand severity levels. Easylogging++ deliberately does not use hierarchical logging in order to fully control what’s enabled and what’s not. That being said, there is still option to use hierarchical logging using LoggingFlag::HierarchicalLogging
. Easylogging++ has following levels (ordered for hierarchical levels)
Level | Description |
---|---|
Global | Generic level that represents all levels. Useful when setting global configuration for all levels. 代表所有级别的通用级别。在为所有级别设置全局配置时很有用。 |
Trace | Information that can be useful to back-trace certain events - mostly useful than debug logs. 可用于回溯某些事件的信息-大多数信息比调试日志有用。 |
Debug | Informational events most useful for developers to debug application. Only applicable if NDEBUG is not defined (for non-VC++) or _DEBUG is defined (for VC++). 信息事件对开发人员调试应用程序最有用。仅在未定义NDEBUG(对于非VC ++)或_DEBUG(对于VC ++)时适用。 |
Fatal | Very severe error event that will presumably lead the application to abort. 非常严重的错误事件,可能会导致应用程序中止。 |
Error | Error information but will continue application to keep running. 错误信息,但将继续使应用程序继续运行。 |
Warning | Information representing errors in application but application will keep running. 表示应用程序错误的信息,但应用程序将继续运行。 |
Info | Mainly useful to represent current progress of application. 主要用于表示当前的应用进度。 |
Verbose | Information that can be highly useful and vary with verbose logging level. Verbose logging is not applicable to hierarchical logging. 信息非常有用,并且随着详细的日志记录级别而变化。详细日志记录不适用于分层日志记录。 |
Unknown | Only applicable to hierarchical logging and is used to turn off logging completely. 仅适用于分层日志记录,用于完全关闭日志记录。 |
Configure
Easylogging++ is easy to configure. There are three possible ways to do so,
- Using configuration file
- Using el::Configurations class
- Using inline configuration
Using Configuration File
Configuration can be done by file that is loaded at runtime by Configurations
class. This file has following format;
* LEVEL:
CONFIGURATION NAME = "VALUE" ## Comment
ANOTHER CONFIG NAME = "VALUE"
Level name starts with a star (*) and ends with colon (😃. It is highly recommended to start your configuration file with Global
level so that any configuration not specified in the file will automatically use configuration from Global
. For example, if you set Filename
in Global
and you want all the levels to use same filename, do not set it explicitly for each level, library will use configuration value from Global
automatically.
Following table contains configurations supported by configuration file.
Configuration Name | Type | Description |
---|---|---|
Enabled | bool | Determines whether or not corresponding level for logger is enabled. You may disable all logs by using el::Level::Global 确定是否启用了记录器的相应级别。您可以通过使用禁用所有日志 el::Level::Global |
To_File | bool | Whether or not to write corresponding log to log file 是否将相应的日志写入日志文件 |
To_Standard_Output | bool | Whether or not to write logs to standard output e.g, terminal or command prompt 是否将日志写入标准输出,例如终端或命令提示符 |
Format | char* | Determines format/pattern of logging for corresponding level and logger. 确定相应级别和记录器的记录格式/模式。 |
Filename | char* | Determines log file (full path) to write logs to for corresponding level and logger 确定日志文件(完整路径)以将日志写入相应级别的日志记录器 |
Subsecond_Precision | uint | Specifies subsecond precision (previously called ‘milliseconds width’). Width can be within range (1-6) 指定亚秒级精度(以前称为“毫秒宽度”)。宽度可以在(1-6)范围内 |
Performance_Tracking | bool | Determines whether or not performance tracking is enabled. This does not depend on logger or level. Performance tracking always uses ‘performance’ logger unless specified 确定是否启用性能跟踪。这与记录器或级别无关。除非指定,否则性能跟踪始终使用“性能”记录器 |
Max_Log_File_Size | size_t | If log file size of corresponding level is >= specified size, log file will be truncated. 如果相应级别的日志文件大小> =指定大小,则日志文件将被截断。 |
Log_Flush_Threshold | size_t | Specifies number of log entries to hold until we flush pending log data 指定在刷新未完成的日志数据之前要保留的日志条目数 |
Please do not use double-quotes anywhere in comment, you might end up in unexpected behaviour.
Sample Configuration File
* GLOBAL:
FORMAT = "%datetime %msg"
FILENAME = "/tmp/logs/my.log"
ENABLED = true
TO_FILE = true
TO_STANDARD_OUTPUT = true
SUBSECOND_PRECISION = 6
PERFORMANCE_TRACKING = true
MAX_LOG_FILE_SIZE = 2097152 ## 2MB - Comment starts with two hashes (##)
LOG_FLUSH_THRESHOLD = 100 ## Flush after every 100 logs
* DEBUG:
FORMAT = "%datetime{%d/%M} %func %msg"
Explanation
Configuration file contents in above sample is straightforward. We start with GLOBAL
level in order to override all the levels. Any explicitly defined subsequent level will override configuration from GLOBAL
. For example, all the levels except for DEBUG
have the same format, i.e, datetime and log message. For DEBUG
level, we have only date (with day and month), source function and log message. The rest of configurations for DEBUG
are used from GLOBAL
. Also, notice {%d/%M}
in DEBUG
format above, if you do not specify date format, default format is used. Default values of date/time is %d/%M/%Y %h:%m:%s,%g
For more information on these format specifiers, please refer to Date/Time Format Specifier section below
ERROR的效果
会具体打印出代码的位子
2021-03-05 09:31:36,584:[ERROR] VOID DataCollecter::LoopFuncList(std::vector<IOFunctionList>)[925] Data collection failed key is:0
2021-03-05 09:31:37,085:[ERROR] VOID DataCollecter::LoopFuncList(std::vector<IOFunctionList>)[925] Data collection failed key is:1
2021-03-05 09:31:37,586:[ERROR] VOID DataCollecter::LoopFuncList(std::vector<IOFunctionList>)[925] Data collection failed key is:2
2021-03-05 09:31:38,087:[ERROR] VOID DataCollecter::LoopFuncList(std::vector<IOFunctionList>)[925] Data collection failed key is:3
2021-03-05 09:31:38,588:[ERROR] VOID DataCollecter::LoopFuncList(std::vector<IOFunctionList>)[925] Data collection failed key is:3
2021-03-05 09:31:39,089:[ERROR] VOID DataCollecter::LoopFuncList(std::vector<IOFunctionList>)[925] Data collection failed key is:4
2021-03-05 09:31:39,591:[ERROR] VOID DataCollecter::LoopFuncList(std::vector<IOFunctionList>)[925] Data collection failed key is:5
2021-03-05 09:31:40,092:[ERROR] VOID DataCollecter::LoopFuncList(std::vector<IOFunctionList>)[925] Data collection failed key is:6
如何设置log存放路径
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!