使用可选参数的java日志记录

3lxsmp7m  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(305)

我有一个方法要添加特定的日志记录:

  1. @Slf4j
  2. @Service
  3. public class SomethingService {
  4. public void doSomething(Something data, String comment, Integer limit) {
  5. Long id = saveSomethingToDatabase(data, comment);
  6. boolean sentNotification = doSomething(id);
  7. // ...
  8. // Log what you done.
  9. // Variables that always have important data: data.getName(), id
  10. // Variables that are optional: sentNotification, comment, limit
  11. // (optional means they aren't mandatory, rarely contains essential data, often null, false or empty string).
  12. }
  13. }

我可以简单地记录所有:

  1. log.info("Done something '{}' and saved (id {}, sentNotification={}) with comment '{}' and limit {}",
  2. something.getName(), id, sentNotification, comment, limit);
  3. // Done something 'Name of data' and saved (id 23, sentNotification=true) with comment 'Comment about something' and limit 2

但大多数时候,大多数参数都是无关的。通过以上步骤,我得到如下日志:

  1. // Done something 'Name of data' and saved (id 23, sentNotification=false) with comment 'null' and limit null

这使得日志很难阅读,很长,而且不必要的复杂(在大多数情况下,其他参数不存在)。
我想处理所有的案件,只保留必要的数据。示例:

  1. // Done something 'Name of data' and saved (id 23)
  2. // Done something 'Name of data' and saved (id 23) with comment 'Comment about something'
  3. // Done something 'Name of data' and saved (id 23) with limit 2
  4. // Done something 'Name of data' and saved (id 23) with comment 'Comment about something' and limit 2
  5. // Done something 'Name of data' and saved (id 23, sent notification)
  6. // Done something 'Name of data' and saved (id 23, sent notification) with limit 2
  7. // Done something 'Name of data' and saved (id 23, sent notification) with comment 'Comment about something'
  8. // Done something 'Name of data' and saved (id 23, sent notification) with comment 'Comment about something' and limit 2

我可以手工编码:

  1. String notificationMessage = sentNotification ? ", sent notification" : "";
  2. String commentMessage = comment != null ? String.format(" with comment '%s'", comment) : "";
  3. String limitMessage = "";
  4. if (limit != null) {
  5. limitMessage = String.format("limit %s", limit);
  6. limitMessage = comment != null ? String.format(" and %s", limitMessage) : String.format(" with %s", limitMessage);
  7. }
  8. log.info("Done something '{}' and saved (id {}{}){}{}",
  9. something.getName(), id, notificationMessage, commentMessage, limitMessage);

但是它很难写,很难读,很复杂而且会导致错误。
我想指定一部分日志。
伪代码示例:

  1. log.info("Done something '{}' and saved (id {} $notification) $parameters",
  2. something.getName(), id,
  3. $notification: sentNotification ? "sent notification" : "",
  4. $parameters: [comment, limit]);

它应该支持可选参数,用给定字符串替换布尔/条件,支持分隔空格、逗号和单词 with 以及 and .
可能有这个图书馆吗?或者至少有一种更简单的方法来编码这个?
如果不是这样,我就没有别的事情可以写我自己的库来记录消息了。此外,这种库将提供所有日志的一致性。
如果您没有看到三个可选参数的问题,只需想象一下还有更多的参数(并且您不能总是将它们打包到一个类中—另一个仅用于参数日志记录的类层会导致更多的复杂性)。
最后,我知道我可以分别记录每个动作。但有了这个,我会得到更多的日志,我不会在一个地方有最重要的信息。其他日志在 debug 水平,不是 info .

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题