How to configure Grails plugin to override Log4J settings for “parent” apps?

限于喜欢 提交于 2019-12-11 04:53:02

问题


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:


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"
      }
  ...
}
...


来源:https://stackoverflow.com/questions/23660006/how-to-configure-grails-plugin-to-override-log4j-settings-for-parent-apps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!