先给结果:(单位:个消息/毫秒,越大越好)
glog spdlog spdlog(file:line) spdlog(glogformat)
406 458 372 275
376 452 369 274
381 453 373 274
397 456 370 275
390 456 373 274
343 458 372 274
384 435 368 273
374 457 372 273
395 458 371 274
403 456 373 274
综上,建议使用google 的glog 说明:行首对应的意思是输出对应格式:
spdlog(file:line):
[2019-04-06 20:19:05.593] [daily_logger] [info] SPEED TEST 958478914 85172499 1939277490 2124885215 main.cpp:45
[2019-04-06 20:19:05.593] [daily_logger] [info] SPEED TEST 45924426 707812707 822932816 383118987 main.cpp:45
spdlog(glogformat):
[I0406 20:37:42.948529] 31738 SPEED TEST 1657012941 787563074 1084038900 432746568 main.cpp:45
[I0406 20:37:42.948532] 31738 SPEED TEST 914842699 1755271366 338999928 1090456647 main.cpp:45
glog:
I0406 19:33:11.966251 25746 main.cpp:104] SPEED TEST 1614634400 1061769102 1857933338 665164151
I0406 19:33:11.966254 25746 main.cpp:104] SPEED TEST 1263201407 473825107 1058894756 677183342
glog测试代码:
#if defined(WIN32)||defined(WINDOWS)
#include <windows.h>
#include <io.h>
#include <direct.h>
#else
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#endif
#include <string.h>
#define GOOGLE_GLOG_DLL_DECL
#define GLOG_NO_ABBREVIATED_SEVERITIES
#include "glog/logging.h"
#include<chrono>
#include<vector>
#include<string>
using namespace std;
bool dirExists(const std::string& dirName_in)
{
return access(dirName_in.c_str(), F_OK) == 0;
}
#include <string>
#include <iostream>
void init_glog(const char* pname, const std::string& logDir)
{
google::InitGoogleLogging(pname);
FLAGS_colorlogtostderr = true;
FLAGS_logbufsecs = 0;
FLAGS_max_log_size = 1024 * 1024;
FLAGS_stop_logging_if_full_disk = true;
bool result = dirExists(logDir);
if (!result)
{
::mkdir(logDir.c_str(), 0755);
}
char buffer[256] = {0};
memset(buffer, 0, sizeof(buffer));
snprintf(buffer, sizeof(buffer), "%s/info.log", logDir.c_str());
google::SetLogDestination(google::GLOG_INFO, buffer);
memset(buffer, 0, sizeof(buffer));
snprintf(buffer, sizeof(buffer), "%s/warning.log", logDir.c_str());
google::SetLogDestination(google::GLOG_WARNING, buffer);
memset(buffer, 0, sizeof(buffer));
snprintf(buffer, sizeof(buffer), "%s/error.log", logDir.c_str());
google::SetLogDestination(google::GLOG_ERROR, buffer);
memset(buffer, 0, sizeof(buffer));
snprintf(buffer, sizeof(buffer), "%s/fatal.log", logDir.c_str());
google::SetLogDestination(google::GLOG_FATAL, buffer);
}
int64_t GetTimeMS()
{
auto time_now = chrono::system_clock::now();
auto duration_in_ms = chrono::duration_cast<chrono::milliseconds>(time_now.time_since_epoch());
return duration_in_ms.count();
}
int main(int argc, const char** argv)
{
init_glog(argv[0], "./logs");
#if 0
char str[20] = "hello glog!";
LOG(INFO) << str;
std::string cStr = "hello google!";
LOG(INFO) << cStr;
LOG(INFO) << "info test" << "hello log!"; //输出一个Info日志
LOG(WARNING) << "warning test"; //输出一个Warning日志
LOG(ERROR) << "error test"; //输出一个Error日志
#else
srand(GetTimeMS());
vector<int> vi1;
vector<int> vi2;
vector<string> vs1;
vector<string> vs2;
for(int nIdx = 0;nIdx < 10;++nIdx)
{
vi1.push_back(rand());
vi2.push_back(rand());
vs1.push_back(" " + std::to_string(rand()) + " ");
vs2.push_back(" " + std::to_string(rand()) + " ");
}
int nTestCount = 10000000;
auto tbegin = GetTimeMS();
cout << "begin" << endl;
for(int nIdx = 0;nIdx < nTestCount;++nIdx)
{
LOG(INFO) << " SPEED TEST " << vi1[nIdx % 10] << vs1[nIdx % 10] << vi2[nIdx % 10] << vs2[nIdx % 10];
}
auto tend = GetTimeMS();
auto diff = tend - tbegin;
auto spd = nTestCount / diff;
cout << "end " << " diff:" << diff << " speed:" << spd << " msgs/MS " << endl;
#endif
#if 0
std::string strInput;
std::cin >> strInput;
while (strInput != "E" && strInput != "e")
{
LOG(ERROR) << strInput;
std::cin >> strInput;
}
#endif
google::ShutdownGoogleLogging();
return 0;
}
spdlog测试代码:
#include <cstdio>
#include "spdlog/spdlog.h"
#include<chrono>
#include<vector>
#include<string>
#include<iostream>
using namespace std;
int64_t GetTimeMS()
{
auto time_now = chrono::system_clock::now();
auto duration_in_ms = chrono::duration_cast<chrono::milliseconds>(time_now.time_since_epoch());
return duration_in_ms.count();
}
#include "spdlog/sinks/daily_file_sink.h"
void daily_example()
{
// Create a daily logger - a new file is created every day on 2:30am.
auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);
srand(GetTimeMS());
vector<int> vi1;
vector<int> vi2;
vector<string> vs1;
vector<string> vs2;
for(int nIdx = 0;nIdx < 10;++nIdx)
{
vi1.push_back(rand());
vi2.push_back(rand());
vs1.push_back(" " + std::to_string(rand()) + " ");
vs2.push_back(" " + std::to_string(rand()) + " ");
}
int nTestCount = 10000000;
auto tbegin = GetTimeMS();
cout << "begin" << endl;
for(int nIdx = 0;nIdx < nTestCount;++nIdx)
{
daily_logger->info(" SPEED TEST {} {} {} {} {}:{}", vi1[nIdx % 10], vs1[nIdx % 10], vi2[nIdx % 10], vs2[nIdx % 10], __FILE__, __LINE__);
}
auto tend = GetTimeMS();
auto diff = tend - tbegin;
auto spd = nTestCount / diff;
cout << "end " << " diff:" << diff << " speed:" << spd << " msgs/MS " << endl;
}
int main(int, char *[])
{
// Customize msg format for all loggers
//spdlog::set_pattern("[%H:%M:%S %z] [%^%L%$] [thread %t] %v");
spdlog::set_pattern("[%^%L%$%m%d %H:%M:%S.%f] %t %v");
spdlog::info("This an info message with custom format");
//spdlog::set_pattern("%+"); // back to default format
try
{
daily_example();
spdlog::shutdown();
}
// Exceptions will only be thrown upon failed logger or sink construction (not during logging).
catch (const spdlog::spdlog_ex &ex)
{
std::printf("Log initialization failed: %s\n", ex.what());
return 1;
}
}
综上,建议使用google的glog
20190408:glog与log4cpp相比也是更快