我有一个程序作为服务运行了很长时间。我用BOOST_LOG_TRIVIAL(info) << "Message"
用于记录各种事件。我愿意与logrotate
合作并在轮换后重新打开我的日志文件。理论上,剧本的工作原理如下:
- 我的程序启动,发生一些日志记录
- 日志由
something.log
重命名至something.log.1
通过其他程序,如logrotate
或者由我手动操作。 - 我的程序继续登录到
something.log.1
文件。 - 我向我的程序发送 SIGHUP (或其他东西),以便我可以重新打开日志文件。但我不知道该怎么做。
到目前为止我准备的示例(可能没有必要):
#include <iostream>
#include <stdexcept>
#include <csignal>
#include <boost/thread.hpp>
#include <boost/chrono.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/support/date_time.hpp>
inline
void setupLogging(std::string const &logFileName)
{
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace keywords = boost::log::keywords;
logging::add_common_attributes();
auto format = expr::stream
<< expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S.%f")
<< " progname " << logging::trivial::severity
<< ": " << expr::smessage;
auto fileOutput = logging::add_file_log(
keywords::file_name = logFileName, keywords::format = format
, keywords::auto_flush = true, keywords::open_mode = std::ios::app
);
auto consoleOutput = logging::add_console_log(
std::cerr, keywords::format = format, keywords::auto_flush = true
);
BOOST_LOG_TRIVIAL(debug) << "CHECKPOINT @ setupLogging() after log initialization.";
}
void my_signal_handler(int signal)
{
BOOST_LOG_TRIVIAL(info) << "my_signal_handler BEGIN";
/* REOPEN LOG HERE */
BOOST_LOG_TRIVIAL(info) << "my_signal_handler END";
}
int main()
{
setupLogging("logrotate.test.log");
if(signal(SIGHUP, my_signal_handler) == SIG_ERR)
{
BOOST_LOG_TRIVIAL(error) << "Failed to register signal handler";
return 1;
} else
BOOST_LOG_TRIVIAL(info) << "Signal handler registered.";
BOOST_LOG_TRIVIAL(info) << "CHECKPOINT 0";
for(size_t i=1; i<100; ++i)
{
boost::this_thread::sleep_for( boost::chrono::seconds(1) );
BOOST_LOG_TRIVIAL(info) << "CHECKPOINT " << i;
}
return 0;
}
编译:
LINK="-lboost_system -lboost_date_time -lboost_log -lboost_log_setup -lboost_thread -lboost_chrono -lpthread"
g++ -std=c++11 -Wextra -DBOOST_LOG_DYN_LINK -pedantic -O3 logrotate_test.cpp -o logrotate_test $LINK
Adam 答案的源代码表示
namespace detail666777888
{
using namespace boost;
using namespace boost::log;
typedef shared_ptr< sinks::synchronous_sink< sinks::text_file_backend > > T;
}
typedef detail666777888::T SPFileSink;
SPFileSink logFileSink;
void setupLogging(...){
... logFileSink = logging::add_file_log ...
}
void my_sighup_handler(int /*signal*/)
{
BOOST_LOG_TRIVIAL(info) << "my_sighup_handler START";
auto oldLFS = logFileSink;
setupLogging("logrotate.test.log");
boost::log::core::get()->remove_sink(oldLFS);
BOOST_LOG_TRIVIAL(info) << "my_sighup_handler FINISH";
}
最佳答案
我遇到了同样的问题,并找到了一个非常简单的解决方案:只需使用单个文件名(没有任何模式)初始化 text_file_backend
并调用 text_file_backend::rotate_file()
>。这将关闭当前文件并打开一个同名的新文件。
// create sink
auto backend = boost::make_shared< boost::log::sinks::text_file_backend >(
boost::log::keywords::file_name = "my.log",
boost::log::keywords::open_mode = std::ios_base::out | std::ios_base::app
);
auto sink = boost::make_shared<sinks::synchronous_sink<boost::log::sinks::text_file_backend>>(backend);
boost::log::core::get()->add_sink(sink);
// ... do some logging
// move log file and...
// reopen log file
sink->locked_backend()->rotate_file();
// ... do more logging
我使用 std::ios_base::app
打开日志文件,以防止日志文件在文件存在时被截断。
该方案的优点是无需复制文件,日志记录不会丢失,也不会重复。
关于c++ - BOOST_LOG_TRIVIAL 与 logrotate(重新打开日志),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37944413/