How to Use change FilePath in log4j appender according environment

Posted By : Ankush Kocher | 19-Nov-2014

In this Blog we see how to use different file path for different environment. We will consider two environment devolepment and production and use file path  -

/opt/dev/myLog.log   
and /opt/production/myLog.log 

respectively.

 

In config/config.groovy

environments {
 
  development {
    log4j = {
      appenders {
        appender new DailyRollingFileAppender(
            name: 'myFileAppender', datePattern: "'.'yyyy-MM-dd",
            layout: pattern(conversionPattern: '%d{dd-MM-yyyy HH:mm:ss} %X{sessionId}  %X{actionQuery} %n'),
            file:   '/opt/dev/myLog.log'
           )
        }
  }
  production {
    grails.logging.jul.usebridge = false
    appenders {
        appender new DailyRollingFileAppender(
            name: 'myFileAppender', datePattern: "'.'yyyy-MM-dd",
            layout: pattern(conversionPattern: '%d{dd-MM-yyyy HH:mm:ss} %X{sessionId}  %X{actionQuery} %n'),
            file:   '/opt/production/myLog.log'
           )
        }
}

alternatively,

def logPath
environments {
 
  development {
    logPath = '/opt/dev/myLog.log'
  }
  production {
    grails.logging.jul.usebridge = false
   logPath =  '/opt/production/myLog.log'
}

 log4j = {
    appenders {
        appender new DailyRollingFileAppender(
            name: 'myFileAppender', datePattern: "'.'yyyy-MM-dd",
            layout: pattern(conversionPattern: '%d{dd-MM-yyyy HH:mm:ss} %X{sessionId}  %X{actionQuery} %n'),
            file:  logPath
           )
   }
  }

Thanks

About Author

Author Image
Ankush Kocher

Ankush is a bright web app developer with expertise in Groovy and Grails development. Ankush is also an expert AngularJS developer.

Request for Proposal

Name is required

Comment is required

Sending message..