How to stop Log4j2 from generating empty log file when the corresponding RollingFileAppender is programitically removed from LoggerConfig?

huangapple 未分类评论43阅读模式
英文:

How to stop Log4j2 from generating empty log file when the corresponding RollingFileAppender is programitically removed from LoggerConfig?

问题

在我的应用程序中,根据特定条件,我必须保留或移除一个RollingFileAppender。以下是我的log4j2.xml配置:

<Configuration>
    <Appenders>
        <Console name="LogToConsole">
            <PatternLayout pattern="[%t] %5p %-50c{2} - %m%n"/>
        </Console>
        <RollingFile name="LogToProfileRollingFile" fileName="${sys:DD_HOME}\logs\DDD\extract\server\Server-${sys:ddd.jvm.startTime}-Profile-log4j.log"
                     filePattern="${sys:DD_HOME}\logs\DDD\extract\server\Server-${sys:ddd.jvm.startTime}-Profile-log4j.log.%i">
            <PatternLayout>
                <Pattern>[%d{yyyy-MMM-dd HH:mm:ss,SSS}] [%t] %-5p %-50c{2}- %m%n</Pattern>
                <charset>UTF-8</charset>
            </PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="50KB"/>
            </Policies>
            <DefaultRolloverStrategy max="5"/>
        </RollingFile>
        <RollingFile name="LogToRollingFile" fileName="${sys:DD_HOME}\logs\DDD\extract\server\Server-${sys:DDD.jvm.startTime}-log4j.log"
                     filePattern="${sys:DD_HOME}\logs\DDD\extract\server\Server-${sys:ddd.jvm.startTime}-log4j.log.%i">
            <PatternLayout>
                <Pattern>[%d{yyyy-MMM-dd HH:mm:ss,SSS}] [%t] %-5p %-50c{2}- %m%n</Pattern>
                <charset>UTF-8</charset>
            </PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="50KB"/>
            </Policies>
            <DefaultRolloverStrategy max="5"/>
        </RollingFile>
    </Appenders>

    <Loggers>
        <Logger name="com.project.log.profile" level="INFO" additivity="false">
            <Appender-ref ref="LogToConsole"/>
            <Appender-ref ref="LogToProfileRollingFile"/>
            <Appender-ref ref="LogToRollingFile"/>
        </Logger>
        <Root level="info">
            <Appender-ref ref="LogToConsole"/>
            <Appender-ref ref="LogToRollingFile"/>
        </Root>
    </Loggers>
</Configuration>

通过以下方式在代码中动态移除了LogToProfileRollingFile appender:

LoggerContext context = LoggerContext.getContext(false);
Configuration configuration = context.getConfiguration();
LoggerConfig loggerConfig = configuration.getLoggerConfig("com.project.log.profile");
if (loggerConfig.getName().equals("com.project.log.profile")) {
    loggerConfig.removeAppender("LogToProfileRollingFile");
} else {
    throw new RuntimeException("There was no logger " + "loggerName");
}
context.updateLoggers();

现在,在成功移除LogToProfileRollingFile Appender后,日志不再输出到${sys:ddd.jvm.startTime}-Profile-log4j.log文件中,但是空文件仍然被创建。所以,以下是我的问题:

  • 如何阻止创建空的${sys:ddd.jvm.startTime}-Profile-log4j.log文件?
  • 另外,如何在日志文件名中解析和替换环境变量?对于系统属性(就像我在这里设置了ddd.jvm.startTime作为系统属性,并用sys:进行解析一样),这是起作用的。但如果我将它设置为用户或系统环境变量,我无法通过env:获取它。
英文:

In my application I have to keep or remove an RollingFileAppender based on certain condition. Below is my log4j2.xml

&lt;Configuration&gt;&lt;Appenders&gt;
    &lt;Console name=&quot;LogToConsole&quot;&gt;
        &lt;PatternLayout pattern=&quot;[%t] %5p %-50c{2} - %m%n&quot;/&gt;
    &lt;/Console&gt;
 &lt;RollingFile name=&quot;LogToProfileRollingFile&quot; fileName=&quot;${sys:DD_HOME}\logs\DDD\extract\server\Server-${sys:ddd.jvm.startTime}-Profile-log4j.log&quot;
                 filePattern=&quot;${sys:DD_HOME}\logs\DDD\extract\server\Server-${sys:ddd.jvm.startTime}-Profile-log4j.log.%i&quot;&gt;
               &lt;PatternLayout&gt;
			&lt;Pattern&gt;[%d{yyyy-MMM-dd HH:mm:ss,SSS}{}] [%t] %-5p %-50c{2}- %m%n&lt;/Pattern&gt;
			&lt;charset&gt;UTF-8&lt;/charset&gt;
		&lt;/PatternLayout&gt;
		&lt;Policies&gt;				
			&lt;SizeBasedTriggeringPolicy size=&quot;50KB&quot;/&gt;				
		&lt;/Policies&gt;
		&lt;DefaultRolloverStrategy max=&quot;5&quot;/&gt;
	&lt;/RollingFile&gt;		
	&lt;RollingFile name=&quot;LogToRollingFile&quot; fileName=&quot;${sys:DD_HOME}\logs\DDD\extract\server\Server-${sys:DDD.jvm.startTime}-log4j.log&quot;
                 filePattern=&quot;${sys:DD_HOME}\logs\DDD\extract\server\Server-${sys:ddd.jvm.startTime}-log4j.log.%i&quot;&gt;
              &lt;PatternLayout&gt;
			&lt;Pattern&gt;[%d{yyyy-MMM-dd HH:mm:ss,SSS}{}] [%t] %-5p %-50c{2}- %m%n&lt;/Pattern&gt;
			&lt;charset&gt;UTF-8&lt;/charset&gt;
		&lt;/PatternLayout&gt;
		&lt;Policies&gt;				
			&lt;SizeBasedTriggeringPolicy size=&quot;50KB&quot;/&gt;				
		&lt;/Policies&gt;
		&lt;DefaultRolloverStrategy max=&quot;5&quot;/&gt;
	&lt;/RollingFile&gt;		
&lt;/Appenders&gt;    

&lt;Loggers&gt;        
    &lt;Logger name=&quot;com.project.log.profile&quot; level=&quot;INFO&quot;
        additivity=&quot;false&quot;&gt;
        &lt;Appender-ref ref=&quot;LogToConsole&quot;/&gt;
        &lt;Appender-ref ref=&quot;LogToProfileRollingFile&quot;/&gt;
        &lt;Appender-ref ref=&quot;LogToRollingFile&quot;/&gt;         
    &lt;/Logger&gt;
     &lt;Root level=&quot;info&quot;&gt;
          &lt;Appender-ref ref=&quot;LogToConsole&quot;/&gt;	          
          &lt;Appender-ref ref=&quot;LogToRollingFile&quot;/&gt;
    &lt;/Root&gt;
&lt;/Loggers&gt;    &lt;/Configuration&gt;

Removed the LogToRollingFile appender programmatically in the following way -

LoggerContext context = LoggerContext.getContext(false);
		Configuration configuration = context.getConfiguration();
		LoggerConfig loggerConfig = configuration.getLoggerConfig(&quot;com.project.log.profile&quot;);
		if (loggerConfig.getName().equals(&quot;com.project.log.profile&quot;)) {
		    loggerConfig.removeAppender(&quot;LogToProfileRollingFile&quot;);
		} else {
		    throw new RuntimeException(&quot;There was no logger &quot; + &quot;loggerName&quot;);
		}
		context.updateLoggers();

Now, after successfully removing the LogToProfileRollingFile Appender, the logs are not getting printed on ${sys:ddd.jvm.startTime}-Profile-log4j.log, but the empty file is getting created. So, below are my questions

  • How to stop this empty ${sys:ddd.jvm.startTime}-Profile-log4j.log file from getting created
  • Also, how to evaluate and replace environment variables in the log file name. It is working for system propertis(like here I have set ddd.jvm.startTime as system property and evaluating it with sys:). But if I set it as an user or system environment variable, I am not able to get it through env:

huangapple
  • 本文由 发表于 2020年7月24日 00:37:40
  • 转载请务必保留本文链接:https://java.coder-hub.com/63059074.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定