seperate info, error logs using logback grails code example

Example 1: logback grails log in different files

//grails-app/confg/logback.groovy
import grails.util.BuildSettings
import grails.util.Environment
import ch.qos.logback.core.rolling.RollingFileAppender
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy
import ch.qos.logback.core.util.FileSize

def HOME_DIR = "."

// See http://logback.qos.ch/manual/groovy.html for details on configuration
appender('STDOUT', ConsoleAppender) {
    encoder(PatternLayoutEncoder) {
        pattern = "%level %logger - %msg%n"
    }
}

appender("ROLLING", RollingFileAppender) {
  encoder(PatternLayoutEncoder) {
      pattern = "%level %logger - %msg%n"
  }
  rollingPolicy(TimeBasedRollingPolicy) {
      fileNamePattern = "${HOME_DIR}/logs/myApp-%d{yyyy-MM-dd_HH-mm}.log"
      maxHistory = 30
      totalSizeCap = FileSize.valueOf("2GB")
  }
}

def targetDir = BuildSettings.TARGET_DIR
if (Environment.isDevelopmentMode() && targetDir != null) {
    appender("FULL_STACKTRACE", FileAppender) {
        file = "${targetDir}/stacktrace.log"
        append = true
        encoder(PatternLayoutEncoder) {
            pattern = "%level %logger - %msg%n"
        }
    }

    logger("StackTrace", ERROR, ['FULL_STACKTRACE'], false)
    root(ERROR, ['STDOUT', 'FULL_STACKTRACE'])
}
else {
    root(ERROR, ['ROLLING'])
}

Example 2: logback grails log in different files

fileNamePattern = "/myApp-log.%d{yyyy/MM}.gz"	      //Rollover at the beginning of each month, compress the rolled-over file with GZIP

Example 3: logback grails log in different files

//grails-app/confg/logback.groovy
import grails.util.BuildSettings
import grails.util.Environment

// See http://logback.qos.ch/manual/groovy.html for details on configuration
appender('STDOUT', ConsoleAppender) {
    encoder(PatternLayoutEncoder) {
        pattern = "%level %logger - %msg%n"
    }
}

def targetDir = BuildSettings.TARGET_DIR
if (Environment.isDevelopmentMode() && targetDir != null) {
    appender("FULL_STACKTRACE", FileAppender) {
        file = "${targetDir}/stacktrace.log"
        append = true
        encoder(PatternLayoutEncoder) {
            pattern = "%level %logger - %msg%n"
        }
    }
    logger("StackTrace", ERROR, ['FULL_STACKTRACE'], false)
    root(ERROR, ['STDOUT', 'FULL_STACKTRACE'])
}
else {
    root(ERROR, ['STDOUT'])
}

Example 4: logback grails log in different files

fileNamePattern = "/myApp-log.%d{yyyy-MM}.log"	      //Rollover at the beginning of each month, file format: myApp-log.2016-11.log
  fileNamePattern = "/myApp-log.%d{yyyy-ww}.log"	      //Rollover at the first day of each week. Note that the first day of the week depends on the locale.
  fileNamePattern = "/myApp-log.%d{yyyy-MM-dd_HH}.log"	//Rollover at the top of each hour.

Tags:

Ruby Example