首页 > 解决方案 > Logging with prefix determined at a Class Initialization

问题描述

I have a module that I'm working on which has the logger set up like this:

public class MyClass {

    private static final Logger LOGGER = LoggerFactory.getLogger(MyClass.class);

    public MyClass(String org, String division) {
        this.org = org;
        this.division = division;
    }

    public void myMethod() {
      if(blah) {
          LOGGER.log("Something happened");
      } else {
          LOGGER.log("Something went Wrong");
      }
    }
}

Now, this module is running for multiple orgs and divisions, so my logs look like this in Splunk:

Something happened
Something went Wrong
Something happened
Something happened
Something went Wrong

with no info of org or division, To fix this, someone started adding a addPrefix method, which looks like this:

private String addPrefix() {
    return String.format("(%s, %s)", this.org, this.division);
}

and updated logs to LOGGER.log("{} Something happened", addPrefix()); and LOGGER.log("{} Something went wrong", addPrefix());

Now our logs look like this:

(org1, div1) Something happened
(org1, div2) Something went Wrong
(org2, div3) Something happened

Problem is, as the number of logs increase, it's painful to maintain this and ensure everyone adds {}, addPrefix() to their logs. Is there a better way to do this?

I looked into mdc but could not figure out how to use it here.

Should I initialize my logger within my constructor where my parameters are all known? Will that affect logging in static methods? Is there a memory overhead to doing this?

标签: javalogginglog4jslf4jmdc

解决方案


你是什​​么意思“我研究了 mdc 但不知道如何在这里使用它。”。您所描述的是当您想要使用 MDC 时的“典型代表”。您将在请求开始时将组织名称和部门名称添加到 MDC(无论您使用的技术可能是什么),然后配置您选择的日志框架以将组织和部门添加到您的输出中。您尚未提供有关您使用的框架或配置的详细信息,但您可以使用 Log4j 2 进行配置

<PatternLayout pattern="(%X{organization, division}) %msg%n"/>

假设您选择存储数据的键被命名为“组织”和“部门”。这将导致输出就像您上面的示例一样。


推荐阅读