我试图让我的团队离开log4cxx并尝试使用Boost.Log v2.我们当前的log4cxx模式非常简单:
log4cxx::helpers::Properties prop;
prop.setProperty("log4j.rootLogger","DEBUG, A1");
prop.setProperty("log4j.appender.A1","org.apache.log4j.ConsoleAppender");
prop.setProperty("log4j.appender.A1.layout","org.apache.log4j.PatternLayout");
prop.setProperty("log4j.appender.A1.layout.ConversionPattern","%d{ABSOLUTE} %-5p [%c] %m%n");
log4cxx::PropertyConfigurator::configure(prop);
Run Code Online (Sandbox Code Playgroud)
但是我找不到支持文件名和行号打印的解决方案.到目前为止,我发现了一个旧帖子,但没有明确的解决方案(没有公认的解决方案).我已经研究过使用它们BOOST_LOG_NAMED_SCOPE但是它们感觉非常难看,因为当在同一个函数中使用多个这些行号时,它们无法打印正确的行号.
我在这里找到了一个更简单的直接解决方案.但是这也觉得很难看,因为它会打印完整路径,而不仅仅是基本名称(它不可配置).因此,完整路径和行号始终打印在固定位置(之前expr::smessage).
我也找到了这篇文章,看起来像一个旧的解决方案.
最后是最有前途的解决方案,我发现了这里.但是它甚至都没有为我编译.
所以我的问题很简单:我该如何使用Boost.Log V2打印的文件名(不FULLPATH)和行号以最小格式的灵活性(无解MACRO和__FILE__/ __LINE__接受,请).我会很感激它不涉及解决方案BOOST_LOG_NAMED_SCOPE,如描述在这里:
如果您想要查看特定日志记录的行号,那么最好的方法是定义一个用于写日志的自定义宏.在该宏中,您可以将文件名和行号作为属性添加到记录中(您可以使用操纵器).请注意,在这种情况下,您将无法在过滤器中使用这些属性,但您可能不需要这些属性.
我正在使用Boost.Log中的boost 1.54.0来查看它是否适用于我的应用程序.一般来说,我没有缓冲问题,所以我不打算打开auto_flush或其他任何东西......但是我注意到在我调用之前记录的消息fork()是重复的,我想知道它是否是因为它们是缓冲的,所以在复制过程映像时缓冲区会被复制,然后两个进程最终将它们的缓冲区副本写入日志文件......
所以基本上,我只想在我打电话之前在日志上做一次手动刷新,fork()以确保没有消息仍然在内存中.换句话说,我正在寻找一个类似于fflush(),.flush(),<< flush等我可以在升压日志使用.
我确实尝试使用<< flush日志,但我仍然得到重复的消息,所以我不是100%确定它是否正在刷新并且重复是由其他一些问题引起的,或者它是否以某种方式默默地忽略了<< flush...
编辑:
我环顾四周,发现升压日志不是叉安全的.所以我应该补充一点,我不是想在父进程和子进程中使用相同的日志.我有两个分叉场景 - 在一个,父母立即终止和孩子contineus(这应该是安全的),而在另一个,孩子应该打开自己的单独的日志文件,所以这应该是安全的...但我需要弄清楚如何关闭日志文件接收器,然后打开一个新文件(在不同的文件上).我想关闭水槽也可能是一种强制冲洗的方法......?
我对Boost.Log库有点新意,第一印象非常好,但有一件事已经耗费了很多时间,我无法解决.我想让Boost.Log立即将每条消息写入日志文件.我知道其他问题(I,II,III),但是他们没有帮助.考虑来自boost docs的这个例子,下一个代码是相同的,除了我设置auto_flush为true:
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
void init()
{
// Construct the sink
typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
// Add a stream to write log to
sink->locked_backend()->add_stream(
boost::make_shared< std::ofstream >("sample.log")); //1
sink->locked_backend()->auto_flush(true);
// Register the sink in the logging core
logging::core::get()->add_sink(sink);
}
int main(int, char*[])
{
init();
src::logger lg;
BOOST_LOG(lg) << "Hello …Run Code Online (Sandbox Code Playgroud) 我有一个广泛使用boost log 2.0的应用程序.现在,我想设置一些默认的标志为应用程序一样std::setprecision(std::numeric_limits<double>::digits10 + 1),std::scientific和std::left.但是我该怎么做?一种方法是在我的main函数的最开始创建一个记录器并创建一个虚拟日志消息.这将永久设置所需的标志.但有没有更好的方法来做到这一点?
编辑回复:"OP应显示实际代码."
我有一个全局的Logging单例,叫做L:
class L{
public:
enum severity_level
{
dddebug,
ddebug,
debug,
control,
iiinfo,
iinfo,
info,
result,
warning,
error,
critical
};
typedef boost::log::sources::severity_channel_logger<
severity_level, // the type of the severity level
std::string // the type of the channel name
> logger_t;
typedef boost::log::sinks::synchronous_sink< boost::log::sinks::text_ostream_backend > text_sink;
boost::shared_ptr< text_sink > sink_;
static L& get();
static boost::shared_ptr<text_sink> sink();
static double t0();
static double tElapsed();
private:
L();
double t0_p;
static std::string …Run Code Online (Sandbox Code Playgroud) 在使用Boost.Log时,我试图保留我的TimeStamp格式化程序,例如:
logging::add_file_log
(
keywords::file_name = "my.log",
keywords::format =
(
expr::stream
<< expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S")
<< "," << expr::attr< int >("Line")
<< " " << expr::attr< std::string >("File")
<< " " << logging::trivial::severity
<< " - " << expr::smessage
)
);
Run Code Online (Sandbox Code Playgroud)
据说我不能使用其他形式的格式化程序,因为我在转换"TimeStamp"为自定义格式时会遇到很多困难:
static void my_formatter(logging::record_view const& rec, logging::formatting_ostream& strm)
{
strm << logging::extract< boost::posix_time::ptime >("TimeStamp", rec);
Run Code Online (Sandbox Code Playgroud)
将输出类似于:2015-Jul-01 16:06:31.514053,而我只对以下内容感兴趣:"%Y-%m-%d %H:%M:%S".但是第一种形式非常难以使用,例如我无法将其expr::attr< std::string >转换为简单std::string的例如:
logging::add_file_log
(
keywords::file_name = "my.log",
keywords::format …Run Code Online (Sandbox Code Playgroud) 我现在正在尝试部署我的应用程序,它使用 Boost Log (Boost 1.58)。它是一个简单的控制台应用程序,在 Windows 7 中运行。日志记录在我的个人桌面上运行得非常好。
但是,当我将应用程序部署到 Win7 虚拟机时,它在我的第一个日志语句处崩溃:
boost::log::sources::severity_logger<SeverityLevel> slg;
BOOST_LOG_SEV(slg, SeverityLevel::Notification) << L"Application loaded"; // <-- Crash here
Run Code Online (Sandbox Code Playgroud)
日志目录已创建,但日志文件从未创建,并且应用程序崩溃。
我已经在我的 %APPDATA% 目录和我的文档目录中尝试了日志文件目录。
奇怪的是:当我以管理员身份运行该应用程序时,它可以工作!
所以这一定是一个权限问题,但我有这些文件夹的权限,所以......
有任何想法吗?
* 更多的 *
这是设置我的记录器的代码:
wstring appData = GetMyAppDataPath();
boost::shared_ptr< boost::log::sinks::synchronous_sink< boost::log::sinks::text_file_backend > > textFileSink(new boost::log::sinks::synchronous_sink< boost::log::sinks::text_file_backend >(to store rotated files
boost::log::keywords::file_name = "log_%7N.log", // file name pattern
boost::log::keywords::rotation_size = 16384 // rotation size, in characters
));
// Set up where the rotated files will be stored
textFileSink->locked_backend()->set_file_collector(boost::log::sinks::file::make_collector( …Run Code Online (Sandbox Code Playgroud) 在升级到升级1.60.0时,某些应用程序在Windows 7上使用MinGw 4.9.2构建时无法与升级日志链接.
我收到以下链接器错误:
undefined reference to `_imp___ZN5boost3log9v2_mt_nt67trivial6logger3getEv'
undefined reference to `_imp___ZN5boost3log9v2_mt_nt63aux15stream_providerIcE17allocate_compoundERNS1_6recordE'
undefined reference to `_imp___ZN5boost3log9v2_mt_nt63aux25unhandled_exception_countEv'
undefined reference to `_imp___ZN5boost3log9v2_mt_nt611record_view11public_data7destroyEPKS3_'
bad reloc address 0x1 in section `.text$_ZNK5boost4asio5error6detail13misc_category4nameEv[__ZNK5boost4asio5error6detail13misc_category4nameEv]'
Run Code Online (Sandbox Code Playgroud)
注意:BOOST_LOG_DYN_LINK定义为:
g++ -c -pipe -fno-keep-inline-dllexport -Wall -Wextra -Wpedantic -Ofast -std=c++1y -frtti -fexceptions -mthreads -DUNICODE -DLOGGING_ENABLED -DNTDDI_VERSION=NTDDI_WIN7 -D_WIN32_WINNT=_WIN32_WINNT_WIN7 -DBOOST_THREAD_USE_LIB=1 -DBOOST_LOG_DYN_LINK=1
Run Code Online (Sandbox Code Playgroud)
boost 1.60.0构建日志文件显示boost log和boost log_setup都没有任何错误或警告,包括一些无法链接的文件,例如:
gcc.compile.c++ bin.v2\libs\log\build\gcc-mingw-4.9.2\release\threading-multi\trivial.o
gcc.compile.c++ bin.v2\libs\log\build\gcc-mingw-4.9.2\release\threading-multi\unhandled_exception_count.o
Run Code Online (Sandbox Code Playgroud)
应用程序在Windows 7上使用boost 1.59.0和MinGw 4.9.2链接正常,并在Fedora 23上使用boost 1.60.0和gcc 5.1.1链接OK.
自从提升1.58.0以来,提升asio没有改变.那么在boost 1.59.0和boost 1.60.0之间的boost日志中有什么变化导致MinGw链接在Windows上失败?
我正在尝试为linux下的boost :: log添加彩色日志输出.我读了以下内容,并尝试了这个:
#define MY_LOG_ERROR() BOOST_LOG_TRIVIAL(error) << "\033[1;31"
MY_LOG_ERROR() << "This is an error log."
Run Code Online (Sandbox Code Playgroud)
但它给了我以下结果:
[2016-07-11 17:23:16.328435] [0x00007f15f03d6780] [错误] [1; 31这是一个错误日志.
如何正确添加彩色日志输出到boost :: log?
我能够为一个简单的结构定义输出流运算符,但不能为 std::array 定义输出流运算符。以下代码无法编译。出了什么问题,我该如何解决?
\n\n#include <array>\n#include <iostream>\n#include <boost/log/core.hpp>\n#include <boost/log/trivial.hpp>\n\nusing hash_t = std::array< unsigned char, 32 >;\n\nstd::ostream& operator<< ( std::ostream& os, hash_t const& arr )\n{\n os << "ole!";\n return os;\n}\n\nint main(int, char*[])\n{\n hash_t arr;\n std::cerr << arr << std::endl; // complies cleanly\n BOOST_LOG_TRIVIAL(debug) << arr; // Error\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n错误就来了。
\n\nGCC(增强 1.55,gcc-4.9.2):
\n\nIn file included from /usr/include/boost/log/sources/record_ostream.hpp:31:0,\n from /usr/include/boost/log/trivial.hpp:23,\n from trival.cpp:4:\n/usr/include/boost/log/utility/formatting_ostream.hpp: In instantiation of \xe2\x80\x98boost::log::v2s_mt_posix::basic_formatting_ostream<CharT, TraitsT, AllocatorT>& boost::log::v2s_mt_posix::operator<<(boost::log::v2s_mt_posix::basic_formatting_ostream<CharT, TraitsT, AllocatorT>&, const T&) [with CharT = char; TraitsT = …Run Code Online (Sandbox Code Playgroud) 我尝试构建这个示例并收到链接错误:
hekto@ubuntu:~$ g++ -g -Wall -std=c++11 -pthread -DBOOST_LOG_DYN_LINK -pthread sample.cpp -o sample -lboost_log -lboost_thread -lboost_system
/tmp/cc2EUizv.o: In function `boost::log::v2_mt_posix::basic_formatter<char> boost::log::v2_mt_posix::parse_formatter<char>(char const*)':
/usr/include/boost/log/utility/setup/formatter_parser.hpp:207: undefined reference to `boost::log::v2_mt_posix::basic_formatter<char> boost::log::v2_mt_posix::parse_formatter<char>(char const*, char const*)'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
如何克服呢?