1

我正在使用 Log4j/log4j extras 来记录日志。这里是配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>

<appender name="fileAppender" class="org.apache.log4j.rolling.RollingFileAppender">
    <param name="append" value="true" />

    <rollingPolicy class="org.apache.log4j.rolling.FixedWindowRollingPolicy">
      <param name="minIndex" value="1"/>
      <param name="maxIndex" value="10"/>
      <param name="FileNamePattern" value="/appserver/Logs/Logs1/log.%i.gz"/>

</rollingPolicy>
 <triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
    <param name="maxFileSize" value="1000000000"/><!-- 1GB -->
</triggeringPolicy>
    <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d{dd MMM yy HH:mm:ss:SSS}] [%p] - [%t %l]- %m%n" />
    </layout>

</appender>
<!--disable logging from the library Jars -->
<category name="org.apache.velocity">
    <priority value="off" />
</category>

<category name="com.someting.www">
    <priority value="off" />
</category>
<category name="org.apache">
    <priority value="off" />
</category>

<root>
    <level value="DEBUG" />
    <appender-ref ref="fileAppender" />
</root>
</log4j:configuration>

我希望日志仅出现在文件中,但我将日志写入文件和控制台中。请提出更改以获得预期结果。

4

1 回答 1

0

我在我的新项目中使用 log4j2。我正在将日志写入文件和控制台中。这是我的 log4j2.xml:

<properties>
    <property name="logPath">/opt/weego_main/logs</property>
</properties>

<Appenders>
    <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n"/>
    </Console>

    <RollingFile name="RollingFile" filename="${logPath}/weego_main.log"
                 filepattern="${logPath}/$${date:yyyy-MM}/weego_main-%d{yyyy-MM-dd}-%i.log.gz">
        <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n"/>
        <Policies>
            <TimeBasedTriggeringPolicy/>
            <SizeBasedTriggeringPolicy size="20 MB"/>
        </Policies>
    </RollingFile>
</Appenders>

<Loggers>
    <Root level="info">
        <AppenderRef ref="Console"/>
        <AppenderRef ref="RollingFile"/>
    </Root>
</Loggers>

我认为您应该创建名为 Console 的 Appenders 节点并将其添加到 Root 节点。

于 2016-03-21T09:30:17.037 回答