问题描述
我的软件使用面向服务的体系结构(SOA).所有服务(为简单起见,我们称其为模块)都将其写入一个日志文件.我想区分模块之间的记录.以下是我希望我的日志消息的样子.
[MODULE-1] INFO - This is a test log message from MODULE ONE [MODULE-2] INFO - This is a test log message from MODULE TWO
我看到的优势是,在"尾随"日志时,我将能够了解必要的信息.另外,在分析日志时,我可以了解必要的模块明智的信息.这是可以实现的吗?我使用log4j作为记录实用程序.请注意,每个模块可以具有多个软件包(软件包的层次结构).我遇到的最接近的是使用MDC.
推荐答案
我能够使用MDC(mappeddiagnosticcontext)来实现这一目标.幸运的是,我的SOA框架使我可以以相当通用的方式将上下文信息(服务/模块名称)放置.以下是为我完成的代码.
MDC.put("MODULE", getServiceName())
我能够在EnhancedPatternlayout中使用-x标签检索信息.
问题描述
My software uses a Service Oriented Architecture (SOA). All the services (lets call them modules for simplicity) writes to a single log file. I would like to distinguish logging between modules. Below is how I would like my log message to look like.
[MODULE-1] INFO - This is a test log message from MODULE ONE [MODULE-2] INFO - This is a test log message from MODULE TWO
The advantage I see doing this is I would be able to grep necessary information while 'tail-ing' the logs. Also, while analyzing the logs, I can grep necessary module wise information. Is this achievable? I use log4j as my logging utility. Note that each modules can have multiple packages (hierarchy of packages). The closest I came across to achieve this is by using MDC.
推荐答案
I was able to achieve this by using MDC (MappedDiagnosticContext). Fortunately, my SOA framework allowed me a place where I could place my context information (service/module name) in a pretty generic way. Below is the code snipped which did it for me.
MDC.put("MODULE", getServiceName())
Ofcourse I was able to retrieve the information using -X tag in the EnhancedPatternLayout.