Example application demonstrating logging with Grails.
The application will:
- log an info message every 5 seconds
- log an error message every 15 seconds
To run the application, execute ./gradlew bootRun
from the root directory
The heart of the logging will be accomplished in the grails-app/conf/logback.groovy
file. Apart from the imports, everything above line 38 is auto-generated by the command grails create-app
.
Setting the HOME_DIR
will be the location where you want the logs to reside. In this project, the logs are generated at the root of our application.
def HOME_DIR = "."
We create a new rolling appender
appender("ROLLING", RollingFileAppender) ...
assign a pattern
encoder(PatternLayoutEncoder) {
pattern = "%level %logger - %msg%n"
}
When assigning a rolling policy we give it 3 arguments
fileNamePattern
the pattern of the filename along with the sub-directory where to write themmaxHistory
the maximum number of files to retaintotalSizeCap
the total size of the log files
Then to utilize the new rolling appender, we need set the logger to ROLLING
.
logger('grails.logging', INFO, ['STDOUT', 'ROLLING'], false)
If we wanted all logs instead of just those scoped to grails.logging
, we assign the appender as:
root(INFO, ['ROLLING'])