impulsar la escritura de registros

Registro de escritura único

#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;
}

Escribir registros según el nivel de registro

#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() {
    
    
    // 为 info 级别日志设置接收器
    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
        )
    );

    // 为 error 级别日志设置接收器
    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;
}

Filtros personalizados

#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() {
    
    
    // 为 ModuleA 设置接收器
    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
        )
    );

    // 为 ModuleB 设置接收器
    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;
}

Supongo que te gusta

Origin blog.csdn.net/weixin_42295969/article/details/132235175
Recomendado
Clasificación