非常简单的log4j2 XML配置文件使用控制台和文件附加器 [英] Very simple log4j2 XML configuration file using Console and File appender

查看:411
本文介绍了非常简单的log4j2 XML配置文件使用控制台和文件附加器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个非常简单的XML配置文件,其中包含控制台和使用log4j2的文件附加程序。

I'd like a very simple XML configuration file with a console and a file appender using log4j2.

(Apache网站正在杀死我很多信息。

(The Apache Website is killing me with much Information.)

推荐答案

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
        <File name="MyFile" fileName="all.log" immediateFlush="false" append="false">
            <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </File>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console" />
            <AppenderRef ref="MyFile"/>
        </Root>
    </Loggers>
</Configuration>

注意


  • 将以下内容放入您的配置文件。

  • 将配置文件命名为log4j2.xml


  • 使用 Logger logger = LogManager.getLogger(); 以初始化您的记录器

  • 我设置了 immediateFlush =false >。如果您需要在日志文件中立即删除日志,请删除参数或将其设置为true

  • Put the following content in your configuration file.
  • Name the configuration file log4j2.xml
  • Put the log4j2.xml in a folder which is in the class-path (i.e. your source folder "src")
  • Use Logger logger = LogManager.getLogger(); to initialize your logger
  • I did set the immediateFlush="false" since this is better for SSD lifetime. If you need the log right away in your log-file remove the parameter or set it to true

这篇关于非常简单的log4j2 XML配置文件使用控制台和文件附加器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆