MDC区分模块之间的日志记录 [英] MDC to differentiate logging between modules

查看:81
本文介绍了MDC区分模块之间的日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的软件使用面向服务的体系结构(SOA).所有服务(为简单起见,将它们称为模块)都写入单个日志文件.我想区分模块之间的日志记录.以下是我希望日志消息显示的样子.

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

我看到的好处是,我可以在对日志进行尾部处理"时复制必要的信息.另外,在分析日志时,我可以grep必要的模块明智信息.这是可以实现的吗?我使用log4j作为日志记录实用程序.请注意,每个模块可以具有多个程序包(程序包的层次结构).我遇到的最接近实现此目标的方法是使用MDC.

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.

推荐答案

我能够通过使用MDC(MappedDiagnosticContext)实现这一目标.幸运的是,我的SOA框架允许我以一种非常通用的方式放置上下文信息(服务/模块名称).以下是为我完成的代码片段.

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())

当然,我能够使用EnhancedPatternLayout中的-X标记来检索信息.

Ofcourse I was able to retrieve the information using -X tag in the EnhancedPatternLayout.

这篇关于MDC区分模块之间的日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆