Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the right logging approach when using Spring Batch?

What is the right logging approach when using Spring Batch? Should I use log4j (or something similar) or Spring Batch provides some instruments that help me to instantiate a logger and use it? Maybe some sort of dependency injection of the logger?

like image 705
yegor256 Avatar asked Feb 24 '23 03:02

yegor256


2 Answers

I'd used log4j. and its the simple and nice approach.

like image 126
jmj Avatar answered Apr 07 '23 13:04

jmj


I'm not sure the original poster's question was answered, so I'll try restating this a bit. In Spring Batch you may have multiple threads going, and you may want to have job-specific logging, so that all events for a particular job are logged into a single log file. You want a Logger whose scope is tied directly to the job you are processing. When the job finishes, the logger (and all references to the logger) go away.

So when you submit Job#1, all events are logged to "job_1.log"; when you submit Job#2, its events are logged to "job_2.log", etc.

In log4j, when you do "Logger.getLogger('mylogger')" you are telling the LogManager to get 'mylogger' out of the cache and give it to you. What you really want is a new instance of the logger, configured using the configuration of mylogger. In Spring this might be typically done with a prototype bean. Every time you ask the context for 'mylogger' you would get a new instance.

like image 30
mfortner Avatar answered Apr 07 '23 13:04

mfortner