boost log的简单封装

基于boost log的简单封装

//
// Created by kqbi on 2020/6/1.
//

#ifndef LOGGER_H
#define LOGGER_H

#include <stdexcept>
#include <string>
#include <iostream>
#include <fstream>

#include <boost/log/common.hpp>
#include <boost/log/sinks.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/attributes/named_scope.hpp>

    class Logger {
    public:
        typedef boost::log::sinks::synchronous_sink<boost::log::sinks::text_file_backend> file_sink;
        enum LoggerType {
            console = 0,
            file,
        };


        Logger() {}

        ~Logger() {}

        static Logger &Instance();

        bool Init(std::string fileName, int type, int level, int maxFileSize, int maxBackupIndex);

        boost::log::sources::severity_logger<boost::log::trivial::severity_level> _logger;

    };

#define S_LOG_TRACE(logEvent)  BOOST_LOG_FUNCTION(); BOOST_LOG_SEV(Logger::Instance()._logger, boost::log::trivial::trace) << logEvent;

#define S_LOG_DEBUG(logEvent)  BOOST_LOG_FUNCTION(); BOOST_LOG_SEV(Logger::Instance()._logger, boost::log::trivial::debug) << logEvent;

#define S_LOG_INFO(logEvent)   BOOST_LOG_FUNCTION(); BOOST_LOG_SEV(Logger::Instance()._logger, boost::log::trivial::info) << logEvent;

#define S_LOG_WARN(logEvent)   BOOST_LOG_FUNCTION(); BOOST_LOG_SEV(Logger::Instance()._logger, boost::log::trivial::warning) << logEvent;

#define S_LOG_ERROR(logEvent)  BOOST_LOG_FUNCTION(); BOOST_LOG_SEV(Logger::Instance()._logger, boost::log::trivial::error) << logEvent;

#define S_LOG_FATAL(logEvent)  BOOST_LOG_FUNCTION(); BOOST_LOG_SEV(Logger::Instance()._logger, boost::log::trivial::fatal) << logEvent;

#endif //IDAS_LOGGER_H

//
// Created by kqbi on 2020/6/1.
//

#include "Logger.h"
#include <boost/log/expressions.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/support/date_time.hpp>

    Logger &Logger::Instance() {
        static Logger log;
        return log;
    }

    bool Logger::Init(std::string fileName, int type, int level, int maxFileSize, int maxBackupIndex) {
        boost::log::formatter formatter =
                boost::log::expressions::stream
                        << "["
                        << boost::log::expressions::format_date_time<boost::posix_time::ptime>("TimeStamp",
                                                                                               "%Y-%m-%d %H:%M:%S.%f") /*.%f*/
                        << "|"
                        << boost::log::expressions::attr<boost::log::attributes::current_thread_id::value_type>(
                                "ThreadID")
                        << "]["
                        << boost::log::expressions::format_named_scope("Scope",
                                                                       boost::log::keywords::format = "(%f:%l)",
                                                                       boost::log::keywords::iteration = boost::log::expressions::reverse,
                                                                       boost::log::keywords::depth = 1)
                        << "]["
                        << boost::log::expressions::attr<boost::log::trivial::severity_level>("Severity")
                        << "] "
                        << boost::log::expressions::smessage;

        switch (type) {
            case console: {
                auto consoleSink = boost::log::add_console_log();
                consoleSink->set_formatter(formatter);
                boost::log::core::get()->add_sink(consoleSink);
            }
                break;
            case file: {
                boost::shared_ptr<file_sink> fileSink(new file_sink(
                        boost::log::keywords::file_name = fileName,                       // file name pattern
                        boost::log::keywords::target_file_name = "%Y%m%d_%H%M%S_%N.log",   // file name pattern
                        boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(0,
                                                                                                                    0,
                                                                                                                    0),
                        boost::log::keywords::rotation_size =
                                maxFileSize * 1024 * 1024,                         // rotation size, in characters
                        boost::log::keywords::open_mode = std::ios::out | std::ios::app
                ));

                fileSink->locked_backend()->set_file_collector(boost::log::sinks::file::make_collector(
                        boost::log::keywords::target = "logs",        //folder name.
                        boost::log::keywords::max_size = maxFileSize * maxBackupIndex * 1024 *
                                                         1024,    //The maximum amount of space of the folder.
                        boost::log::keywords::min_free_space = 10 * 1024 * 1024,  //Reserved disk space minimum.
                        boost::log::keywords::max_files = maxBackupIndex
                ));

                fileSink->set_formatter(formatter);
                fileSink->locked_backend()->scan_for_files();
                fileSink->locked_backend()->auto_flush(true);
                boost::log::core::get()->add_sink(fileSink);
            }
                break;
            default: {
                auto consoleSink = boost::log::add_console_log();
                consoleSink->set_formatter(formatter);
                boost::log::core::get()->add_sink(consoleSink);
            }
                break;
        }
        boost::log::add_common_attributes();
        boost::log::core::get()->add_global_attribute("Scope", boost::log::attributes::named_scope());
        boost::log::core::get()->set_filter(
                boost::log::trivial::severity >= level
        );
        return true;
    }

效果如下
[2020-06-15 14:32:49.181031|threadid][(file:lineid)][level] 1234

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值