|
1 | 1 | // Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 |
|
4 |
| -use logger::{error, info}; |
| 4 | +use logger::{error, info, log_enabled, Level}; |
5 | 5 | use micro_http::{Body, Method, Request, Response, StatusCode, Version};
|
6 | 6 | use serde::ser::Serialize;
|
7 | 7 | use serde_json::Value;
|
@@ -232,17 +232,34 @@ fn log_received_api_request(api_description: String) {
|
232 | 232 | fn describe(method: Method, path: &str, body: Option<&Body>) -> String {
|
233 | 233 | match (path, body) {
|
234 | 234 | ("/mmds", Some(_)) | (_, None) => format!("{:?} request on {:?}", method, path),
|
235 |
| - (_, Some(value)) => format!( |
236 |
| - "{:?} request on {:?} with body {:?}", |
237 |
| - method, |
238 |
| - path, |
239 |
| - std::str::from_utf8(value.body.as_slice()) |
240 |
| - .unwrap_or("inconvertible to UTF-8") |
241 |
| - .to_string() |
242 |
| - ), |
| 235 | + ("/cpu-config", Some(payload_value)) => { |
| 236 | + // If the log level is at Debug or higher, include the CPU template in |
| 237 | + // the log line. |
| 238 | + if log_enabled!(Level::Debug) { |
| 239 | + describe_with_body(method, path, payload_value) |
| 240 | + } else { |
| 241 | + format!( |
| 242 | + "{:?} request on {:?}. To view the CPU template received by the API, \ |
| 243 | + configure log-level to DEBUG", |
| 244 | + method, path |
| 245 | + ) |
| 246 | + } |
| 247 | + } |
| 248 | + (_, Some(payload_value)) => describe_with_body(method, path, payload_value), |
243 | 249 | }
|
244 | 250 | }
|
245 | 251 |
|
| 252 | +fn describe_with_body(method: Method, path: &str, payload_value: &Body) -> String { |
| 253 | + format!( |
| 254 | + "{:?} request on {:?} with body {:?}", |
| 255 | + method, |
| 256 | + path, |
| 257 | + std::str::from_utf8(payload_value.body.as_slice()) |
| 258 | + .unwrap_or("inconvertible to UTF-8") |
| 259 | + .to_string() |
| 260 | + ) |
| 261 | +} |
| 262 | + |
246 | 263 | /// Generates a `GenericError` for each request method.
|
247 | 264 | pub(crate) fn method_to_error(method: Method) -> Result<ParsedRequest, Error> {
|
248 | 265 | match method {
|
|
0 commit comments