log4j2 not writing to the log file with rollingFile appenders

為{幸葍}努か 提交于 2019-12-11 06:31:34

问题


Hi i am new using log4j

I am trying to configure the xml to send those logging into the log file using the and appender, well, the appender works fine so i want to use appender so that it will clear the log file daily, but when it first created the log file, it doesn't write any log message into the file created by , so i come here to get some advise

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="FATAL" >

<Appenders>
    <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout pattern="${pattern}" />
    </Console>

    <RollingFile name="rollingfile" fileName="rolling.log"
    filePattern="rolling-%d{MM-dd-yyyy}.log">
        <PatternLayout pattern="${pattern}" />
        <Policies>
              <TimeBasedTriggeringPolicy interval="1" modulate="true" />
              <SizeBasedTriggeringPolicy size="10 MB" />
        </Policies>
    </RollingFile>

    <File name="file" fileName="test2.log">
        <PatternLayout pattern="${pattern}" />
    </File>     

</Appenders>


<Loggers>   
    <Root level="trace" >
        <AppenderRef ref="Console" />
        <AppenderRef ref="rollingfile" />
        <AppenderRef ref="file" />
    </Root>
</Loggers>
</Configuration>

回答1:


You have AppenderRef="rolling", but your Appender is named "rollingfile". These need to match.

Also, log4j probably issues a warning that there is a problem with the configuration. I recommend you specify <Configuration status="WARN" > at the start of your config instead of FATAL so that you can see these warnings.

It may be that you need to specify ${sys:pattern} to read the system property. Instead of the ${pattern} variable, first try an explicit pattern like %d %p [%t] %m%n.



来源:https://stackoverflow.com/questions/36996935/log4j2-not-writing-to-the-log-file-with-rollingfile-appenders

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