[root@localhost log]# cat config.ini
#comments are allowed. Comment line begins with the ‘#‘ character
# and spans until the end of the line.
# Logging core settings section. May be omitted if no parameters specified within it.
[Core]
DisableLogging=false
Filter="%Severity% > debug"
# Sink settings sections
[Sinks.MySink1]
# Sink destination type
Destination=TextFile
FileName="MySink1_%Y-%m-%d_%H-%M-%S_%N.log"
# Sink-specific filter. Optional, by default no filter is applied.
Filter="%Channel% contains \"MySink1\" and %Severity% > info "
# Formatter string. Optional, by default only log record message text is written.
Format="<%TimeStamp%>-<%Severity%>-<%Channel%>:%Message%"
# The flag shows whether the sink should be asynchronous
Asynchronous=false
# Enables automatic stream flush after each log record.
AutoFlush=true
RotationSize=5000000
[Sinks.MySink2]
# Sink destination type
Destination=TextFile
FileName="MySink2_%Y-%m-%d_%H-%M-%S_%N.log"
# Sink-specific filter. Optional, by default no filter is applied.
Filter="%Channel% contains \"MySink2\""
# Formatter string. Optional, by default only log record message text is written.
Format="<%TimeStamp%>-<%Severity%>-<%Channel%>:%Message%"
# The flag shows whether the sink should be asynchronous
Asynchronous=false
# Enables automatic stream flush after each log record.
AutoFlush=true
RotationSize=5000000
[Sinks.Console]
# Sink destination type
Destination=Console
# Formatter string. Optional, by default only log record message text is written.
Format="%TimeStamp%-[%Severity%]-[%ProcessID%]-[%ThreadID%]-%Channel%:%LineID%:%Message%"
# The flag shows whether the sink should be asynchronous
Asynchronous=false
# Enables automatic stream flush after each log record.
AutoFlush=true
[root@localhost src]# cat main.cpp
#include <boost/date_time/posix_time/posix_time_types.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/common_attributes.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/sources/channel_feature.hpp>
#include <boost/log/sources/channel_logger.hpp>
#include <string>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/log/common.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/sinks.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/sources/severity_channel_logger.hpp>
#include <boost/log/utility/setup/settings.hpp>
#include <boost/log/utility/setup/from_stream.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <fstream>
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace keywords = boost::log::keywords;
namespace attrs = boost::log::attributes;
namespace sinks = boost::log::sinks;
using namespace std;
int main(int, char*[]) {
logging::add_common_attributes();
using namespace logging::trivial;
src::severity_channel_logger<severity_level,std::string> moduleOneLogger(keywords::channel = "MySink1");
src::severity_channel_logger<severity_level,std::string> moduleTwoLogger(keywords::channel = "MySink2");
logging::register_simple_formatter_factory<severity_level, char>("Severity");
logging::register_simple_filter_factory<severity_level, char>("Severity");
std::ifstream file("config.ini");
logging::init_from_stream(file);
BOOST_LOG_SEV(moduleOneLogger, trace)<< "A trace severity message";
BOOST_LOG_SEV(moduleOneLogger, debug)<< "A debug severity message";
BOOST_LOG_SEV(moduleOneLogger, info)<< "An informational severity message";
BOOST_LOG_SEV(moduleOneLogger, warning)<< "A warning severity message";
BOOST_LOG_SEV(moduleOneLogger, error)<< "An error severity message";
BOOST_LOG_SEV(moduleOneLogger, fatal)<< "A fatal severity message";
BOOST_LOG_SEV(moduleTwoLogger, trace)<< "A trace severity message";
BOOST_LOG_SEV(moduleTwoLogger, debug)<< "A debug severity message";
BOOST_LOG_SEV(moduleTwoLogger, info)<< "An informational severity message";
BOOST_LOG_SEV(moduleTwoLogger, warning)<< "A warning severity message";
BOOST_LOG_SEV(moduleTwoLogger, error)<< "An error severity message";
BOOST_LOG_SEV(moduleTwoLogger, fatal)<< "A fatal severity message";
return 0;
}
[root@localhost src]# more Makefile
CXXFLAGS = -O2 -g -Wall -fmessage-length=0
MODULE=log
OBJS_DIR=../objs/$(MODULE)
SUB_OBJS_DIR_FOR_TEST=$(OBJS_DIR)/testcase
OBJS+= \
$(OBJS_DIR)/main.o \
INCLUDES=\
-I../include \
-I../3rd/gtest-1.6.0/include \
-I../3rd
LIBS=\
-L../lib \
-L../3rd/gtest-1.6.0/lib -lgtest \
-lssl \
-lboost_log_setup \
-lboost_log \
-lboost_filesystem \
-lboost_thread \
-lboost_date_time \
-lboost_system \
-lrt
TARGET = $(OBJS_DIR)/log
.PHONY:all clean $(TARGET)
all: echomsg clean checkdir $(TARGET)
echo "complete"
echomsg:
echo $(OBJS)
$(TARGET):$(OBJS)
$(CXX) -o $(TARGET) $(OBJS) $(LIBS)
$(OBJS_DIR)/%.o:%.cpp
$(CXX) $(CXXFLAGS) -c -o $@ $^ $(INCLUDES)
checkdir:
echo "!run checkdir"
@if test ! -d $(SUB_OBJS_DIR_FOR_TEST) ; \
then \
mkdir -p $(SUB_OBJS_DIR_FOR_TEST) ; \
fi
.PHONY:clean
clean:
rm -f $(OBJS) $(TARGET)
boost log 配置实现多模块多文件,布布扣,bubuko.com
boost log 配置实现多模块多文件
原文:http://blog.csdn.net/jiafu1115/article/details/20048789