Boost Log library learns to use three (output log to file)

Source: Internet
Author: User
Tags file size generator local time log log
problems with the program log.

The above examples are the output of log information to the console, the actual application is not commonly used, the most commonly used situation is that we will log information output to a text file, so that we can view later, and the program does not run when the log information will not be lost. After exporting to a file, there are some problems. If the program has been running, the log information will be unlimited record down. Too much time to clean up the log, or log files that are larger than the deleted files are re-recorded. I want to change the format of the record. How to name the log file to facilitate our management and so on. boost log log to file example code Example 1:

Add a file name for the output log information to output the log information to the Sample.log file

#include "stdafx.h" #include <stdio.h> #include <boost/log/core.hpp> #include <boost/log/trivial.hpp > #include <boost/log/expressions.hpp> #include <boost/log/sinks/text_file_backend.hpp> #include < boost/log/utility/setup/file.hpp> #include <boost/log/utility/setup/common_attributes.hpp> #include < Boost/log/sources/severity_logger.hpp> #include <boost/log/sources/record_ostream.hpp> namespace logging =
Boost::log;
namespace src = boost::log::sources;
namespace sinks = Boost::log::sinks;

namespace keywords = boost::log::keywords;
namespace Logging=boost::log;
namespace src = boost::log::sources;
namespace sinks = Boost::log::sinks;

namespace keywords = boost::log::keywords;
    void Init1 () {Logging::add_file_log ("Sample.log");
Logging::core::get ()->set_filter (logging::trivial::severity>=logging::trivial::info);
    } int _tmain (int argc, _tchar* argv[]) {init1 (); Boost_log_trivial (Trace) << "A Trace severityMessage ";
    Boost_log_trivial (Debug) << "A debug Severity Message";
    Boost_log_trivial (Info) << "An informational severity message";
    Boost_log_trivial (warning) << "A warning severity message";
    Boost_log_trivial (Error) << "An error severity message";

    Boost_log_trivial (fatal) << "A fatal severity message";
    System ("pause");
return 0; }

Note one: About sink
1. Sometimes trivial logging doesn ' t provide enough flexibility. For example, one could want a more sophisticated logic's log processing, rather than simply printing it on the console. in order to customize the construct logging sinks and registers them with the logging core. This should normally is done by once somewhere in the startup code of your application.
2. Now we are experiencing the concept of sink. Sink determines where the log information is exported. In the previous two articles, Sink was not added because boost log uses the console as the default sink. Now, because the Add_file_log function is called, the default sink is not valid and is replaced by the file sink once the sink has been added. To set up sink, you need to add it to the core

    Logging::add_file_log ("Sample.log");

Note two: Add_file_log ()
1. As the name implies, the function initializes a logging sink that stores log records into a text file. The function also accepts a number of customization options, such as the file rotation interval and size limits.
2. This function will be applied in more detail in the next example

Operation Result:
As you can see, the log information is recorded in the Sample.log file.
code Example 2:

Set the log file name, file size limit, output information format and so on.

#include "stdafx.h" #include <stdio.h> #include <boost/log/core.hpp> #include <boost/log/trivial.hpp > #include <boost/log/expressions.hpp> #include <boost/log/sinks/text_file_backend.hpp> #include < boost/log/utility/setup/file.hpp> #include <boost/log/utility/setup/common_attributes.hpp> #include < Boost/log/sources/severity_logger.hpp> #include <boost/log/sources/record_ostream.hpp> namespace logging =
Boost::log;
namespace src = boost::log::sources;
namespace sinks = Boost::log::sinks;

namespace keywords = boost::log::keywords;
namespace Logging=boost::log;
namespace src = boost::log::sources;
namespace sinks = Boost::log::sinks;

namespace keywords = boost::log::keywords;                                        void Init () {logging::add_file_log (keywords::file_name = "Sample_%n.log",                                   /*< file name pattern >*/keywords::rotation_size = 10 * 1024 * 1024, /*< RotaTe files every MiB ... >*/keywords::time_based_rotation = sinks::file::rotation_at_time_point (0, 0, 0),/*&L T ... or at midnight >*/keywords::format = "[%timestamp%]:%Message%"/*< log

    Record format >*/);
Logging::core::get ()->set_filter (logging::trivial::severity>=logging::trivial::info);
    } int _tmain (int argc, _tchar* argv[]) {init ();

    Logging::add_common_attributes ();
    using namespace logging::trivial;

    Src::severity_logger<severity_level> LG;
    Boost_log_sev (LG, Trace) << "A trace severity message";
    Boost_log_sev (LG, Debug) << "A debug Severity Message";
    Boost_log_sev (LG, info) << "An informational severity message";
    Boost_log_sev (LG, warning) << "A warning severity message";
    Boost_log_sev (LG, error) << "An error severity message";

    Boost_log_sev (LG, fatal) << "A fatal severity message";
    System ("pause");
return 0;} 

Note One: Keywords

keywords Description
file_name The keyword allows to pass log file name the rotating file stream methods
Rotation_size The keyword allows to pass maximum log file size to the file sink
Time_based_rotation The keyword allows to pass time-based file rotation predicate to the file sink backend
Format The keyword for passing format specifiers to functions

Note two: Add_common_attributes ()
The function adds commonly used attributes to the logging system. Specifically, the following attributes is registered globally:

attributes Description
LineID Logging records counter with value type unsigned int
TimeStamp local time generator with value type boost::p osix_time::p time
ProcessID Current process identifier with value type Attributes::current_process_id::value_type
ThreadID In multithreaded builds, current thread identifier with value type Attributes::current_thread_id::value_type

Operation Result:
Note the change in file name. And the change in the output format of the log file.
Boost Log related source code

The following code is Keywords::format related code

/* * Copyright Andrey Semashev 2007-2015.
 * Distributed under the Boost software License, Version 1.0.
 * (see accompanying file license_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) *//*! * \file keywords/format.hpp * \author Andrey Semashev * \date 14.03.2009 * * The header contains the \c format key
 Word declaration. */#ifndef boost_log_keywords_format_hpp_included_ #define BOOST_LOG_KEYWORDS_FORMAT_HPP_INCLUDED_ #include < boost/parameter/keyword.hpp> #include <boost/log/detail/config.hpp> #ifdef boost_has_pragma_once #pragma Once #endif namespace boost {Boost_log_open_namespace namespace keywords {//! The keyword for passing format specifiers to functions Boost_parameter_keyword (tag, format)//Here is the focus}//Namespace key 
Words boost_log_close_namespace//NAMESPACE LOG}//NAMESPACE BOOST #endif//Boost_log_keywords_format_hpp_included_ 

The following section is COMMON_ATTRIBUTES.HPP source code

/* Copyright Andrey Semashev 2007-2015.
 * Distributed under the Boost software License, Version 1.0.
 * (see accompanying file license_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) *//*! * \file common_attributes.hpp * \author Andrey Semashev * \date 16.05.2008 * * The header contains implementation
 of convenience functions for registering commonly used attributes. */#ifndef boost_log_utility_setup_common_attributes_hpp_included_ #define Boost_log_utility_setup_common_ Attributes_hpp_included_ #include <iostream> #include <boost/log/detail/config.hpp> #include <boost/ log/core/core.hpp> #include <boost/log/attributes/clock.hpp> #include <boost/log/attributes/ counter.hpp> #include <boost/log/attributes/current_process_id.hpp> #if!defined (boost_log_no_threads) # Include <boost/log/attributes/current_thread_id.hpp> #endif #include <boost/log/detail/default_attribute_ Names.hpp> #include &Lt;boost/log/detail/header.hpp> #ifdef boost_has_pragma_once #pragma ONCE #endif namespace Boost {Boost_log_open_nam
 ESPACE/*! * \brief Simple Attribute Initialization routine * * The function adds commonly used attributes to the logging system. Specifically, the following * attributes is registered globally: * * \li lineid-logging Records counter with value T ype <tt>unsigned int</tt> * \li timestamp-local Time generator with value type <tt>boost::p osix_time: :p time</tt> * \li processid-current process identifier with value type * &LT;TT&GT;ATTRIBUTES::CURRENT_PROCE ss_id::value_type</tt> * \li threadid-in multithreaded builds, current thread identifier with * value type & lt;tt>attributes::current_thread_id::value_type</tt> */inline void add_common_attributes () {shared_ptr&lt ;   Core > Pcore = Core::get (); Get Single instance//add several default attribute values Pcore->add_global_attribute (aux::d efault_attribute_names::lINE_ID (), attributes::counter< unsigned int > (1));
    Pcore->add_global_attribute (aux::d efault_attribute_names::timestamp (), Attributes::local_clock ()); Pcore->add_global_attribute (aux::d efault_attribute_names::p rocess_id (), Attributes::current_process_
ID ());
        #if!defined (boost_log_no_threads) pcore->add_global_attribute (aux::d efault_attribute_names::thread_id (),
ATTRIBUTES::CURRENT_THREAD_ID ());

#endif} boost_log_close_namespace//NAMESPACE LOG}//NAMESPACE BOOST #include <boost/log/detail/footer.hpp>
 #endif//Boost_log_utility_setup_common_attributes_hpp_included_

The following is the DEFAULT_ATTRIBUTE_NAMES.HPP source code, followed by a source view

/* Copyright Andrey Semashev 2007-2015.
 * Distributed under the Boost software License, Version 1.0.
 * (see accompanying file license_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) *//*! * \file default_attribute_names.hpp * \author Andrey Semashev * \date 15.01.2012 * * \brief This header is the Bo Ost. Log Library Implementation, see the library documentation * at Http://www.boost.org/doc/libs/release/libs/log/doc
 /html/index.html. */#ifndef boost_log_detail_default_attribute_names_hpp_included_ #define Boost_log_detail_default_attribute_names _hpp_included_ #include <boost/log/detail/config.hpp> #include <boost/log/attributes/attribute_name.hpp > #include <boost/log/detail/header.hpp> #ifdef boost_has_pragma_once #pragma ONCE #endif namespace Boost {BO
Ost_log_open_namespace NAMESPACE aux {NAMESPACE default_attribute_names {boost_log_api attribute_name severity (); Boost_log_api Attribute_name ChanNel ();
BOOST_LOG_API attribute_name message ();    Boost_log_api attribute_name line_id ();
The following four is the attribute name used in the source file above Boost_log_api attribute_name timestamp ();
Boost_log_api attribute_name process_id ();

Boost_log_api attribute_name thread_id (); }//Namespace Default_attribute_names}//Namespace aux Boost_log_close_namespace//namespace LOG}//Namespace Boo
 St #include <boost/log/detail/footer.hpp> #endif//Boost_log_detail_default_attribute_names_hpp_included_

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.