Im using grails 2.2.4 version with log4j.
I have 15 cron jobs running at different times and some at the same time.
I need to get the logs of each job to a separate log file.
When a job is getting executed I need to get the log of the entire flow.
Example
class SampleTestOneJob{
def utilService
def execute() {
log.info "Beginning SampleTestOneJob."
List<Admin> adminList = utilService.findAllAdmins()//Calling the
findAllAdmins of utilService.
log.info "Finished SampleTestOneJob."
}
}
class UtilService{
def findAllAdmins() {
log.info "Beginning findAllAdmins."
//Implementation to get all the admins
log.info "Finished findAllAdmins."
}
}
class SampleTestTwoJob{
def utilService
def execute() {
log.info "Beginning SampleTestTwoJob."
//Refreshing logic
//Calling the findAllAdmins of utilService.
List<Admin> adminList = utilService.findAllAdmins()
log.info "Finished SampleTestTwoJob."
}
}
How can I get logs of SampleTestOneJob and UtilService only when SampleTestOneJob is executed.
How can I get logs of SampleTestTwoJob and UtilService only when SampleTestTwoJob is executed.
Right now using RollingFileAppender i have done that. But the problem is when SampleTestOneJob and SampleTestTwoJob are executed. Since both are calling the findAllAdmins in UtilService. The logs when calling from SampleTestOneJob to findAllAdmins will be available in SampleTestTwoJob and the logs when calling from SampleTestTwoJob to findAllAdmins will be available in SampleTestOneJob.
Is there a way to identify for the flow and add the flow logs to the file.
Config.groovy
log4j = {
def logDir = 'target' + '/logs'
def logPattern = '%d [%t] %-5p %c{2} %x - %m%n'
def jobRollingFile1 = new RollingFileAppender(name: 'SampleOneLogs', layout: pattern(conversionPattern: logPattern))
def jobRollingPolicy1 = new TimeBasedRollingPolicy(fileNamePattern: "$logDir/Logs/Sample1.%d{yyyy-MM-dd}.gz", activeFileName: "$logDir/Logs/Sample1.log")
jobRollingPolicy1.activateOptions()
jobRollingFile1.setRollingPolicy jobRollingPolicy1
def jobRollingFile2 = new RollingFileAppender(name: 'SampleTwoLogs', layout: pattern(conversionPattern: logPattern))
def jobRollingPolicy2 = new TimeBasedRollingPolicy(fileNamePattern: "$logDir/Logs/Sample2.%d{yyyy-MM-dd}.gz", activeFileName: "$logDir/Logs/Sample2.log")
jobRollingPolicy2.activateOptions()
jobRollingFile2.setRollingPolicy jobRollingPolicy2
appenders {
appender jobRollingFile1
appender jobRollingFile2
}
info additivity: false, SampleOneLogs : ["grails.app.jobs.SampleTestOneJob", "grails.app.services.UtilService"]
info additivity: false, SampleTwoLogs : ["grails.app.jobs.SampleTestTwoJob", "grails.app.services.UtilService"]
}