Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging activities in multithreaded applications

I have a layered application in Java which has a multi thread data access layer which is invoked from different points. A single call to this layer is likely to spawn several threads to parallelize requests to the DB.

What I'm looking for is a logging tool that would allow me to define "activities" that are composed by various threads. Therefore, the same method in the data access layer should log different outputs depending on its caller. The ability to group different outputs to summarize the total cost of an operation is also important.

Although the application is in Java, language is not a restriction; what I need are the design guidelines so to eventually implement it. We are currently using log4j, but can't get this behaviour from it.

like image 533
Santiago Palladino Avatar asked Sep 17 '08 13:09

Santiago Palladino


People also ask

What are some examples of multithreaded applications?

Another example of a multithreaded program that we are all familiar with is a word processor. While you are typing, multiple threads are used to display your document, asynchronously check the spelling and grammar of your document, generate a PDF version of the document.

What are multithreaded processes?

Multi-thread processing is a mechanism to accomplish high performance by partitioning and processing the data in multiple threads parallelly. The number of threads will be determined automatically under multiple conditions such as the size of the read data or the number of CPU cores.


2 Answers

You should also have a look at the nested diagnostic context feature of log4j. Pushing different contexts to the logger for different callers might do the trick for you.

like image 189
Andreas Skoog Avatar answered Oct 12 '22 23:10

Andreas Skoog


You should be able to pass a logger around, so you create a logger based on some "common" for the task data - i.e. username, etc. Then, pass this logger as parameter to all methods you need. That way, you'll be able to set different filters and/or rules in your log4j config file. Or to scrape the output file based on the logger name.

EDIT: Also check MDC and NDC classes in log4j. You can add there context data.

like image 43
Sunny Milenov Avatar answered Oct 13 '22 00:10

Sunny Milenov