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