1 Easylogging++
Easylogging++ é uma biblioteca de logging de plataforma cruzada de código aberto com apenas um único arquivo de cabeçalho. Ela tem as vantagens de ser simples e fácil de integrar, extremamente rápida, thread-safe, eficiente, configurável e escalável. Agora é minha principal biblioteca de logging.
1.1 Baixar Easylogging++
Endereço Github: https://github.com/amrayn/easyloggingpp
Baixe Easylogging++ do Githu. Existem apenas dois arquivos baixados easylogging++.h
e easylogging++.cc
.
1.1 Configurar Easylogging++ no VS
Clique com o botão direito nos projetos incluídos project-Properties-C++General-Additional, adicione easylogging++.h
o diretório onde ele está localizado
será easylogging++.cc
adicionado ao projeto.
1.2 Usando Easylogging++
(1) Incluir arquivos de cabeçalho
// easylogging++
#include "easylogging++.h"
#define ELPP_THREAD_SAFE
(2) Inicialize o Easylogging++
INITIALIZE_EASYLOGGINGPP
(3) Definir configuração de saída de log
static void InitEasyloggingPP()
{
el::Configurations conf;
// 启用日志
conf.setGlobally(el::ConfigurationType::Enabled, "true");
//设置日志文件目录以及文件名
conf.setGlobally(el::ConfigurationType::Filename, "log\\log_%datetime{%Y%M%d %H%m%s}.log");
//设置日志文件最大文件大小
conf.setGlobally(el::ConfigurationType::MaxLogFileSize, "20971520");
//是否写入文件
conf.setGlobally(el::ConfigurationType::ToFile, "true");
//是否输出控制台
conf.setGlobally(el::ConfigurationType::ToStandardOutput, "true");
//设置日志输出格式
conf.setGlobally(el::ConfigurationType::Format, "[%datetime] [%loc] [%level] : %msg");
//设置日志文件写入周期,如下每100条刷新到输出流中
conf.setGlobally(el::ConfigurationType::LogFlushThreshold, "100");
//设置配置文件
el::Loggers::reconfigureAllLoggers(conf);
}
(4) Exemplo de programa
// easylogging++
#include "easylogging++.h"
#define ELPP_THREAD_SAFE
INITIALIZE_EASYLOGGINGPP
static void InitEasyloggingPP()
{
el::Configurations conf;
// 启用日志
conf.setGlobally(el::ConfigurationType::Enabled, "true");
//设置日志文件目录以及文件名
conf.setGlobally(el::ConfigurationType::Filename, "log\\log_%datetime{%Y%M%d %H%m%s}.log");
//设置日志文件最大文件大小
conf.setGlobally(el::ConfigurationType::MaxLogFileSize, "20971520");
//是否写入文件
conf.setGlobally(el::ConfigurationType::ToFile, "true");
//是否输出控制台
conf.setGlobally(el::ConfigurationType::ToStandardOutput, "true");
//设置日志输出格式
conf.setGlobally(el::ConfigurationType::Format, "[%datetime] [%loc] [%level] : %msg");
//设置日志文件写入周期,如下每100条刷新到输出流中
conf.setGlobally(el::ConfigurationType::LogFlushThreshold, "100");
//设置配置文件
el::Loggers::reconfigureAllLoggers(conf);
}
int main()
{
InitEasyloggingPP();
LOG(INFO) << "Hello World";
}
Se você estiver interessado, pode visitar meu site pessoal: https://www.stubbornhuang.com/