Log4j2没有翻转文件,而是在SizeBasedTriggeringPolicy

时间:2016-12-10 14:12:33

标签: java json log4j2

我有一个以下JSON配置,它在达到最大大小限制后不会翻转,而是覆盖它。

{
  "configuration": {
    "name": "cool_log",
    "appenders": { "RollingFile": { "name": "rollingFile",
        "fileName": "/var/log/app_test.log",
        "filePattern": "/var/log/%d{MM-dd-yy-HH-mm-ss}-%i.log.gz",
        "JSONLayout": {
          "complete": false,
          "compact" : true,
          "eventEol": true
        },
        "Policies": { "SizeBasedTriggeringPolicy": { "size": "100 KB" } },
        "DefaultRolloverStrategy": { "max": "10" }
      }
    },
    "loggers": {
      "root": {
        "level": "DEBUG",
        "appender-ref": {
          "ref": "rollingFile"
        }
      }
    }
  }
}

我根据this official example

创建的
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
  <Appenders>
    <RollingFile name="RollingFile" fileName="var/log/app_test.log"
                 filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
      <PatternLayout>
        <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
      </PatternLayout>
      <Policies>
        <TimeBasedTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="250 MB"/>
      </Policies>
      <DefaultRolloverStrategy max="20"/>
    </RollingFile>
  </Appenders>
  <Loggers>
    <Root level="DEBUG">
      <AppenderRef ref="RollingFile"/>
    </Root>
  </Loggers>
</Configuration>

xml配置完美展开,但只有我失踪的是PatternLayout,对我来说是JSONLayout

log4j2的设置是,

public static void initialiseLoggingConfiguration() {
        try {
            PropertiesConfiguration logConfig = new PropertiesConfiguration("app.properties");
            System.out.println("Setting up log4j2 " + logConfig.getString("log4j2.config"));
            Configurator.initialize(null, logConfig.getString("log4j2.config"));
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
    }

和maven依赖,

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.6.2</version>
</dependency>

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.6.2</version>
</dependency>

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-web</artifactId>
    <version>2.6.2</version>
</dependency>

1 个答案:

答案 0 :(得分:1)

没关系,

    <Policies>
        <SizeBasedTriggeringPolicy size="1 KB"/>
    </Policies>

成为

"SizeBasedTriggeringPolicy": {
          "size": "1 KB"
        }

所以工作log4j2.json是,

{
  "configuration": {
    "name": "cool_log",
    "appenders": {
      "RollingFile": {
        "name": "rollingFile",
        "fileName": "logservice.log",
        "filePattern": "%d{MM-dd-yy-HH-mm-ss}-%i.log.gz",
        "JSONLayout": {
          "complete": false,
          "compact": true,
          "eventEol": true
        },
        "SizeBasedTriggeringPolicy": {
          "size": "1 KB"
        },
        "DefaultRolloverStrategy": {
          "max": "10"
        }
      }
    },
    "loggers": {
      "root": {
        "level": "DEBUG",
        "appender-ref": {
          "ref": "rollingFile"
        }
      }
    }
  }
}

参考

https://logging.apache.org/log4j/2.x/manual/configuration.html