使用DailyRollingFileAppender的Log4j MaxFileSize

时间:2018-03-19 14:29:38

标签: java log4j log4j2

我的目标是每天生成日志文件,但MaxFileSize=100KB

所以我使用DailyRollingFileAppender,因为我使用的是log4j 2.2版本

日期工作正常,当我使用MaxFileSize时,它没有工作,并且没有基于filesize创建新文件仍然可以使用日期。

问题:MaxFileSize无法使用DatePattern for DailyRollingfileAppender

这是log4j.properties文件代码:

log4j.rootLogger=DEBUG, stdout, file

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender  
log4j.appender.file.layout=org.apache.log4j.PatternLayout  
log4j.appender.file.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n
log4j.appender.file.File=${catalina.home}/logs/abc
log4j.appender.file.MaxFileSize=80KB
log4j.appender.file.MaxBackupIndex=2
log4j.appender.file.DatePattern='-'yyyy-MM-dd'.log'

有些东西需要在日期模式中更改,但还不知道

2 个答案:

答案 0 :(得分:1)

尝试以下配置,根据时间和大小旋转文件 -

status = warn
name= properties_configuration

property.basePath = ${catalina.home}/logs/

appender.console.type = Console
appender.console.name = consoleLogger
appender.console.target = SYSTEM_OUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %level %c{1}:%L - %msg%n
appender.rolling.type = RollingFile
appender.rolling.name = fileLogger
appender.rolling.fileName=${basePath}/abc.log
appender.rolling.filePattern=${basePath}abc_%d{yyyyMMdd}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %level (%F:%L) - %msg%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size = 100 KB

# write your package name as the value of this key
logger.example.name = log4j2.example
logger.example.level = info
logger.example.additivity = false
logger.example.appenderRef.rolling.ref = fileLogger
logger.example.appenderRef.console.ref = consoleLogger

rootLogger.level = error
rootLogger.additivity = false
rootLogger.appenderRef.rolling.ref = fileLogger
rootLogger.appenderRef.console.ref = consoleLogger

答案 1 :(得分:0)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Configuration>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" />
        </Console>

        <!-- Rolling File Appender -->
        <RollingFile name="RollingFile">
            <FileName>C:/logs/x2c.log</FileName>
            <FilePattern>C:/logs/%d{yyyy-MM-dd-hh-mm}-%i.log</FilePattern>
            <PatternLayout>
                <Pattern>style{%d{yyyy-MMM-dd HH:mm:ss a}}{black} %-5p %style{[%t]}{blue} %highlight{%level}{FATAL=bg_red, ERROR=red, WARN=yellow, INFO=green, DEBUG=blue} %logger{36} - %msg%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="2"
                    modulate="true" />
                    <SizeBasedTriggeringPolicy size="80 KB" />
            </Policies>
            <DefaultRolloverStrategy max="5" />
        </RollingFile>

    </Appenders>
    <Loggers>
        <Logger name="com.mkyong.common.controller" level="debug"
            additivity="false">
            <AppenderRef ref="RollingFile" />
            <AppenderRef ref="Console" />
        </Logger>
        <Root level="trace">
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>

</Configuration><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Configuration>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" />
        </Console>

        <!-- Rolling File Appender -->
        <RollingFile name="RollingFile">
            <FileName>C:/logs/x2c.log</FileName>
            <FilePattern>C:/logs/%d{yyyy-MM-dd-hh-mm}-%i.log</FilePattern>
            <PatternLayout>
                <Pattern>style{%d{yyyy-MMM-dd HH:mm:ss a}}{black} %-5p %style{[%t]}{blue} %highlight{%level}{FATAL=bg_red, ERROR=red, WARN=yellow, INFO=green, DEBUG=blue} %logger{36} - %msg%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="2"
                    modulate="true" />
                    <SizeBasedTriggeringPolicy size="80 KB" />
            </Policies>
            <DefaultRolloverStrategy max="5" />
        </RollingFile>

    </Appenders>
    <Loggers>
        <Logger name="com.mkyong.common.controller" level="debug"
            additivity="false">
            <AppenderRef ref="RollingFile" />
            <AppenderRef ref="Console" />
        </Logger>
        <Root level="trace">
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>

</Configuration><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Configuration>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" />
        </Console>

        <!-- Rolling File Appender -->
        <RollingFile name="RollingFile">
            <FileName>C:/logs/x2c.log</FileName>
            <FilePattern>C:/logs/%d{yyyy-MM-dd-hh-mm}-%i.log</FilePattern>
            <PatternLayout>
                <Pattern>style{%d{yyyy-MMM-dd HH:mm:ss a}}{black} %-5p %style{[%t]}{blue} %highlight{%level}{FATAL=bg_red, ERROR=red, WARN=yellow, INFO=green, DEBUG=blue} %logger{36} - %msg%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="2"
                    modulate="true" />
                    <SizeBasedTriggeringPolicy size="80 KB" />
            </Policies>
            <DefaultRolloverStrategy max="5" />
        </RollingFile>

    </Appenders>
    <Loggers>
        <Logger name="com.mkyong.common.controller" level="debug"
            additivity="false">
            <AppenderRef ref="RollingFile" />
            <AppenderRef ref="Console" />
        </Logger>
        <Root level="trace">
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>

</Configuration><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Configuration>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" />
        </Console>

        <!-- Rolling File Appender -->
        <RollingFile name="RollingFile">
            <FileName>C:/logs/x2c.log</FileName>
            <FilePattern>C:/logs/%d{yyyy-MM-dd-hh-mm}-%i.log</FilePattern>
            <PatternLayout>
                <Pattern>style{%d{yyyy-MMM-dd HH:mm:ss a}}{black} %-5p %style{[%t]}{blue} %highlight{%level}{FATAL=bg_red, ERROR=red, WARN=yellow, INFO=green, DEBUG=blue} %logger{36} - %msg%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="2"
                    modulate="true" />
                    <SizeBasedTriggeringPolicy size="80 KB" />
            </Policies>
            <DefaultRolloverStrategy max="5" />
        </RollingFile>

    </Appenders>
    <Loggers>
        <Logger name="com.mkyong.common.controller" level="debug"
            additivity="false">
            <AppenderRef ref="RollingFile" />
            <AppenderRef ref="Console" />
        </Logger>
        <Root level="trace">
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>

</Configuration>

这是基于xml的配置,它工作正常,但确切的属性或基本配置不起作用。文件未创建且没有控制台输出