Separate logs for each order in Log4J

In my current project we are using Apache CommonsLogging backed up by Log4J.

Though it is a Multi-threaded application (processes multiple orders simultaneously), all the logs from all these threads simultaneously go inside a single file (via RollingFileAppender). Recently there was a change request to maintain separate logs for each order.
Having a separate file for every incoming order can prove a little more than costly.

— Housekeeping of these logs on the basis of time? Who decides which log has to be kept and which one has to be discarded? Perhaps it can be handled by writing a few shellscripts.

— Under development, or in production, for each run we’d have to open a different Log file to check the Logs for debugging purposes.

— On a Unix server there is a limited number of inodes or files that you can keep inside a directory. You run out of it then you cannot create more files and for such a bulky directory, manipulation would be a big pain.
So I would rather append an identifier (which can tell the order uniquely) with each and every log statement and then can easily `grep` the main Log file for the complete logs of a particular order.

— Appending ORDERID of that order to each and every log statement of a big big application would be a major overhead and isn’t maintainable. What if there comes a NEWUNIQUEVALUE for the orders!?

— Appending ORDERID just to the first and the last log statement wouldn’t guarantee that all the logs which get extracted are of that same order. It is a multi-threaded application anyway.


Make changes to the file. Add a %t at the starting of the Pattern.


ConversionPattern=%p %c – %m%n


ConversionPattern=%t %p %c – %m%n

(Here we are asking the Logging Framework to append the name of the Thread to the logs)

t = Used to output the name of the thread that generated the logging event.

p = Used to output the priority of the logging event.

c = Used to output the category of the logging event.

m = Used to output the application supplied message associated with the logging event.

n = Outputs the platform dependent line separator character or characters.


In your Java code, inside the run() method of the ThreadClass or the Concrete Implementation of the Runnable Interface change the name of the Thread using:

String uniqueOrderId = ${ fetched from inside the order XML etc };


If for some reason you need to preserve the ThreadName, in that case append it back to the uniqueOrderId.


$ grep uniqueOrderId MAINLOG.log > uniqueOrderId.log

(find the lines with uniqueOrderId in the MAINLOG.log and create a new file uniqueOrderId.log with that)


(Dinner is getting cold. I know I could have been more verbose! TING!)

One thought on “Separate logs for each order in Log4J

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s