userver: userver/server/handlers/json_body_logging.hpp Source File
Loading...
Searching...
No Matches
json_body_logging.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/server/handlers/json_body_logging.hpp
4/// @brief JSON body logging helpers for generated HTTP handlers.
5
6#include <optional>
7#include <string>
8#include <string_view>
9
10#include <userver/dynamic_config/fwd.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace server::handlers {
15
16struct HandlerConfig;
17
18/// @brief Options for selecting JSON body logging settings.
19struct JsonBodyLoggingOptions final {
20 /// Optional handler lookup key override for @ref USERVER_HTTP_SERVER_LOGS.
21 /// When unset, uses the low-cardinality path from @ref server::handlers::HandlerConfig.
22 std::optional<std::string_view> handler_name_override;
23};
24
25/// @brief Apply the request JSON masking policy and limit to a logging hook's output.
26///
27/// Masking rules are configured in @ref USERVER_HTTP_SERVER_LOGS.
28/// Configured bodies are processed as JSON. Existing hooks must run first.
29/// Returns masked JSON or a UTF-8-safe prefix with `...(truncated, total N bytes)`.
30/// The prefix and suffix together fit the limit; N is the input byte size after custom hooks.
31/// If the suffix itself does not fit, returns only its leading bytes.
32/// With no policy, returns the body unchanged.
33/// Invalid JSON produces an error marker.
34/// Final UTF-8 limiting belongs to @ref utils::log::ToLimitedUtf8.
35/// @ref server::handlers::JsonBodyLoggingOptions::handler_name_override may select settings by a custom key.
36/// Slash-prefixed keys are validated as static handler paths; other custom keys are not validated.
37/// The override limit is applied while masking;
38/// final limiting of unmasked bodies still uses the configured handler path.
40 const std::string& body,
41 const dynamic_config::Snapshot& snapshot,
42 const HandlerConfig& config,
43 JsonBodyLoggingOptions options = {}
44);
45
46/// @overload
47/// Reuses the input string when no masking policy is configured.
49 std::string&& body,
50 const dynamic_config::Snapshot& snapshot,
51 const HandlerConfig& config,
52 JsonBodyLoggingOptions options = {}
53);
54
55/// @brief Apply the response JSON masking policy and limit.
56///
57/// Explicit policies apply to typed and non-schema response bodies alike.
58/// @copydetails server::handlers::MaskJsonRequestBodyForLogging
60 const std::string& body,
61 const dynamic_config::Snapshot& snapshot,
62 const HandlerConfig& config,
63 JsonBodyLoggingOptions options = {}
64);
65
66/// @overload
67/// Reuses the input string when no masking policy is configured.
69 std::string&& body,
70 const dynamic_config::Snapshot& snapshot,
71 const HandlerConfig& config,
72 JsonBodyLoggingOptions options = {}
73);
74
75} // namespace server::handlers
76
77USERVER_NAMESPACE_END