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 {
50 public:
51 LogLevel(const components::ComponentConfig& config,
52 const components::ComponentContext& component_context);
53
54 /// @ingroup userver_component_names
55 /// @brief The default name of server::handlers::LogLevel
56 static constexpr std::string_view kName = "handler-log-level";
57
58 std::string HandleRequestThrow(const http::HttpRequest& request,
59 request::RequestContext&) const override;
60
61 static yaml_config::Schema GetStaticConfigSchema();
62
63 private:
64 std::string ProcessGet(const http::HttpRequest& request,
65 request::RequestContext&) const;
66 std::string ProcessPut(const http::HttpRequest& request,
67 request::RequestContext&) const;
68
69 components::Logging& logging_component_;
70 struct Data {
71 logging::Level default_init_level;
72 mutable std::unordered_map<std::string, logging::Level> init_levels;
73 };
74 concurrent::Variable<Data> data_;
75};
76
77} // namespace server::handlers
78
79template <>
80inline constexpr bool components::kHasValidate<server::handlers::LogLevel> =
81 true;
82
83USERVER_NAMESPACE_END