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