没有日志文件被创建 [英] No logs file getting created

查看:100
本文介绍了没有日志文件被创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的链接中使用示例。

I am using the example at the following link.

https://www.boost.org/doc/libs/1_57_0/libs/ log / doc / html / log / detailed / utilities.html#log.detailed.utilities.setup.settings_file

我的代码如下。

#include "stdafx.h"
#include <iostream>
#include <fstream>

#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/from_stream.hpp>
#include <boost/log/utility/setup/settings.hpp>
#include <boost/log/utility/setup/from_settings.hpp>
#include <boost/regex.hpp>

namespace logging = boost::log;
namespace keywords = boost::log::keywords;
#define BOOST_LOG_DYN_LINK 1


int main(int, char*[])
{
    //init_logging();
    std::ifstream file("settings.ini");
    logging::init_from_stream(file);

    BOOST_LOG_TRIVIAL(trace) << "This is a trace severity message";
    BOOST_LOG_TRIVIAL(debug) << "This is a debug severity message";
    BOOST_LOG_TRIVIAL(info) << "This is an informational severity message";
    BOOST_LOG_TRIVIAL(warning) << "This is a warning severity message";
    BOOST_LOG_TRIVIAL(error) << "This is an error severity message";
    BOOST_LOG_TRIVIAL(fatal) << "and this is a fatal severity message";

    return 0;
}

我的settings.ini文件如下所示。

My settings.ini file look as follows.

# Logging core settings section. May be omitted if no parameters specified within it.
[Core]
DisableLogging=false
Filter="%Severity% > 3"

# Sink settings sections
[Sinks.MySink1]

# Sink destination type
Destination=TextFile
FileName="MyApp.log"
RotationSize=1024

# Formatter string. Optional, by default only log record message text is written.
Format="<%TimeStamp%> - %Message%"

# The flag shows whether the sink should be asynchronous
Asynchronous=false

但是没有创建文件。我在做什么错了?

However no file is getting created. What am I doing wrong?

推荐答案

一切正常,直到您添加过滤器为止:

Everything works right up until you add the filter:

 Filter="%Severity% > 3"

使用Google搜索(例如,通过 Boost.Log配置文件),我发现:

Googling a bit (via Boost.Log Configuration Files e.g.) I found:



如何使用init_from_settings()配置严重性日志记录器以在日志中获取
自定义严重性级别?

How to configure severity_logger using init_from_settings() to get the custom severity levels in the log?

您必须在
库中注册任何用户定义的属性值类型,才能将其与过滤和格式化解析器。
这包括您的严重性级别枚举。

You have to register any user-defined attribute value types with the library in order to be able to use it with filter and format parsers. This includes your severity level enum.

添加为:

logging::register_simple_filter_factory<logging::trivial::severity_level>("Severity");

现在,修复过滤器表达式以使用文本表示形式:

Now, fix the filter expression to use the text representation:

Filter="%Severity% > debug"

同样,您需要将 TimeStamp 属性添加到核心

In similar vein, you need to add the TimeStamp attribute to core:

logging::core::get()->add_global_attribute("TimeStamp", logging::attributes::local_clock());

现在一切正常:

#include <fstream>
#include <iostream>

#include <boost/log/attributes.hpp>
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/from_settings.hpp>
#include <boost/log/utility/setup/from_stream.hpp>
#include <boost/log/utility/setup/settings.hpp>
#include <boost/regex.hpp>

namespace logging = boost::log;
namespace keywords = boost::log::keywords;
#define BOOST_LOG_DYN_LINK 1

int main(int, char *[]) {
    std::ifstream file("settings.ini");
    logging::core::get()->add_global_attribute("TimeStamp", logging::attributes::local_clock());
    logging::register_simple_filter_factory<logging::trivial::severity_level>("Severity");

    logging::init_from_stream(file);

    BOOST_LOG_TRIVIAL(trace) << "This is a trace severity message";
    BOOST_LOG_TRIVIAL(debug) << "This is a debug severity message";
    BOOST_LOG_TRIVIAL(info) << "This is an informational severity message";
    BOOST_LOG_TRIVIAL(warning) << "This is a warning severity message";
    BOOST_LOG_TRIVIAL(error) << "This is an error severity message";
    BOOST_LOG_TRIVIAL(fatal) << "and this is a fatal severity message";
}

写入MyApp.log的

Which writes to MyApp.log:

<2018-09-01 00:36:11.972436> - This is an informational severity message
<2018-09-01 00:36:11.972712> - This is a warning severity message
<2018-09-01 00:36:11.972735> - This is an error severity message
<2018-09-01 00:36:11.972747> - and this is a fatal severity message

已添加 Demo Live on Coliru

Added Demo Live On Coliru

这篇关于没有日志文件被创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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