userver: userver/server/handlers/log_level.hpp Source File
Loading...
Searching...
No Matches
log_level.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/server/handlers/log_level.hpp
4/// @brief @copybrief server::handlers::LogLevel
5
6#include <userver/concurrent/variable.hpp>
7#include <userver/logging/level.hpp>
8#include <userver/server/handlers/http_handler_base.hpp>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace components {
13class Logging;
14} // namespace components
15
16namespace server::handlers {
17// clang-format off
18
19/// @ingroup userver_components userver_http_handlers
20///
21/// @brief Handler that controls logging levels of all the loggers.
22///
23/// The component has no service configuration except the
24/// @ref userver_http_handlers "common handler options".
25///
26/// ## Static configuration example:
27///
28/// @snippet components/common_server_component_list_test.cpp Sample handler log level component config
29///
30/// ## Scheme
31/// For the GET and PUT requests this handler returns the following JSON:
32/// @code
33/// {
34/// "init-log-level": <log level on service start>,
35/// "current-log-level": <current log level>
36/// }
37/// @endcode
38///
39/// Particular logger name could be specified by an optional `logger` query
40/// argument. Default logger is used, if no `logger` was provided.
41///
42/// PUT request changes the logger level to the value specified in the `level`
43/// query argument. Set it to the `reset` value, to reset the logger level to
44/// the initial values.
45///
46/// @see @ref scripts/docs/en/userver/log_level_running_service.md
47
48// clang-format on
49class LogLevel final : public HttpHandlerBase {
50public:
51 LogLevel(const components::ComponentConfig& config, const components::ComponentContext& component_context);
52
53 /// @ingroup userver_component_names
54 /// @brief The default name of server::handlers::LogLevel
55 static constexpr std::string_view kName = "handler-log-level";
56
57 std::string HandleRequestThrow(const http::HttpRequest& request, request::RequestContext&) const override;
58
59 static yaml_config::Schema GetStaticConfigSchema();
60
61private:
62 std::string ProcessGet(const http::HttpRequest& request, request::RequestContext&) const;
63 std::string ProcessPut(const http::HttpRequest& request, request::RequestContext&) const;
64
65 components::Logging& logging_component_;
66 struct Data {
67 logging::Level default_init_level;
68 mutable std::unordered_map<std::string, logging::Level> init_levels;
69 };
70 concurrent::Variable<Data> data_;
71};
72
73} // namespace server::handlers
74
75template <>
76inline constexpr bool components::kHasValidate<server::handlers::LogLevel> = true;
77
78USERVER_NAMESPACE_END