将每个grails cron作业的日志记录到单独的文件中

时间:2016-08-30 08:42:12

标签: grails logging cron rollingfileappender fileappender

我使用带有log4j的grails 2.2.4版本。 我有15个cron作业在不同的时间运行,有些同时运行。 我需要将每个作业的日志记录到一个单独的日志文件中 当一个工作被执行时,我需要得到整个流程的日志。

示例

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."

 }
}

如何在执行SampleTestOneJob时获取SampleTestOneJob和UtilService的日志。

如何在执行SampleTestTwoJob时获取SampleTestTwoJob和UtilService的日志。

现在使用RollingFileAppender我已经做到了。但问题是当执行SampleTestOneJob和SampleTestTwoJob时。 因为两者都在UtilService中调用findAllAdmins。从SampleTestOneJob调用findAllAdmins时的日志将在SampleTestTwoJob中可用,并且从SampleTestTwoJob调用findAllAdmins时的日志将在SampleTestOneJob中可用。

是否有方法可以识别流并将流日志添加到文件中。

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"]

}

0 个答案:

没有答案