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