1

I'm upgrading log4j dependency in my grails 2.5.4 application from 1.2.17 to the latest version 2.17.1

I've excluded log4j from the BuildConfig.groovy and added the following dependencies pertaining to v2.17.1:

  • log4j-api
  • log4j-core
  • log4j-1.2-api
  • log4j-slf4j-impl

I'm using the log4-1.x bridge to reduce overall codebase change.

I've added the following log4j2.properties file under the conf directory:

rootLogger.level = INFO
rootLogger.additivity = false
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %m%n

But during the application boot-up and usage, I notice extra logs getting added to the console:

..........................................Attempting to load [0] user defined plugins
Grails plug-in [dataBinding] with version [2.5.4] loaded successfully
Grails plug-in [i18n] with version [2.5.4] loaded successfully
Grails plug-in [restResponder] with version [2.5.4] loaded successfully
Grails plug-in [core] with version [2.5.4] loaded successfully
Grails plug-in [greenmail] with version [1.3.4] loaded successfully
Grails plug-in [executor] with version [0.3] loaded successfully
Grails plug-in [webxml] with version [1.4.1] loaded successfully
Grails plug-in [cacheHeaders] with version [1.1.7] loaded successfully
Grails plug-in [browserDetection] with version [2.8.1] loaded successfully
Grails plug-in [console] with version [1.5.12] loaded successfully
Grails plug-in [remotePagination] with version [0.4.8] loaded successfully
Grails plug-in [tomcat] with version [7.0.42] loaded successfully
...

Some Hibernate logs are also appearing (didn't add the logs due to Domain information present)

It seems like the internal Grails logs are appended to the console. During application use, following kind of logs appeared:

FrameworkServlet 'gsp': initialization started
GSP servlet initialized
FrameworkServlet 'gsp': initialization completed in 39 ms
Invocation of <r:resource> for a resource that apparently doesn't exist: /assets/favicon.ico
Invocation of <r:resource> for a resource that apparently doesn't exist: /assets/spinner.gif
Invocation of <r:resource> for a resource that apparently doesn't exist: /assets/spinner.gif
Invocation of <r:resource> for a resource that apparently doesn't exist: /assets/spinner.gif
Invocation of <r:resource> for a resource that apparently doesn't exist: /assets/spinner.gif
Invocation of <r:resource> for a resource that apparently doesn't exist: /assets/spinner.gif
Invocation of <r:resource> for a resource that apparently doesn't exist: /assets/spinner.gif
...

Seems like setting the additivity property to false is not working. I tried using additive as well, but in vain.

I notice that log4j config present in Config.groovy is also not being read. Here's the config:

    log4j = {
  appenders {


    'null' name: 'empty'

    environments {
      development {
        'null' name: 'stacktrace'

        appender name: "appLog",
            new org.apache.log4j.DailyRollingFileAppender(
                threshold: org.apache.log4j.Level.INFO,
                datePattern: "'.'yyyy-MM-dd",
                file: "/tmp/test.log",
                layout: pattern(conversionPattern: '[%d{yyyy-MM-dd hh:mm:ss.SSS}] %p %c{5} %m%n'))
        root {
          info stdout
        }
      }

      production {
        file name: 'stacktrace', file: "/logs/stacktrace.log".toString()


        root {
          error 'stdout'
          info stdout
        }
      }

    }

  }

//  off 'ErrorsController & ResourceMeta'
  off 'org.grails.plugin.resource.ResourceMeta',
      'org.springframework.security.saml.metadata'

  error empty: ['grails.app.services.org.grails.plugin.resource',
                'grails.app.taglib.org.grails.plugin.resource',
                'grails.app.resourceMappers.org.grails.plugin.resource',
                'grails.app.resource.ResourceMeta']

  info 'grails.plugin.springsecurity.web.filter.DebugFilter'

  error 'org.codehaus.groovy.grails.web.servlet',  //  controllers
      'org.codehaus.groovy.grails.web.pages', //  GSP
      'org.codehaus.groovy.grails.web.sitemesh', //  layouts
      'org.codehaus.groovy.grails.web.mapping.filter', // URL mapping
      'org.codehaus.groovy.grails.web.mapping', // URL mapping
      'org.codehaus.groovy.grails.commons', // core / classloading
      'org.codehaus.groovy.grails.plugins', // plugins
      'org.codehaus.groovy.grails.orm.hibernate', // hibernate integration
      'org.springframework',
      'org.hibernate'

  debug 'org.springframework.security.saml'

  warn 'org.mortbay.log'
  environments {
    development {
      //info additivity: true, appLog: "grails.app"
    }
  }


  root {
    additivity = false
  }
}

Could someone please guide what I'm doing wrong? How can I stop the internal logs from appending?

dev-eloper
  • 110
  • 11
  • "How can I stop the internal logs from appending?" - Do you want to turn off all logging that comes from the framework and only include logging that comes from your application's code? – Jeff Scott Brown Jan 17 '22 at 17:21
  • @JeffScottBrown I think better to have just the error logs from the framework, and my application logs. These never used to appear before upgrading to log4j2 v.2.17.1. Not sure what exactly am doing wrong. I notice that the log4j config in Config.groovy is not being read. – dev-eloper Jan 18 '22 at 10:42
  • @JeffScottBrown I've updated the question with the log4j config present in Config.groovy. – dev-eloper Jan 18 '22 at 10:55
  • "I think better to have just the error logs from the framework, and my application logs" - If you mean that you want different logs for the framework vs your application code, then you should configure a separate appender for your app classes. – Jeff Scott Brown Jan 18 '22 at 14:23
  • @JeffScottBrown Is there a way I can disable these extra logs? – dev-eloper Jan 18 '22 at 16:48

1 Answers1

0

First, specifying an additivity on the root logger is meaningless. Please see Log4j 2 Configuration - Additivity for how additivity works.

Out of the box, Log4j 1 does not support configuration via Groovy. From what I can tell you are providing a basic log4j 1.x configuration and then the Groovy tool is modifying that configuration. I can't be sure what all of the syntax means without looking at the tools documentation. However, it seems pretty clear that the lines doing

  error 'org.codehaus.groovy.grails.web.servlet',  //  controllers
  'org.codehaus.groovy.grails.web.pages', //  GSP
  'org.codehaus.groovy.grails.web.sitemesh', //  layouts
  'org.codehaus.groovy.grails.web.mapping.filter', // URL mapping
  'org.codehaus.groovy.grails.web.mapping', // URL mapping
  'org.codehaus.groovy.grails.commons', // core / classloading
  'org.codehaus.groovy.grails.plugins', // plugins
  'org.codehaus.groovy.grails.orm.hibernate', // hibernate integration
  'org.springframework',
  'org.hibernate'

are declaring individual loggers at the error level.

So you would need to add lines to your configuration such as

logger.hibernate.name = org.hibernate
logger.hibernate.level = error

to match what your Groovy code is doing. If you do not specify the additivity it will default to true and the message will be routed to the appender associated with the root logger.

rgoers
  • 8,696
  • 1
  • 22
  • 24
  • Thank you for your suggestion. I've explicitly defined the loggers in the log4j2.properties file and most of the internal logs have disappeared. – dev-eloper Jan 24 '22 at 12:11
  • There are still some internal hibernate logs, `Jan 24, 2022 4:49:43 PM org.hibernate.tool.hbm2ddl.TableMetadata ` which are appearing during the application bootup. These are being displayed for the tables of my database, where they mention table found, keys, columns of the tables. Any idea what needs to be configured to disable these meta information logs? – dev-eloper Jan 24 '22 at 12:14
  • Please post your current configuration ti the Jira issue. That is a better support forum – rgoers Jan 24 '22 at 12:18