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

【已解决】java中好用的log日志的库

Java crifan 1961浏览 0评论
折腾:
【未解决】用Java代码解析104协议收到的数据
期间,去找找java中好用的log的库
java log lib
logging – Logger for Java library – Stack Overflow
说推荐:logging facade
比如:
SLF4J
http://www.slf4j.org
The Simple Logging Facade for Java (SLF4J) serves as a simple facade or abstraction for various logging frameworks (e.g. java.util.logging, logback, log4j) allowing the end user to plug in the desired logging framework at deployment time.
Before you start using SLF4J, we highly recommend that you read the two-page SLF4J user manual.
SLF4J Manual
Logging in “Java Library Code” libs for Android applications – Stack Overflow
logging – Create a Logger Library in java – Stack Overflow
Java logging framework – Wikipedia
Log4J
Java Logging API
tinylog
Logback
Apache Commons Logging
SLF4J
Log4j – Apache Log4j 2
Java Logging Frameworks: log4j vs logback vs log4j2
  • Log4j
    • 用的比较广,2015年停止维护了
  • Logback
    • 同一作者,重写,为了提升性能
      • 原生支持SLF4J
  • Log4j2
    • 最新,包含Log4j和Logback的优点,支持更多新特性
还是用Log4j2
Introduction to Java Logging | Baeldung
Log4j – Log4j 2 Appenders
The Central Repository Search Engine
Log4j – Download Apache Log4j 2
Log4j Provider Using SLF4J – Log4j to SLF4J Adapter
SLF4J Binding Using Log4j – Log4j 2 SLF4J Binding
Log4j – Log4j Runtime Dependencies
log4j-slf4j-impl 和 log4j-to-slf4j 二选一,同时只能用其中一个
SLF4J
http://www.slf4j.org
去加上配置
不过关于版本,尽量用新版本
Log4j – Download Apache Log4j 2
->
->
Apache Download Mirrors
->
Index of /apache/logging/log4j/2.13.0
最新的是2.13.0
SLF4J Binary files
The latest stable SLF4J is version 1.7.30.
Central Repository: org/slf4j
Central Repository: org/slf4j/slf4j-log4j13
Central Repository: org/slf4j/nlog4j
Central Repository: org/slf4j/log4j-over-slf4j
而关于SLF4J版本与 log4j的版本匹配
SLF4J Binding Using Log4j – Log4j 2 SLF4J Binding
  • log4j-slf4j-impl should be used with SLF4J 1.7.x releases or older.
  • log4j-slf4j18-impl should be used with SLF4J 1.8.x releases or newer.
至此:
  • SLF4J:用最新稳定版 1.7.30
    • 属于1.7.x
      • 用 log4j-slf4j-impl
去找最新版log4j-slf4j-impl
log4j-slf4j-impl
Maven Repository: org.apache.logging.log4j » log4j-slf4j-impl
Home » org.apache.logging.log4j » log4j-slf4j-impl
Apache Log4j SLF4J Binding
The Apache Log4j SLF4J API binding to Log4j 2 Core
最新版:2.13.0
SLF4J Binding Using Log4j – Log4j 2 SLF4J Binding
SLF4J Binding Using Log4j – Dependency Information
        <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>
Maven Repository: SLF4J
Maven Repository: org.slf4j
Maven Repository: org.slf4j » slf4j-api
最新稳定版是:1.7.30
Maven Repository: org.slf4j » slf4j-api » 1.7.30
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.30</version>
</dependency>
加起来就是:
/Users/crifan/dev/dev_root/projects/xxx/src/xxx/pom.xml
        <!-- https://stackify.com/compare-java-logging-frameworks/ -->
        <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>
然后去看看如何编译和更新
再去加上配置
log4j.xml
log4j log4j.xml
Log4J.xml配置详解_亘易的专栏-CSDN博客
log4j.xml example – Log4j xml configuration example – HowToDoInJava
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'>


  <appender name="console" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out"/>
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
    </layout>
  </appender>


  <appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="demoApplication.log"/>
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
    </layout>
  </appender>


  <root>
    <priority value ="debug"></priority>
    <appender-ref ref="console"></appender>
    <appender-ref ref="fileAppender"></appender>
  </root>


</log4j:configuration>
会报错:
改用:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
  <Appenders>
    <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"/>
    </Root>
  </Loggers>
</Configuration>
 
然后:
【已解决】java的log4j2中没有生成File类型的log文件
另外可以看到
Java Dependencies
的Maven Dependencies
依赖的库有log4j2
再去看看,能否改为SLF4J
Apache Log4j 2 Tutorials – Mkyong.com
https://mkyong.com/logging/apache-log4j-2-tutorials/
SLF4j Vs Log4j – Which one is better? – HowToDoInJava
src/xxx/src/test/java/com/iec/test/Analysis104Test.java
// import org.apache.logging.log4j.Logger;
// import org.apache.logging.log4j.LogManager;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class Analysis104Test {


    @Test
    public void analysis() {
        // log4j2
        // Logger logger = LogManager.getLogger(Analysis104Test.class);
        
        // LSF4J
        Logger logger = LoggerFactory.getLogger(Analysis104Test.class);


        logger.debug("Debug log message");
        logger.info("Info log message");
        logger.error("Error log message");
        logger.trace("Trace log message");
即可正常输出:
【总结】
目前用如下配置:
src/xxx/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project ...
    <dependencies>
...
        <!-- https://stackify.com/compare-java-logging-frameworks/ -->
        <dependency> 
            <groupId>org.apache.logging.log4j</groupId> 
            <artifactId>log4j-api</artifactId> 
            <version>${log4j2.version}</version> 
        </dependency> 
        <dependency> 
            <groupId>org.apache.logging.log4j</groupId> 
            <artifactId>log4j-core</artifactId> 
            <version>${log4j2.version}</version> 
            </dependency> 
        <dependency> 
            <groupId>org.apache.logging.log4j</groupId> 
            <artifactId>log4j-slf4j-impl</artifactId> 
            <version>${log4j2.version}</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/main/resources 下面的 log4j2.xml:
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>
代码:
src/xxx/src/test/java/com/iec/test/Analysis104Test.java

// import org.apache.logging.log4j.Logger;
// import org.apache.logging.log4j.LogManager;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class Analysis104Test {


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


        // log4j2
        // Logger logger = LogManager.getLogger(Analysis104Test.class);
        
        // LSF4J
        // Logger logger = LoggerFactory.getLogger(Analysis104Test.class);
        Logger logger = LoggerFactory.getLogger(this.getClass());


        logger.debug("Debug log message");
        logger.info("Info log message");
        logger.error("Error log message");
        logger.trace("Trace log message");
即可正常输出log到
  • log文件:xxx.log
  • console:VSCode调试的console

转载请注明:在路上 » 【已解决】java中好用的log日志的库

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.183 seconds, using 22.14MB memory