单个写日志
# 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/support/date_time.hpp> int main ( ) { boost:: log:: add_file_log ( boost:: log:: keywords:: file_name = "example.log" , boost:: log:: keywords:: rotation_size = 10 * 1024 * 1024 , boost:: log:: keywords:: format = ( boost:: log:: expressions:: stream<< "[" << boost:: log:: expressions:: format_date_time < boost:: posix_time:: ptime > ( "TimeStamp" , "%Y-%m-%d %H:%M:%S.%f" ) << "]" << "[" << boost:: log:: trivial:: severity << "]" << " " << boost:: log:: expressions:: smessage) ) ; boost:: log:: core:: get ( ) -> set_filter ( boost:: log:: trivial:: severity >= boost:: log:: trivial:: debug) ; boost:: log:: add_common_attributes ( ) ; BOOST_LOG_TRIVIAL ( debug) << "Hello, Boost log!" ; return 0 ;
}
根据日志等级写日志
# 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/support/date_time.hpp> int main ( ) { boost:: log:: add_file_log ( boost:: log:: keywords:: file_name = "info.log" , boost:: log:: keywords:: filter = boost:: log:: trivial:: severity == boost:: log:: trivial:: info, boost:: log:: keywords:: format = ( boost:: log:: expressions:: stream<< "[" << boost:: log:: expressions:: format_date_time < boost:: posix_time:: ptime > ( "TimeStamp" , "%Y-%m-%d %H:%M:%S.%f" ) << "]" << "[" << boost:: log:: trivial:: severity << "]" << " " << boost:: log:: expressions:: smessage) ) ; boost:: log:: add_file_log ( boost:: log:: keywords:: file_name = "error.log" , boost:: log:: keywords:: filter = boost:: log:: trivial:: severity == boost:: log:: trivial:: error, boost:: log:: keywords:: format = ( boost:: log:: expressions:: stream<< "[" << boost:: log:: expressions:: format_date_time < boost:: posix_time:: ptime > ( "TimeStamp" , "%Y-%m-%d %H:%M:%S.%f" ) << "]" << "[" << boost:: log:: trivial:: severity << "]" << " " << boost:: log:: expressions:: smessage) ) ; boost:: log:: add_common_attributes ( ) ; BOOST_LOG_TRIVIAL ( info) << "This is an info message." ; BOOST_LOG_TRIVIAL ( error) << "This is an error message." ; return 0 ;
}
自定义过滤器
# 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/attributes/named_scope.hpp>
# include <boost/log/sources/record_ostream.hpp>
# include <boost/log/sources/severity_logger.hpp> namespace logging = boost:: log;
namespace attrs = boost:: log:: attributes;
namespace src = boost:: log:: sources;
namespace expr = boost:: log:: expressions;
BOOST_LOG_ATTRIBUTE_KEYWORD ( module , "Module" , std:: string) int main ( ) { logging:: add_file_log ( logging:: keywords:: file_name = "moduleA.log" , logging:: keywords:: filter = module == "ModuleA" , logging:: keywords:: format = ( expr:: stream<< "[" << expr:: format_date_time < boost:: posix_time:: ptime > ( "TimeStamp" , "%Y-%m-%d %H:%M:%S.%f" ) << "]" << "[" << logging:: trivial:: severity << "]" << "[" << module << "]" << " " << expr:: smessage) ) ; logging:: add_file_log ( logging:: keywords:: file_name = "moduleB.log" , logging:: keywords:: filter = module == "ModuleB" , logging:: keywords:: format = ( expr:: stream<< "[" << expr:: format_date_time < boost:: posix_time:: ptime > ( "TimeStamp" , "%Y-%m-%d %H:%M:%S.%f" ) << "]" << "[" << logging:: trivial:: severity << "]" << "[" << module << "]" << " " << expr:: smessage) ) ; logging:: add_common_attributes ( ) ; src:: severity_logger< logging:: trivial:: severity_level> lg; auto modulea= lg. add_attribute ( "Module" , attrs:: constant < std:: string> ( "ModuleA" ) ) ; BOOST_LOG_SEV ( lg, logging:: trivial:: info) << "This is a message from ModuleA." ; lg. remove_attribute ( modulea. first) ; lg. add_attribute ( "Module" , attrs:: constant < std:: string> ( "ModuleB" ) ) ; BOOST_LOG_SEV ( lg, logging:: trivial:: info) << "This is a message from ModuleB." ; return 0 ;
}