72438

MDC to differentiate logging between modules

Question:

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.

Answer1:

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.

Recommend

  • change text style inline in matplotlib
  • Two versions of selection sort
  • Flush previous Retrofit responses
  • R on MacOS Error: vector memory exhausted (limit reached?)
  • Octave signal package installation
  • Throttling websockets with netty-socketio server
  • ASP.NET MVC + jqGrid without AJAX
  • Java On AND'ing a short with an short, it is upgraded to int and returns weird values
  • why when we write \\n in the file it converts into \\r\\n combination?
  • How to recognize PDF watermark and remove it using PDFBox
  • Java library to escape/clean XML?
  • remove Niqqud from string in javascript
  • SocketChannel: java.io.IOException: An existing connection was forcibly closed by the remote host
  • Asking for undo/redo events in html/javascript
  • firebase unauth with google doesn't allow change of user
  • Geo Fix not working in Android SDK 2.2
  • System call time out?
  • Multilingual set up of codeigniter
  • Most efficient way to move table rows from one table to another
  • New Firebase failed: First argument must be a valid firebase URL and the path can't contain “.”
  • JSON encode and decode on PHP
  • Building Qt project for C++11 standard
  • Detecting null parameter in preprocessor macro
  • Servlet stops working on Tomcat server after some hits or time
  • Azure webjobs output logs indexing taking very long
  • ThreadStatic in asynchronous ASP.NET Web API
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • Is there a perl module to validate passwords stored in “{crypt}hashedpassword” “{ssha}hashedpassword
  • Meteor helpers not available in Angular template
  • NHibernate Validation Localization with S#arp Architecture
  • How to rebase a series of branches?
  • How can I send an e-mail from a vbs script
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • Accessing IRQ description array within a module and displaying action names
  • Perl system calls when running as another user using sudo
  • JSON with duplicate key names losing information when parsed
  • Jquery - Jquery Wysiwyg return html as a string
  • using HTMLImports.whenReady not working in chrome
  • Getting Messege Twice Using IMvxMessenger