Change log4j properties at runtime

后端 未结 2 978
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 19:06

I need to change my log4j properties (rootLogger, MaxFileSize, etc.) at runtime. How can I do this?

2条回答
  •  暖寄归人
    2020-12-05 19:58

    Use LogManager.resetConfiguration(); to clear the current config and configure it again.

    Another approach is to build a new appender and replace the old one with it (most appenders don't support changing their config). This way, all the loggers (and their levels, etc) stay intact.

    For this to work, I usually add the first appender from code (and not with a config file). That allows me to save a reference which makes it more simple to remove it later.

提交回复
热议问题