I am new to CPP boost. I want to introduce logging in my CPP project
structure of code in 3 .hpp file contains classes which use logger to log info.
out of 3 classes 3 only 3 objects are created in main cpp file. in all .hpp files and .cpp i have included log using logger.hpp when i run the code no logs are getting dumped into log file but logs are visible on console.
this is logger code
#pragma once
#include <iostream>
#include <stdexcept>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/support/date_time.hpp>
#define BOOST_LOG_DYN_LINK 1
class Logger
{
public:
public:
static void init()
{
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;
namespace expr = boost::log::expressions;
keywords::auto_flush = true ;
// Create a file sink
auto file_sink = logging::add_file_log(
keywords::file_name = "mangesh.log", // Filename pattern
keywords::rotation_size = 10 * 1024 * 1024, // Rotate files every 10 MiB
keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), // Rotate daily at midnight
keywords::format = expr::stream
<< expr::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S.%f")
<< " | " << logging::trivial::severity
<< " | " << expr::smessage
);
// Also log to console
auto console_sink = logging::add_console_log(std::cout, keywords::format = "%TimeStamp% | %Severity% | %Message%");
// Set a filter to log all severities
file_sink->set_filter(logging::trivial::severity >= logging::trivial::trace);
console_sink->set_filter(logging::trivial::severity >= logging::trivial::trace);
// Add common attributes like TimeStamp
logging::add_common_attributes();
}
static void logging_trace(std::string function,std::string message, int line, std::string file)
{
try
{
BOOST_LOG_TRIVIAL(trace) << file << "|" << function << " | " << line << " | " << message;
}
catch (const std::exception &ex)
{
log_exception("logging_trace", ex.what(), line, file);
}
}
static void logging_debug(std::string function,std::string message, int line, std::string file)
{
try
{
BOOST_LOG_TRIVIAL(debug) << file << "|" << function << " | " << line << " | " << message;
}
catch (const std::exception &ex)
{
log_exception("logging_debug", ex.what(), line, file);
}
}
static void logging_info(std::string function,std::string message, int line, std::string file)
{
try
{
BOOST_LOG_TRIVIAL(info) << file << "|" << function << " | " << line << " | " << message;
}
catch (const std::exception &ex)
{
log_exception("logging_info", ex.what(), line, file);
}
}
static void logging_warning(std::string function,std::string message, int line, std::string file)
{
try
{
BOOST_LOG_TRIVIAL(warning) << file << "|" << function << " | " << line << " | " << message;
}
catch (const std::exception &ex)
{
log_exception("logging_warning", ex.what(), line, file);
}
}
static void logging_error(std::string function,std::string message, int line, std::string file)
{
try
{
BOOST_LOG_TRIVIAL(error) << file << "|" << function << " | " << line << " | " << message;
}
catch (const std::exception &ex)
{
log_exception("logging_error", ex.what(), line, file);
}
}
static void logging_fatal(std::string function,std::string message, int line, std::string file)
{
try
{
BOOST_LOG_TRIVIAL(fatal) << file << "|" << function << " | " << line << " | " << message;
}
catch (const std::exception &ex)
{
log_exception("logging_fatal", ex.what(), line, file);
}
}
private:
static void log_exception(const std::string &function, const std::string &error, int line, const std::string &file)
{
BOOST_LOG_TRIVIAL(error) << "Exception in " << function << " | " << file << " | " << line << " | " << error;
}
};```