小型记录器类 [英] Small logger class

查看:98
本文介绍了小型记录器类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c ++中寻找一个小的轻量级日志系统。我发现一些现有的框架,但我不需要所有的功能,在这个时间点。我主要寻找一个小系统,例如可以配置日志级别输出文件。我正在寻找一个现有的解决方案,因为我不想重塑轮子。

I am looking for a small lightweight logging system in c++. I have found some existing frameworks but I don't need all of their features at this point in time. I primarily am looking for a small system that can for example configure the log level output file. I am looking for an existing solution as I don't want to reinvent the wheel.

推荐答案

我强烈推荐这个简单的日志系统: http://www.drdobbs.com/cpp/201804215 。它由一个单一的头文件组成。我已经成功地在Linux,Windows和Mac OS X上使用它。

I strongly recommend this simple logging system: http://www.drdobbs.com/cpp/201804215. It is composed of a single header file. I have successfully used it on Linux, Windows and Mac OS X.

这样写入日志:

FILE_LOG(logWARNING) << "Ops, variable x should be " << expectedX << "; is " << realX;

我真的很喜欢流语法。它是不引人注目,类型安全和表达。日志框架自动在行尾添加 \\\
,加上日期,时间和缩进。

I really like the stream syntax. It is unobtrusive, typesafe and expressive. The logging framework automatically adds a \n at the end of the line, plus date, time and indentation.

配置日志很容易:

FILELog::ReportingLevel() = logDEBUG3;
FILE* log_fd = fopen( "mylogfile.txt", "w" );
Output2FILE::Stream() = log_fd;

这个框架也很容易扩展。在工作中,我们最近做了一些修改,以便它现在使用 std :: ofstream 而不是 FILE * 。因此,我们现在可以添加不错的功能,例如通过链接流来加密日志。

This framework is also easy to extend. At work, we have recently made some adaptations to it so that it now uses an std::ofstream instead of a FILE*. As a result, we are now able to add nice features such as encrypting the logs, by chaining the streams.

这篇关于小型记录器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆