Log4j2 - RootLogger appender无法正常工作

时间:2017-11-04 17:37:37

标签: java maven logging log4j2

我有一个使用log4j2进行日志记录的maven项目。我想覆盖RootLogger的模式。

这是我的log4j2.properties文件:

status = debug
name= properties_configuration

# Give directory path where log files should get stored
property.basePath = ./log/

appenders =console, rolling

# ConsoleAppender will print logs on console
appender.console.type = Console
appender.console.name = consoleLogger
appender.console.target = SYSTEM_OUT
appender.console.layout.type = PatternLayout

# Specify the pattern of the logs
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] 
[%c] [%M] [%l] - %msg %xEx %n


# RollingFileAppender will print logs in file which can be rotated based on 
time or size
appender.rolling.type = RollingFile
appender.rolling.name = fileLogger
appender.rolling.fileName=${basePath}/app.log
appender.rolling.filePattern=${basePath}app_%d{yyyyMMdd}.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] 
[%c] [%M] [%l] - %msg %xEx %n
appender.rolling.policies.type = Policies

# Rotate log file each day
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.delete.type = Delete
appender.rolling.strategy.delete.basePath = ${basePath}
appender.rolling.strategy.delete.maxDepth = 1
appender.rolling.strategy.delete.ifLastModified.type = IfLastModified
# Delete files older than 30 days
appender.rolling.strategy.delete.ifLastModified.age = 30d


logger.example.name = com.parohia
logger.example.level = debug
logger.example.appenderRef.rolling.ref = fileLogger
logger.example.appenderRef.console.ref = consoleLogger

rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = consoleLogger

这是我对log4j的pom.xml文件:

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

我这样称呼我的记录器:

      private final Logger logger = LogManager.getLogger(PersoaneController.class);
      try{
          .....
      }
      catch(Exception ex){
           logger.error(ex.getMessage(),ex);
      }

我的配置有2个问题。第一个是根记录器不使用我的consoleAppender(警告等以默认格式出现),当记录错误时,我只得到第一行而不是整个堆栈跟踪。)

1 个答案:

答案 0 :(得分:0)

如果您的模式实际上在您的属性文件中分成多行,如问题所示:

appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] 
[%c] [%M] [%l] - %msg %xEx %n

appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] 
[%c] [%M] [%l] - %msg %xEx %n

然后你需要改变它们,使得模式完全在一行上:

appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] [%c] [%M] [%l] - %msg %xEx %n

appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] [%c] [%M] [%l] - %msg %xEx %n