userver: userver/logging/component.hpp Source File
Loading...
Searching...
No Matches
component.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/logging/component.hpp
4/// @brief @copybrief components::Logging
5
6#include <string>
7#include <unordered_map>
8
9#include <userver/alerts/storage.hpp>
10#include <userver/components/component_fwd.hpp>
11#include <userver/components/raw_component_base.hpp>
12#include <userver/concurrent/async_event_source.hpp>
13#include <userver/os_signals/component.hpp>
14
15#include <userver/rcu/rcu_map.hpp>
16#include <userver/utils/fast_pimpl.hpp>
17#include <userver/utils/periodic_task.hpp>
18#include <userver/utils/statistics/entry.hpp>
19#include <userver/utils/statistics/writer.hpp>
20
21#include "logger.hpp"
22
23USERVER_NAMESPACE_BEGIN
24
25namespace logging::impl {
26class TpLogger;
27class TcpSocketSink;
28} // namespace logging::impl
29
30namespace components {
31
32// clang-format off
33
34/// @ingroup userver_components
35///
36/// @brief %Logging component
37///
38/// Allows to configure the default logger and/or additional loggers for your
39/// needs.
40///
41/// ## Static options:
42/// Name | Description | Default value
43/// ---- | ----------- | -------------
44/// file_path | path to the log file | -
45/// level | log verbosity | info
46/// format | log output format, either `tskv` or `ltsv` | tskv
47/// flush_level | messages of this and higher levels get flushed to the file immediately | warning
48/// message_queue_size | the size of internal message queue, must be a power of 2 | 65536
49/// overflow_behavior | message handling policy while the queue is full: `discard` drops messages, `block` waits until message gets into the queue | discard
50/// testsuite-capture | if exists, setups additional TCP log sink for testing purposes | {}
51/// fs-task-processor | task processor for disk I/O operations for this logger | fs-task-processor of the loggers component
52///
53/// ### Logs output
54/// You can specify logger output, in `file_path` option:
55/// - Use `@stdout` to write your logs to standard output stream;
56/// - Use `@stderr` to write your logs to standard error stream;
57/// - Use `@null` to suppress sending of logs;
58/// - Use `%file_name%` to write your logs in file. Use USR1 signal or `OnLogRotate` handler to reopen files after log rotation;
59/// - Use `unix:%socket_name%` to write your logs to unix socket. Socket must be created before the service starts and closed by listener after service is shut down.
60///
61/// ### testsuite-capture options:
62/// Name | Description | Default value
63/// ---- | ----------- | -------------
64/// host | testsuite hostname, e.g. localhost | -
65/// port | testsuite port | -
66///
67/// ## Static configuration example:
68///
69/// @snippet components/common_component_list_test.cpp Sample logging component config
70///
71/// `default` section configures the default logger for LOG_*.
72
73// clang-format on
74
75class Logging final : public RawComponentBase {
76public:
77 /// @ingroup userver_component_names
78 /// @brief The default name of components::Logging component
79 static constexpr std::string_view kName = "logging";
80
81 /// The component constructor
82 Logging(const ComponentConfig&, const ComponentContext&);
83 ~Logging() override;
84
85 /// @brief Returns a logger by its name
86 /// @param name Name of the logger
87 /// @returns Pointer to the Logger instance
88 /// @throws std::runtime_error if logger with this name is not registered
89 logging::LoggerPtr GetLogger(const std::string& name);
90
91 /// @brief Sets a logger
92 /// @param name Name of the logger
93 /// @param logger Logger to set
94 void SetLogger(const std::string& name, logging::LoggerPtr logger);
95
96 /// @brief Returns a logger by its name
97 /// @param name Name of the logger
98 /// @returns Pointer to the Logger instance, or `nullptr` if not registered
99 logging::LoggerPtr GetLoggerOptional(const std::string& name);
100
101 void StartSocketLoggingDebug(const std::optional<logging::Level>& log_level);
102 void StopSocketLoggingDebug(const std::optional<logging::Level>& log_level);
103
104 /// Reopens log files after rotation
106 void TryReopenFiles();
107
108 void WriteStatistics(utils::statistics::Writer& writer) const;
109
110 static yaml_config::Schema GetStaticConfigSchema();
111
112private:
113 void Init(const ComponentConfig&, const ComponentContext&);
114 void Stop() noexcept;
115
116 void FlushLogs();
117
118 engine::TaskProcessor* fs_task_processor_{nullptr};
119 std::unordered_map<std::string, std::shared_ptr<logging::impl::TpLogger>> loggers_;
120 rcu::RcuMap<std::string, logging::LoggerPtr> extra_loggers_;
121 utils::PeriodicTask flush_task_;
122 logging::impl::TcpSocketSink* socket_sink_{nullptr};
123 alerts::Storage& alert_storage_;
124
125 // Subscriptions must be the last fields.
126 os_signals::Subscriber signal_subscriber_;
127 utils::statistics::Entry statistics_holder_;
128};
129
130template <>
131inline constexpr bool kHasValidate<Logging> = true;
132
133} // namespace components
134
135USERVER_NAMESPACE_END