コード例 #1
0
ファイル: LogManager.cpp プロジェクト: tiandian/cpp-proto-net
void CLogManager::Init()
{
	try
	{
		// The first thing we have to do to get using the library is
		// to set up the logging sinks - i.e. where the logs will be written to.
		logging::init_log_to_console(std::clog, keywords::format = "%TimeStamp%: %_%");

		// One can also use lambda expressions to setup filters and formatters
		logging::init_log_to_file
			(
			keywords::file_name = "log\\gateway_%N.txt",       // file name pattern
			keywords::rotation_size = 1024 * 1024,             // rotate files every 1 MiB...
			// ...or at midnight
			keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),

			keywords::filter = flt::attr< severity_level >("Severity", std::nothrow) >= trace,
			keywords::format = fmt::format("%1% [%2%] <%3%> %4%")
			% fmt::date_time("TimeStamp", std::nothrow)
			% fmt::time_duration("Uptime", std::nothrow)
			% fmt::attr< severity_level >("Severity", std::nothrow)
			% fmt::message()
			);

		// Also let's add some commonly used attributes, like timestamp and record counter.
		logging::add_common_attributes();

		// Let's pretend we also want to profile our code, so add a special timer attribute.
		slg.add_attribute("Uptime", boost::make_shared< attrs::timer >());

		m_isEnabled = true;
		cout << "Log initialized successfully" << endl;
	}
	catch(exception& e)
	{
		cerr << "Failed to initialize log due to " << e.what() << endl;
	}
}
コード例 #2
0
int CBoostLogger::initBoostLog()
{
	std::string strLogPath = m_strLogPath;
	std::string strLogFileName = m_strLogFileName;
	int nMaxLogSize = 1*1024*1024*1024;   //maxth log size 10GB
	int nMinFreeSpace = 100*1024*1024;  //min free size


	shared_ptr< sinks::synchronous_sink< sinks::text_file_backend > > file_sink =
		logging::add_file_log
		(
		keywords::file_name=strLogFileName,
		//keywords::file_name="Server_LOG_%Y-%m-%d_%H_%M_%S_%f_%4N.log",      //log file name  eg.sample_2013-11-27_17_26_49_750319_0001.log
		keywords::filter = expr::attr< trivial::severity_level >("Severity") >= m_nLogLevel,//log level
		keywords::rotation_size=20*1024*1024,  //log file size
		keywords::time_based_rotation=sinks::file::rotation_at_time_point(0,0,0)//create log file everyday
		);

	//[RecordID] [TimeStamp] [ThreadID] <Severity> [file:line] msg
	logging::formatter formatterLog = expr::stream
		<<" "<<"["<<expr::attr< unsigned int >("RecordID")<<"]"
		<<" "<<"["<<expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d, %H:%M:%S.%f")<<"]"
		//<<" "<<expr::format_named_scope("Scope", keywords::format = "[%f:%l] [%n]") 
		<<" "<<expr::format_named_scope("Scope", keywords::format = "") 
		<<" "<<"["<<expr::attr< attrs::current_thread_id::value_type >("ThreadID")<<"]"
		//<<" "<< default_attribute_names::line_id<<" "
		<<" "<<"<" << expr::attr< trivial::severity_level >("Severity")<< ">"
		<<" " <<expr::message;


	file_sink->set_formatter(formatterLog);

	//set all log path
	file_sink->locked_backend()->set_file_collector(
		sinks::file::make_collector(
		keywords::target=strLogPath,   //path  		
		//keywords::target="ALL_Server_LOG",   //path     
		keywords::max_size = nMaxLogSize,    //maxth log size 10GB
		keywords::min_free_space = nMinFreeSpace  //min free size
		));


	//logging::core::get()->set_filter(expr::attr< trivial::severity_level >("Severity") >= trivial::debug);//TODO, Test this

	file_sink->set_filter(expr::attr< trivial::severity_level >("Severity") >= m_nLogLevel);   //log level trivial::debug
	file_sink->locked_backend()->scan_for_files();
	file_sink->locked_backend()->auto_flush(true);


	logging::core::get()->add_global_attribute("Scope",attrs::named_scope());
	logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
	logging::core::get()->add_global_attribute("ThreadID", attrs::current_thread_id());
	logging::core::get()->add_global_attribute("RecordID", attrs::counter< unsigned int >());


	// Also let's add some commonly used attributes, like timestamp and record counter.
	logging::add_common_attributes();

	BOOST_LOG_FUNCTION();//

	// Now, let's try logging with severity
	//src::severity_logger< trivial::severity_level > g_logHandleFile;

	// Let's pretend we also want to profile our code, so add a special timer attribute.
	g_logHandleFile.add_attribute("Uptime", attrs::timer());

	return 0;
}