折腾:
【未解决】用Java代码解析104协议收到的数据
期间,去找找java中好用的log的库
java log lib
说推荐:logging facade
比如:
- Apache Commons Logging
- SLF4J
SLF4J
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.
Log4J
Java Logging API
tinylog
Logback
Apache Commons Logging
SLF4J
- Log4j
- 用的比较广,2015年停止维护了
- Logback
- 同一作者,重写,为了提升性能
- 原生支持SLF4J
- Log4j2
- 最新,包含Log4j和Logback的优点,支持更多新特性
还是用Log4j2
log4j-slf4j-impl 和 log4j-to-slf4j 二选一,同时只能用其中一个
SLF4J
去加上配置
不过关于版本,尽量用新版本
->
->
最新的是2.13.0
The latest stable SLF4J is version 1.7.30.
而关于SLF4J版本与 log4j的版本匹配
- 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
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
<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>
最新稳定版是: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
<!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
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日志的库