How to configure Grails plugin to override Log4J s

2019-08-10 06:49发布

I'm writing a Grails plugin that will impose very specific log4j settings (in its own log4j.xml), and I would like to configure it such that an app which uses this plugin is forced to use the plugin's log4j configuration (instead of any log4j configs that the app itself has defined).

For example, let's say I want the plugin to force all downstream Grails apps to log to a file called /var/log/fizz.log. How could I make this work?

Update: And if it's not possible for the plugin to override the app that uses it, then at the very least I need the app to be able to use the plugin's log4j config without having to define its own.

1条回答
Juvenile、少年°
2楼-- · 2019-08-10 07:14

First, the application configuration settings will always trump the plugin settings. You can't avoid that (as far as I know).

However, you can always remove the log4j configuration from the application's config.groovy and make your plugin wire in the log4j configuration within the doWithSpring event of the plugin's lifecycle. This will ensure that the plugin is responsible for configuring log4j.

I will assume you don't need any help wiring up the log4j.xml portion since you didn't ask about that. Just in case, that might look something like this:

MyLoggingPlugin.groovy

...
def doWithSpring = {
  ...
      log4jConfigurer(org.springframework.beans.factory.config.MethodInvokingFactoryBean){
        targetClass = "org.springframework.util.Log4jConfigurer"
        targetMethod = "initLogging"
        arguments = "path/to/log4j.xml"
      }
  ...
}
...
查看更多
登录 后发表回答