将文件、函数、行添加到全局增强记录器
Posted
技术标签:
【中文标题】将文件、函数、行添加到全局增强记录器【英文标题】:add File, Function, Line to global boost logger 【发布时间】:2016-03-01 21:17:14 【问题描述】:我有工作的 boost 日志代码,但想将 File、Function 和 Line 添加到每个日志调用。这是我的代码:
日志记录.hpp
#ifndef LOGGING_HPP
#define LOGGING_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)
#define SYS_LOGFILE "/var/www/html/logs/pal5.log"
//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)
#endif
日志记录.cpp
#include "../include/logging.hpp"
// CFLAGS += -DBOOST_LOG_DYN_LINK
// LIBS += -lboost_log_setup -lboost_log -lpthread
namespace logging = boost::log;
namespace sinks = boost::log::sinks;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace attrs = boost::log::attributes;
namespace keywords = boost::log::keywords;
//Defines a global logger initialization routine
BOOST_LOG_GLOBAL_LOGGER_INIT(my_logger, logger_t)
logger_t lg;
logging::add_common_attributes();
logging::add_file_log(
keywords::file_name = SYS_LOGFILE,
keywords::rotation_size = 1024 * 1024 * 20, // megabytes
keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point (0, 0, 0),
keywords::auto_flush = true,
keywords::format = (
expr::stream << expr::format_date_time <boost::posix_time::ptime> ("TimeStamp", "%y-%m-%d %H:%M:%S.%f")
<< " [" << expr::attr <boost::log::trivial::severity_level> ("Severity") << "]: "
<< expr::smessage
)
);
logging::add_console_log(
std::cout,
keywords::format = (
expr::stream << expr::format_date_time <boost::posix_time::ptime> ("TimeStamp", "%y-%m-%d %H:%M:%S.%f")
<< " [" << expr::attr <boost::log::trivial::severity_level> ("Severity") << "]: "
<< expr::smessage
)
);
logging::core::get()->set_filter
(
logging::trivial::severity >= logging::trivial::info
);
return lg;
这是我目前的称呼:
INFO << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | started";
是否可以通过全局定义自动执行此操作?谢谢你。
【问题讨论】:
【参考方案1】:只需将宏定义为
#define INFO BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::info) << __FILE__ << "(" ..
【讨论】:
哎呀,我试过了,但我想我没有正确地重新编译头文件;谢谢【参考方案2】:四年后我更新的logging.hpp
只显示文件名而没有路径:
#ifndef LOGGING_HPP
#define LOGGING_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 __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__)
#define TRACE BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::trace) << "[" << __FILENAME__ << "/" << __FUNCTION__ << ":" << __LINE__ << "] "
#define DEBUG BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::debug) << "[" << __FILENAME__ << "/" << __FUNCTION__ << ":" << __LINE__ << "] "
#define INFO BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::info) << "[" << __FILENAME__ << "/" << __FUNCTION__ << ":" << __LINE__ << "] "
#define WARNING BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::warning) << "[" << __FILENAME__ << "/" << __FUNCTION__ << ":" << __LINE__ << "] "
#define ERROR BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::error) << "[" << __FILENAME__ << "/" << __FUNCTION__ << ":" << __LINE__ << "] "
#define FATAL BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::fatal) << "[" << __FILENAME__ << "/" << __FUNCTION__ << ":" << __LINE__ << "] "
#define SYS_LOGFILE "/var/www/html/logs/pal5_%y-%m-%d_#%N.log"
//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)
#endif
【讨论】:
以上是关于将文件、函数、行添加到全局增强记录器的主要内容,如果未能解决你的问题,请参考以下文章
JavaScript错题记录变量定义提升this指针指向运算符优先级原型继承全局变量污染对象属性及原型属性优先级