java - 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?
解决方案
你是什么意思“我研究了 mdc 但不知道如何在这里使用它。”。您所描述的是当您想要使用 MDC 时的“典型代表”。您将在请求开始时将组织名称和部门名称添加到 MDC(无论您使用的技术可能是什么),然后配置您选择的日志框架以将组织和部门添加到您的输出中。您尚未提供有关您使用的框架或配置的详细信息,但您可以使用 Log4j 2 进行配置
<PatternLayout pattern="(%X{organization, division}) %msg%n"/>
假设您选择存储数据的键被命名为“组织”和“部门”。这将导致输出就像您上面的示例一样。
推荐阅读
- c# - 为什么没有为此代码调用析构函数
- javascript - JavaScript。字典键不是字符串?
- angular - Ionic PWA 与传统的 Ionic 是分开的吗?
- grails - Spring 安全核心插件 - 如何从控制器访问用户 ID
- angular - 请求语音识别 iOS、Ionic 3 的权限
- openshift - Openshift oc 工具和 web ui 超时:来自服务器的错误 (ServerTimeout)
- sql - 我收到异常“形式参数“@param1”未声明为 OUTPUT 参数,但在请求的输出中传递了实际参数”
- javascript - 复选框,本地存储上的单选按钮
- vue.js - Vue中如何在不同组件之间搭建事件的桥梁?
- docker - 带有来自临时容器的复制数据的 Docker 挂载卷