herom
herom

Reputation: 2542

Groovy Script - Logback configuration unawaited behaviour

I want to use Logback as my logging framework within Grails. therefore I set up everything in place to work but my implementation fails on the configuration file itself. the reason is, as I guess, somewhere whithin the scoping of Groovy Script but I'm not able to figure it out...

if I define my String properties without any identifier which I want to use later I get a warning that it may not be accessed. For example:

LOG_DIR = 'c:/temp/myproject/logs/'
BACKUP_DIR = LOG_DIR + 'backup/'

appender('F_MAIN', RollingFileAppender) {
  file = LOG_DIR + 'test.log'
  rollingPolicy(FixedWindowRollingPolicy) {
    fileNamePattern = BACKUP_DIR + 'test.%d{yyyy-MM-dd}.%i.log.zip'
    // .... and so on
  }
}

I get the following error message from Logback, which I'm pretty sure is indicating that both LOG_DIR and BACKUP_DIR can not be reached:

13:33:32,036 |-ERROR in ch.qos.logback.classic.gaffer.AppenderDelegate@6fd00b - Appender [F_MAIN] of type [ch.qos.logback.core.rolling.RollingFileAppender] has no appplicable [LOG_DIR] property 
13:33:32,068 |-ERROR in ch.qos.logback.classic.gaffer.ComponentDelegate@788ac3 - Component  of type [ch.qos.logback.core.rolling.FixedWindowRollingPolicy] has no appplicable [BACKUP_DIR] property 

I also tried the following approach by declaring both variables with the @Field tag, but it still does not work:

@Field String LOG_DIR = 'c:/temp/myproject/logs/'
@Field String BACKUP_DIR = LOG_DIR + 'backup/'

appender('F_MAIN', RollingFileAppender) {
  file = LOG_DIR + 'test.log'
  rollingPolicy(FixedWindowRollingPolicy) {
    fileNamePattern = BACKUP_DIR + 'test.%d{yyyy-MM-dd}.%i.log.zip'
    // .... and so on
  }
}

what am I doing wrong here?

Upvotes: 1

Views: 1006

Answers (1)

herom
herom

Reputation: 2542

oh my! after searching and a lot of trial/error I found the solution and it was so close and definitely seems obvious now: I had to declare both variables with def, so now they are visible throughout the whole script ;)

For example, this is working code:

def LOG_DIR = 'c:/temp/myproject/logs/'
def BACKUP_DIR = LOG_DIR + 'backup/'

appender('F_MAIN', RollingFileAppender) {
  file = LOG_DIR + 'test.log'
  rollingPolicy(FixedWindowRollingPolicy) {
    fileNamePattern = BACKUP_DIR + 'test.%d{yyyy-MM-dd}.%i.log.zip'
    // .... and so on
  }
}

now, I'm also able to use a function like this within my script:

def createFilename(String directory, String name, boolean isBackupFile) {
  String filename = ''
  if(isBackupFile) {
    filename = "${directory}backup/MyProject-${name}.%d{yyyy-MM-dd}.%i.log.zip"
  } else {
    filename = "${directory}MyProject-${name}.log"
  }
  return filename
}

def fileAppenderLog = createFilename(LOG_DIR, 'output', false)
def fileAppenderLogBackup = createFilename(LOG_DIR, 'output', true)

appender('F_MAIN', RollingFileAppender) {
  file = fileAppenderLog
  rollingPoliciy(FixedWindowRollingPolicy) {
    fileNamePattern = fileAppenderLogBackup
    // .... and so on
  }
}

which is pretty useful, I think :), especially if you want to declare a bunch of different logfiles and even if you want to declare temporary logfiles which are created when Logback is rescanning this file ...

Upvotes: 3

Related Questions