Repo-Init
 
Loading...
Searching...
No Matches
Logger.cpp
Go to the documentation of this file.
1#include "logging/Logger.hpp"
2
3#include "Version.h"
4#include "logging/Loki.hpp"
5#include "logging/Sentry.hpp"
6
7#include <spdlog/sinks/dup_filter_sink.h>
8#include <spdlog/sinks/stdout_color_sinks.h>
9#include <spdlog/sinks/syslog_sink.h>
10#include <spdlog/spdlog.h>
11
12#include <unistd.h>
13
14// Log filtering interval for duplicate filtering in seconds
15constexpr int LOG_FILTER_SECS = 5;
16// Log flush interval in seconds
17constexpr int LOG_FLUSH_SECS = 2;
18
19MainLogger::MainLogger(const std::string &lokiAddr, const std::string &sentryAddr)
20{
21 spdlog::set_level(spdlog::level::off);
22
23 // Prepare spdlog loggers
24 auto dupFilter = std::make_shared<spdlog::sinks::dup_filter_sink_mt>(std::chrono::seconds(LOG_FILTER_SECS));
25 if (getppid() != 1) // Disable stdout output for systemd
26 {
27 dupFilter->add_sink(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
28 }
29 dupFilter->add_sink(std::make_shared<spdlog::sinks::syslog_sink_mt>(PROJECT_NAME, LOG_USER, 0, false));
30 dupFilter->add_sink(std::make_shared<spdlog::sinks::loki_api_sink_mt>(lokiAddr));
31 dupFilter->add_sink(std::make_shared<spdlog::sinks::sentry_api_sink_mt>(sentryAddr));
32
33 // Register main logger
34 _mainLogger = std::make_shared<spdlog::logger>(PROJECT_NAME, dupFilter);
35
36 spdlog::set_default_logger(_mainLogger);
37 spdlog::flush_every(std::chrono::seconds(LOG_FLUSH_SECS));
38
39#ifdef NDEBUG
40 spdlog::set_level(spdlog::level::warn);
41#else
42 spdlog::set_level(spdlog::level::info);
43#endif
44
45 PRINT_VERSION();
46}
47
49{
50 spdlog::info("Goodbye!");
51 _mainLogger->flush();
52}
constexpr int LOG_FILTER_SECS
Definition Logger.cpp:15
constexpr int LOG_FLUSH_SECS
Definition Logger.cpp:17
~MainLogger()
Definition Logger.cpp:48
MainLogger(const std::string &lokiAddr, const std::string &sentryAddr)
Definition Logger.cpp:19
std::shared_ptr< spdlog::logger > _mainLogger
Definition Logger.hpp:10