How to set the log level in Grails 4
Asked Answered
P

3

6

In my Grails 4 app, log.info("log message") doesn't show log, but log.error("log message") does.

How do I change the log level from error to info in Grails 4?

Providence answered 3/1, 2020 at 21:58 Comment(0)
P
7

Option 1

All I needed to do was update the application.yml file and added the following to the bottom

logging:
    level:
        root: INFO

You can also set a single the log level for a single package:

logging:
    level:
        packageName: INFO

Option 2

Since Grails 4 is based on Spring Boot, I ended up just setting the appropriate environment variable, i.e. logging.level.root=INFO or logging.level.com.mycompany.mypackage=INFO which I did in intellij by editing my run configuration (see below). This way, I can set the logging level differently when I deploy it.

enter image description here enter image description here

Providence answered 3/1, 2020 at 21:58 Comment(0)
S
1

You want to edit grails-app/conf/logback.groovy. Below is what the default file looks like for Grails 4.0.1.

import grails.util.BuildSettings
import grails.util.Environment
import org.springframework.boot.logging.logback.ColorConverter
import org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter

import java.nio.charset.StandardCharsets

conversionRule 'clr', ColorConverter
conversionRule 'wex', WhitespaceThrowableProxyConverter

// See http://logback.qos.ch/manual/groovy.html for details on configuration
appender('STDOUT', ConsoleAppender) {
    encoder(PatternLayoutEncoder) {
        charset = StandardCharsets.UTF_8

        pattern =
                '%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} ' + // Date
                        '%clr(%5p) ' + // Log level
                        '%clr(---){faint} %clr([%15.15t]){faint} ' + // Thread
                        '%clr(%-40.40logger{39}){cyan} %clr(:){faint} ' + // Logger
                        '%m%n%wex' // Message
    }
}

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

The specific change depends on what you really want to do. For example, if you have a controller named demo.SomeController and you want to set its log level to INFO, you could add something like this:

logger 'demo.SomeController', INFO, ['STDOUT'], false

See http://logback.qos.ch/manual/groovy.html for the full config reference.

I hope that helps.

Spheroid answered 3/1, 2020 at 22:33 Comment(1)
I added logger ("org.hibernate.orm.deprecation", ERROR, ["STDOUT"]) root(ERROR, ['STDOUT']) but I still get warning level logs from org.hibernate.orm.deprecation. What am I missing?Electrocautery
R
1

Simple Way:

Update/Replace your grails-app/conf/logback.groovy with following code:

    import ch.qos.logback.classic.encoder.PatternLayoutEncoder

appender("FILE", RollingFileAppender) {
    file = "logs/FILE-NAME.log"
    rollingPolicy(TimeBasedRollingPolicy) {
        fileNamePattern = "logs/FILE-NAME-%d{yyyy-MM-dd}.log"
        maxHistory = 30
    }
    encoder(PatternLayoutEncoder) {
        pattern = "%d{HH:mm:ss.SSS} %-4relative [%thread] %-5level %logger{35} - %msg%n"
    }

}
root(INFO, ["FILE"])

Above solution shows logger level to INFO

You can refer this more details and all log levels.

Hope this will helps you.

Renfrew answered 5/1, 2020 at 10:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.