最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】java的log4j2中没有生成File类型的log文件

Java crifan 152浏览 0评论
折腾:
【未解决】java中好用的log日志的库
期间,用配置:
src/xxx/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="
http://maven.apache.org/POM/4.0.0
"
         xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance
"
         xsi:schemaLocation="
http://maven.apache.org/POM/4.0.0
 
http://maven.apache.org/xsd/maven-4.0.0.xsd
">
    <modelVersion>4.0.0</modelVersion>

    <groupId>iec-analysis</groupId>
    <artifactId>analysis</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <
maven.compiler.source
>1.8</
maven.compiler.source
>
        <
maven.compiler.target
>1.8</
maven.compiler.target
>
    </properties>

    <!--    引入junit,使用单元测试-->
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <!-- 
Java Logging Frameworks: log4j vs logback vs log4j2
-->         <dependency>             <groupId>org.apache.logging.log4j</groupId>             <artifactId>log4j-api</artifactId>             <version>2.13.0</version>         </dependency>         <dependency>             <groupId>org.apache.logging.log4j</groupId>             <artifactId>log4j-core</artifactId>             <version>2.13.0</version>             </dependency>         <dependency>             <groupId>org.apache.logging.log4j</groupId>             <artifactId>log4j-slf4j-impl</artifactId>             <version>2.13.0</version>         </dependency>         <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->         <dependency>             <groupId>org.slf4j</groupId>             <artifactId>slf4j-api</artifactId>             <version>1.7.30</version>         </dependency>     </dependencies> </project>
src/xxx/log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Appenders>
    <File name="FILE_OUT" fileName="xxx.log" append="true">
      <!-- <Log4j1XmlLayout /> -->
      <PatternLayout>
        <Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %m%nw</Pattern>
      </PatternLayout>
    </File>


    <Console name="STDOUT" target="SYSTEM_OUT">
      <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
    </Console>
  </Appenders>


  <Loggers>
    <Logger name="org.apache.log4j.xml" level="info"/>
    <Root level="debug">
      <AppenderRef ref="STDOUT"/>
      <AppenderRef ref="FILE_OUT"/>
    </Root>
  </Loggers>
</Configuration>
调试输出效果:
即:
  • console的log:输出了
    • 但是info(和debug?)没输出,好像不对
  • file的log:没有生成log文件
【】
去改了参数:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Appenders>
    <File name="fout" fileName="xxx.log" append="true">
      <Log4j1XmlLayout />
      <!-- <PatternLayout>
        <Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %m%nw</Pattern>
      </PatternLayout> -->
    </File>


    <Console name="stdout" target="SYSTEM_OUT">
      <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
    </Console>
  </Appenders>


  <Loggers>
    <!-- <Logger name="org.apache.log4j.xml" level="info"/> -->
    <Root level="INFO">
      <AppenderRef ref="stdout"/>
      <AppenderRef ref="fout"/>
    </Root>
  </Loggers>
</Configuration>
问题依旧,还是log日志没有产生
    <Logger name="com.iec.test.Analysis104Test" level="trace">
      <AppenderRef ref="fout"/>
    </Logger>
问题依旧。
Log4j – Configuring Log4j 2
<File name="File1" fileName="xxx.log" bufferedIO="false" advertiseURI="
file://Users/crifan/dev/dev_root/projects/xxx/debug/xxx.log
" advertise="true">
结果:
问题依旧。
<File name="fout" fileName="xxx.log" bufferedIO="false" advertiseURI="
file:///Users/crifan/dev/dev_root/projects/xxx/debug/xxx.log
" advertise="true">
结果:
问题依旧。
调试半天,发现个奇怪的事情:
public class Analysis104Test {
    // private static Logger logger = LogManager.getLogger(Analysis104Test.class);
    // private static Logger logger = LogManager.getLogger(Analysis104Test.class.getName());
    private static Logger logger = LogManager.getRootLogger();

    @Test
    public void analysis() {
        logger.debug("Debug log message");
        logger.info("Info log message");
        logger.error("Error log message");
        logger.trace("Trace log message");
此处class Analysis104Test中的全局变量:
    private static Logger logger = LogManager.getRootLogger();
竟然没有被运行到。
之前偶尔怀疑,是JUnit的问题。
使得class的private的变量都初始化
所以放到代码里:
public class Analysis104Test {


    @Test
    public void analysis() {
        // private static Logger logger = LogManager.getLogger(Analysis104Test.class);
        // private static Logger logger = LogManager.getLogger(Analysis104Test.class.getName());
        // private static Logger logger = LogManager.getRootLogger();


        Logger logger = LogManager.getLogger(Analysis104Test.class);


        logger.debug("Debug log message");
        logger.info("Info log message");
        logger.error("Error log message");
        logger.trace("Trace log message");
结果:
还是没日志文件生成。
log4j2 not creating file
logging – Why isn’t log4j2 generating a log file? – Stack Overflow
java – Log4j2 not creating log files – Stack Overflow
没有把配置文件加到java能找到的classpath中?
Log4j – Frequently Asked Questions
“How do I specify the configuration file location?
By default, Log4j looks for a configuration file named log4j2.xml (not log4j.xml) in the classpath.
You can also specify the full path of the configuration file with this system property: -Dlog4j.configurationFile=path/to/log4j2.xml”
把此处的配置文件改为:
src/xxx/log4j2.xml
然后再去运行看看
问题依旧。
不过要去搞清楚,如何指定xml到classpath中,让系统找到:
【未解决】VSCode中把log4j加到maven的classpath中
实在不行,还是去放到,太多人说的
比如:
If using maven, usually you put log4j.properties under java or resources? – Stack Overflow
src/main/resources
java – Where does maven search for log4j.properties file? – Stack Overflow
“The file needs to go into src/main/resources/ or src/test/resources/ (if you need it only for unit tests).”
特定位置:
src/main/resources
即:
src/main/resources/log4j2.xml
然后去调试,果然配置文件立刻就生效了:
console中:
2020-01-15 20:58:59,619 main ERROR File contains an invalid element or attribute "Log4j1XmlLayout"
2020-01-15 20:59:04,906 DEBUG [main] test.Analysis104Test (Analysis104Test.java:29) - Debug log message
2020-01-15 20:59:06,843 INFO [main] test.Analysis104Test (Analysis104Test.java:30) - Info log message
2020-01-15 20:59:08,035 ERROR [main] test.Analysis104Test (Analysis104Test.java:31) - Error log message
log文件xxx.log 中
Debug log message
Info log message
Error log message
再去更新和调整配置
src/xxx/src/main/resources/log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Appenders>
    <File name="FILEOUT" fileName="xxx.log" append="false">
      <PatternLayout>
        <Pattern>%d{yyyyMMdd HH:mm:ss} %-5p [%t] %C{2} %F:%L - %m%n</Pattern>
      </PatternLayout>
    </File>


    <Console name="STDOUT" target="SYSTEM_OUT">
      <PatternLayout pattern="%d %-5p %F:%L - %m%n"/>
    </Console>
  </Appenders>


  <Loggers>
    <Root level="debug">
      <AppenderRef ref="FILEOUT"/>
      <AppenderRef ref="STDOUT"/>
    </Root>
  </Loggers>
</Configuration>
输出效果:
文件xxx.log:
20200115 21:15:22 DEBUG [main] test.Analysis104Test Analysis104Test.java:29 - Debug log message
20200115 21:15:22 INFO [main] test.Analysis104Test Analysis104Test.java:30 - Info log message
20200115 21:15:22 ERROR [main] test.Analysis104Test Analysis104Test.java:31 - Error log message
console中:
2020-01-15 21:15:22,346 DEBUG Analysis104Test.java:29 - Debug log message
2020-01-15 21:15:22,349 INFO Analysis104Test.java:30 - Info log message
2020-01-15 21:15:22,350 ERROR Analysis104Test.java:31 - Error log message
目前凑合这么用吧。

转载请注明:在路上 » 【已解决】java的log4j2中没有生成File类型的log文件

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
92 queries in 0.118 seconds, using 20.62MB memory