日志文件未创建 [英] Log file is not getting created
问题描述
我需要一个可以在项目中的多个类之间使用的全局记录器。我想在ini文件中添加记录器设置。
I need a global logger that I can use across multiple classes in my project. I want to have logger settings in an ini file.
I start following example here and combined it with some suggestions at my earlier question here.
我的Logger.h如下。
My Logger.h is as follows.
#pragma once
#include <boost/log/common.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/utility/setup/from_stream.hpp>
#include <boost/regex.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup.hpp>
#define INFO BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::info)
#define WARN BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::warning)
#define ERROR BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::error)
//Narrow-char thread-safe logger.
typedef boost::log::sources::severity_logger_mt<boost::log::trivial::severity_level> logger_t;
//declares a global logger with a custom initialization
BOOST_LOG_GLOBAL_LOGGER(my_logger, logger_t)
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(test_lg, boost::log::sources::severity_logger< >)
Logger.cpp如下。
The Logger.cpp is as follows.
#include "stdafx.h"
#include "Logger.h"
#include <fstream>
namespace attrs = boost::log::attributes;
namespace expr = boost::log::expressions;
namespace logging = boost::log;
//Defines a global logger initialization routine
BOOST_LOG_GLOBAL_LOGGER_INIT(my_logger, logger_t)
{
logger_t lg;
logging::add_common_attributes();
std::ifstream settings("settings.ini");
if (!settings.is_open())
{
std::cout << "Could not open settings.txt file" << std::endl;
//return 1;
}
// Read the settings and initialize logging library
logging::init_from_stream(settings);
// Add some attributes
logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
logging::register_simple_filter_factory<logging::trivial::severity_level>("Severity");
return lg;
}
settings.ini如下所示
The settings.ini looks like below
#
# Copyright Andrey Semashev 2007 - 2014.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
#
[Core]
Filter="%Severity% >= debug"
[Sinks.1]
Destination=TextFile
FileName=test.log
AutoFlush=true
Format="[%TimeStamp%] <%Severity%> (%Channel%): %Message%"
最后,我尝试使用记录器,如下所示。
Finally I am trying to use logger as below.
#include "Logger.h"
int wmain(int argc, wchar_t* argv[])
{
INFO << "Program started";
}
但是,没有创建日志文件。我现在在做什么错?
However, no log file is getting created. What am I doing wrong now?
推荐答案
在解析设置之前,必须先注册过滤器工厂 ini。
The filter factory needs to be registered before parsing the settings ini.
// Add some attributes
logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
logging::register_simple_filter_factory<logging::trivial::severity_level>("Severity");
需要在通话前
// Read the settings and initialize logging library
logging::init_from_stream(settings);
没有工厂,过滤器不会被解释,最终会过滤所有内容。
Without the factory, the filter is not interpreted and ends up filtering everything.
这篇关于日志文件未创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!