一个开源的C++日志框架-G3log的简单示例

概述

G3log 是一个开源、支持跨平台的异步 C++ 日志框架,支持自定义日志格式。基于 g2log 构建,提升了性能,支持自定义格式。

G3log 主要特性:

  • 日志和契约式设计框架
  • 异步调用
  • 线程安全
  • 队列式日志
  • 捕获和记录 SIGSEGV 以及其他严重的信号
  • 在 Linux/OSX 上严重的信号会生成堆栈记录
  • G3log 跨平台,支持 Windows, Linux 和 OSX

链接:https://github.com/KjellKod/g3log

编译安装

从github上下载 g3log 源码

准备工作

1
2
3
4
5
6
# cd g3log
# cd 3rdParty/gtest
# unzip gtest-1.7.0.zip
# cd ../../
# mkdir build
# cd build

编译

1
2
# cmake -DCMAKE_BUILD_TYPE=Release ..
# make

当前g3log默认使用的是c++14,若系统用的是c++11,则把目录下所有的c++14都替换为c++11后即可;

安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# sudo make install
[ 60%] Built target g3logger
[ 73%] Built target g3log-FATAL-contract
[ 86%] Built target g3log-FATAL-sigsegv
[100%] Built target g3log-FATAL-choice
Install the project...
-- Install configuration: "Release"
-- Up-to-date: /usr/local/lib/libg3logger.so.1.3.0-0
-- Up-to-date: /usr/local/lib/libg3logger.so
-- Up-to-date: /usr/local/include/g3log/active.hpp
-- Up-to-date: /usr/local/include/g3log/atomicbool.hpp
-- Up-to-date: /usr/local/include/g3log/crashhandler.hpp
-- Up-to-date: /usr/local/include/g3log/filesink.hpp
-- Up-to-date: /usr/local/include/g3log/future.hpp
-- Up-to-date: /usr/local/include/g3log/g3log.hpp
-- Up-to-date: /usr/local/include/g3log/logcapture.hpp
-- Up-to-date: /usr/local/include/g3log/loglevels.hpp
-- Up-to-date: /usr/local/include/g3log/logmessage.hpp
-- Up-to-date: /usr/local/include/g3log/logworker.hpp
-- Up-to-date: /usr/local/include/g3log/moveoncopy.hpp
-- Up-to-date: /usr/local/include/g3log/shared_queue.hpp
-- Up-to-date: /usr/local/include/g3log/sink.hpp
-- Up-to-date: /usr/local/include/g3log/sinkhandle.hpp
-- Up-to-date: /usr/local/include/g3log/sinkwrapper.hpp
-- Up-to-date: /usr/local/include/g3log/stacktrace_windows.hpp
-- Up-to-date: /usr/local/include/g3log/std2_make_unique.hpp
-- Up-to-date: /usr/local/include/g3log/stlpatch_future.hpp
-- Up-to-date: /usr/local/include/g3log/time.hpp
-- Up-to-date: /usr/local/include/g3log/g2log.hpp
-- Up-to-date: /usr/local/include/g3log/generated_definitions.hpp
-- Up-to-date: /usr/local/lib/cmake/g3logger/g3loggerConfig.cmake

使用g3log

代码示例

文件:g3log_tst.cc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <g3log/g3log.hpp>
#include <g3log/logworker.hpp>

using namespace std;

string path_to_log_file = "/tmp/";
string log_file = "g3log_file";

std::unique_ptr<g3::LogWorker> worker;

void log_init()
{
worker = g3::LogWorker::createLogWorker();
auto handle = worker->addDefaultLogger(log_file, path_to_log_file);
g3::initializeLogging(worker.get());
}

void log_shutdown()
{
g3::internal::shutDownLogging();
}

int main(int argc, char* argv[])
{
log_init();

// use LOGF which like printf
LOGF(INFO, "Hi log %d", 123);
LOGF(WARNING, "Printf-style syntax is also %s", "available”);

// use LOG wich like std::cout
LOG(INFO) << "Hi " << "LOG";

log_shutdown();
return 0;
}

编译运行

1
2
3
4
# g++ -std=c++11 -Wl,-rpath,/usr/local/lib -lg3logger -og3log_tst g3log_tst.cc
# ./g3log_tst
g3log g3FileSink shutdown at: 14:56:51 332071
Log file at: [/tmp/g3log_file.g3log.20171011-145651.log]

查看log文件内容:

1
2
3
4
5
6
7
8
# cat /tmp/g3log_file.g3log.20171011-145651.log
g3log created log at: Wed Oct 11 14:56:51 2017
LOG format: [YYYY/MM/DD hh:mm:ss uuu* LEVEL FILE->FUNCTION:LINE] message (uuu*: microseconds fractions of the seconds value)

2017/10/11 14:56:51 046351 INFO [g3log_tst.cc->main:29] Hi log 123
2017/10/11 14:56:51 046376 WARNING [g3log_tst.cc->main:30] Printf-style syntax is also available
2017/10/11 14:56:51 046413 INFO [g3log_tst.cc->main:33] Hi LOG
g3log g3FileSink shutdown at: 14:56:51 046529

g3log的动态log level

g3log支持dynamic logging level,默认是关闭的,若需要使用,在编译g3log时打开。

1
# cmake -DCMAKE_BUILD_TYPE=Release -DUSE_DYNAMIC_LOGGING_LEVELS=ON ../

在文件 g3log/loglevels.hpp 里有各个log level的定义:

1
2
3
4
5
6
7
8
9
10
11
12
13
namespace g3 {
static const int kDebugValue = 100;
static const int kInfoValue = 300;
static const int kWarningValue = 500;
static const int kFatalValue = 1000;
static const int kInternalFatalValue = 2000;
} // g3


const LEVELS G3LOG_DEBUG{g3::kDebugValue, {"DEBUG"}},
INFO {g3::kInfoValue, {"INFO"}},
WARNING {g3::kWarningValue, {"WARNING"}},
FATAL {g3::kFatalValue, {"FATAL"}};

设置log level的定义为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
namespace log_levels {
/// Enable log level >= log_level.
/// log levels below will be disabled
/// log levels equal or higher will be enabled.
void setHighest(LEVELS level);

void set(LEVELS level, bool enabled);
void disable(LEVELS level);
void enable(LEVELS level);

/// WARNING: This will also disable FATAL events from being logged
void disableAll();
void enableAll();


/// print all levels with their disabled or enabled status
std::string to_string(std::map<int, g3::LoggingLevel> levelsToPrint);

/// print snapshot of system levels with their
/// disabled or enabled status
std::string to_string();


/// Snapshot view of the current logging levels' status
std::map<int, g3::LoggingLevel> getAll();

enum class status {Absent, Enabled, Disabled};
status getStatus(LEVELS level);
} // log_levels

代码示例

文件:g3log_tst.cc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <g3log/g3log.hpp>
#include <g3log/logworker.hpp>

using namespace std;

string path_to_log_file = "/tmp/";
string log_file = "g3log_file";

std::unique_ptr<g3::LogWorker> worker;

void log_init()
{
worker = g3::LogWorker::createLogWorker();
auto handle = worker->addDefaultLogger(log_file, path_to_log_file);
g3::initializeLogging(worker.get());
}

void log_shutdown()
{
g3::internal::shutDownLogging();
}

void set_log_level()
{
g3::log_levels::setHighest(INFO);
}

int main(int argc, char* argv[])
{
log_init();
set_log_level();

// use LOGF which like printf
LOGF(INFO, "Hi log %d", 123);
LOGF(DEBUG, "Hi debug log %d", 123);
LOGF(WARNING, "Printf-style syntax is also %s", "available”);

// use LOG wich like std::cout
LOG(INFO) << "Hi " << "LOG";

log_shutdown();
return 0;
}

编译运行

1
2
3
4
# g++ -std=c++11 -Wl,-rpath,/usr/local/lib -lg3logger -og3log_tst g3log_tst.cc
# ./g3log_tst
g3log g3FileSink shutdown at: 16:25:39 537074
Log file at: [/tmp/g3log_file.g3log.20171011-162539.log]

查看log文件输出内容如下,可以看出并没有DEBUG级别的log输出:

1
2
3
4
5
6
7
8
# cat /tmp/g3log_file.g3log.20171011-162539.log
g3log created log at: Wed Oct 11 16:25:39 2017
LOG format: [YYYY/MM/DD hh:mm:ss uuu* LEVEL FILE->FUNCTION:LINE] message (uuu*: microseconds fractions of the seconds value)

2017/10/11 16:25:39 041987 INFO [g3log_tst.cc->main:35] Hi log 123
2017/10/11 16:25:39 042001 WARNING [g3log_tst.cc->main:37] Printf-style syntax is also available
2017/10/11 16:25:39 042016 INFO [g3log_tst.cc->main:40] Hi LOG
g3log g3FileSink shutdown at: 16:25:39 042180

参考

g3log还有很多别的特性,有需要的朋友可以详细研究下代码,很多使用示例也可以在源码里的g3log/example/test_unit/目录里找到,cmake和代码都可以参考之。

支持原创