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/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 needs.
38///
39/// ## Static options of components::Logging :
40/// @include{doc} scripts/docs/en/components_schema/core/src/logging/component.md
41///
42/// Options inherited from @ref components::RawComponentBase :
43/// @include{doc} scripts/docs/en/components_schema/core/src/components/impl/component_base.md
44///
45/// `default` logger is the one used for `LOG_*`.
46///
47/// ### Logs output
48/// You can specify where logs are written in the `file_path` option:
49/// - Use <tt>file_path: '\@stdout'</tt> to write your logs to standard output stream;
50/// - Use <tt>file_path: '\@stderr'</tt> to write your logs to standard error stream;
51/// - Use <tt>file_path: '\@null'</tt> to suppress sending of logs;
52/// - Use <tt>file_path: /absolute/path/to/log/file.log</tt> to write your logs to file. Use USR1 signal or @ref server::handlers::OnLogRotate to reopen files after log rotation;
53/// - Use <tt>file_path: 'unix:/absolute/path/to/logs.sock'</tt> to write your logs to unix socket. Socket must be created before the service starts and closed by listener after service is shut down.
54///
55/// For sending logs directly to an otlp (opentelemetry) sink, see @ref opentelemetry.
56///
57/// Customization of log sinks beyond the ones listed above is not supported at the moment.
58///
59/// ### Logs format
60/// You can specify in what format logs are written in the `format` option:
61/// - Use `format: tskv` for traditional optimized userver-flavoured TSKV representation. See @ref utils::encoding::TskvParser for the exact format specification;
62/// - Use `format: ltsv` for the same format, with `:` instead of `=` for key-value separator;
63/// - Use `format: raw` for TSKV logs with timestamp and other default tags stripped, useful for @ref custom_loggers "custom loggers";
64/// - Use `format: json` for JSON logs. @ref logging::JsonString can be used with this format for rich hierarchical log tags;
65/// - Use `format: json_yadeploy` for JSON logs with a slightly different structure.
66///
67/// When sending logs using @ref opentelemetry, logs are written to otlp protobuf messages.
68///
69/// Customization of log formats beyond the ones listed above is not supported at the moment.
70///
71/// ## Static configuration examples:
72///
73/// Writing logs to stderr:
74///
75/// @snippet core/functional_tests/basic_chaos/static_config.yaml logging
76///
77/// Advanced configuration showing options for access logs and a custom opentracing logger:
78///
79/// @snippet components/common_component_list_test.cpp Sample logging component config
80
81// clang-format on
82
83class Logging final : public RawComponentBase {
84public:
85 /// @ingroup userver_component_names
86 /// @brief The default name of @ref components::Logging component
87 static constexpr std::string_view kName = "logging";
88
89 /// The component constructor
90 Logging(const ComponentConfig&, const ComponentContext&);
91 ~Logging() override;
92
93 /// @brief Returns a logger by its name
94 /// @param name Name of the logger
95 /// @returns Pointer to the Logger instance
96 /// @throws std::runtime_error if logger with this name is not registered
97 logging::LoggerPtr GetLogger(const std::string& name);
98
99 /// @brief Returns a text logger by its name
100 /// @param name Name of the logger
101 /// @returns Pointer to the Logger instance
102 /// @throws std::runtime_error if logger with this name is not registered
103 /// @throws std::runtime_error if logger is not a text logger
104 logging::TextLoggerPtr GetTextLogger(const std::string& name);
105
106 /// @brief Sets a logger
107 /// @param name Name of the logger
108 /// @param logger Logger to set
109 void SetLogger(const std::string& name, logging::LoggerPtr logger);
110
111 /// @brief Returns a logger by its name
112 /// @param name Name of the logger
113 /// @returns Pointer to the Logger instance, or `nullptr` if not registered
114 logging::LoggerPtr GetLoggerOptional(const std::string& name);
115
116 void StartSocketLoggingDebug(const std::optional<logging::Level>& log_level);
117 void StopSocketLoggingDebug(const std::optional<logging::Level>& log_level);
118
119 /// Reopens log files after rotation
121 void TryReopenFiles();
122
123 void WriteStatistics(utils::statistics::Writer& writer) const;
124
125 static yaml_config::Schema GetStaticConfigSchema();
126
127private:
128 void Init(const ComponentConfig&, const ComponentContext&);
129 void Stop() noexcept;
130
131 void FlushLogs();
132
133 engine::TaskProcessor& fs_task_processor_;
134 std::unordered_map<std::string, std::shared_ptr<logging::impl::TpLogger>> loggers_;
135 rcu::RcuMap<std::string, logging::LoggerPtr> extra_loggers_;
136 utils::PeriodicTask flush_task_;
137 logging::impl::TcpSocketSink* socket_sink_{nullptr};
138 utils::statistics::MetricsStoragePtr metrics_storage_;
139
140 // Subscriptions must be the last fields.
141 os_signals::Subscriber signal_subscriber_;
142 utils::statistics::Entry statistics_holder_;
143};
144
145template <>
146inline constexpr bool kHasValidate<Logging> = true;
147
148} // namespace components
149
150USERVER_NAMESPACE_END