最近看到一本小书《LOGGING FRAMEWORKS IN JAVA》,写些代码练个手。
主文件:
package com.example.demo;
import org.slf4j.LoggerFactory;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
public class DemoApplication {
private static final Logger logger = Logger.getLogger(DemoApplication.class.getName());
private static final LogManager logManager = LogManager.getLogManager();
private static final Logger LOGGER = Logger.getLogger("com.example.demo");
private static final org.slf4j.Logger log = LoggerFactory.getLogger(DemoApplication.class);
static {
try {
logManager.readConfiguration(new FileInputStream("D:\\test\\demo\\src\\main\\resources\\logging.properties"));
} catch (IOException exception) {
LOGGER.log(Level.SEVERE, "Error in loading configuration", exception);
}
}
public static void main(String[] args) throws SecurityException, IOException {
// final Logger logger = Logger.getLogger(DemoApplication.class.getName());
FileHandler fileHandler = new FileHandler();
logger.addHandler(fileHandler);
logger.setLevel(Level.ALL);
System.out.println("Hi! It's my first Java Program.");
logger.info("My first Log statement.");
logger.severe("Severe message");
logger.warning("Warning Message");
logger.severe("Another Severs message");
int a = 5;
int b = 2;
try {
int c = a / b;
System.out.println("result: " + c);
} catch (ArithmeticException ex) {
logger.log(Level.SEVERE, "Arithmethic Exception", ex);
}
int arr[] = {1, 2, 3};
try {
for (int i = 3; i > 0; i--) {
System.out.println(arr[i]);
}
} catch (ArrayIndexOutOfBoundsException e) {
logger.log(Level.SEVERE, "Array Index is out of bounds.", e);
}
}
}
日志配置文件:
handlers=java.util.logging.ConsoleHandler
.level=INFO
com.example.demo.level= ALL
java.util.logging.ConsoleHandler.level = ALL
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.level = SEVERE
java.util.logging.FileHandler.pattern = myFile.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
输出:
D:\Java\jdk17\bin\java.exe "-javaagent:D:\JetBrains\IntelliJ IDEA Community Edition 2024.3.1.1\lib\idea_rt.jar=1589:D:\JetBrains\IntelliJ IDEA Community Edition 2024.3.1.1\bin" -Dfile.encoding=UTF-8 -classpath D:\test\demo\target\classes;C:\Users\ccc\.m2\repository\org\springframework\boot\spring-boot-starter\3.3.7\spring-boot-starter-3.3.7.jar;C:\Users\ccc\.m2\repository\org\springframework\boot\spring-boot\3.3.7\spring-boot-3.3.7.jar;C:\Users\ccc\.m2\repository\org\springframework\spring-context\6.1.16\spring-context-6.1.16.jar;C:\Users\ccc\.m2\repository\org\springframework\spring-aop\6.1.16\spring-aop-6.1.16.jar;C:\Users\ccc\.m2\repository\org\springframework\spring-beans\6.1.16\spring-beans-6.1.16.jar;C:\Users\ccc\.m2\repository\org\springframework\spring-expression\6.1.16\spring-expression-6.1.16.jar;C:\Users\ccc\.m2\repository\io\micrometer\micrometer-observation\1.13.9\micrometer-observation-1.13.9.jar;C:\Users\ccc\.m2\repository\io\micrometer\micrometer-commons\1.13.9\micrometer-commons-1.13.9.jar;C:\Users\ccc\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\3.3.7\spring-boot-autoconfigure-3.3.7.jar;C:\Users\ccc\.m2\repository\org\springframework\boot\spring-boot-starter-logging\3.3.7\spring-boot-starter-logging-3.3.7.jar;C:\Users\ccc\.m2\repository\ch\qos\logback\logback-classic\1.5.12\logback-classic-1.5.12.jar;C:\Users\ccc\.m2\repository\ch\qos\logback\logback-core\1.5.12\logback-core-1.5.12.jar;C:\Users\ccc\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.23.1\log4j-to-slf4j-2.23.1.jar;C:\Users\ccc\.m2\repository\org\apache\logging\log4j\log4j-api\2.23.1\log4j-api-2.23.1.jar;C:\Users\ccc\.m2\repository\org\slf4j\jul-to-slf4j\2.0.16\jul-to-slf4j-2.0.16.jar;C:\Users\ccc\.m2\repository\jakarta\annotation\jakarta.annotation-api\2.1.1\jakarta.annotation-api-2.1.1.jar;C:\Users\ccc\.m2\repository\org\springframework\spring-core\6.1.16\spring-core-6.1.16.jar;C:\Users\ccc\.m2\repository\org\springframework\spring-jcl\6.1.16\spring-jcl-6.1.16.jar;C:\Users\ccc\.m2\repository\org\yaml\snakeyaml\2.2\snakeyaml-2.2.jar;C:\Users\ccc\.m2\repository\org\slf4j\slf4j-api\2.0.16\slf4j-api-2.0.16.jar com.example.demo.DemoApplication
Hi! It's my first Java Program.
1月 01, 2025 3:47:33 下午 com.example.demo.DemoApplication main
信息: My first Log statement.
1月 01, 2025 3:47:34 下午 com.example.demo.DemoApplication main
严重: Severe message
1月 01, 2025 3:47:34 下午 com.example.demo.DemoApplication main
警告: Warning Message
1月 01, 2025 3:47:34 下午 com.example.demo.DemoApplication main
严重: Another Severs message
1月 01, 2025 3:47:34 下午 com.example.demo.DemoApplication main
严重: Array Index is out of bounds.
java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
at com.example.demo.DemoApplication.main(DemoApplication.java:45)
result: 2
Process finished with exit code 0
日志内容:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
<date>2025-01-01T07:47:34.012257900Z</date>
<millis>1735717654012</millis>
<nanos>257900</nanos>
<sequence>1</sequence>
<logger>com.example.demo.DemoApplication</logger>
<level>SEVERE</level>
<class>com.example.demo.DemoApplication</class>
<method>main</method>
<thread>1</thread>
<message>Severe message</message>
</record>
<record>
<date>2025-01-01T07:47:34.013889500Z</date>
<millis>1735717654013</millis>
<nanos>889500</nanos>
<sequence>3</sequence>
<logger>com.example.demo.DemoApplication</logger>
<level>SEVERE</level>
<class>com.example.demo.DemoApplication</class>
<method>main</method>
<thread>1</thread>
<message>Another Severs message</message>
</record>
<record>
<date>2025-01-01T07:47:34.018939500Z</date>
<millis>1735717654018</millis>
<nanos>939500</nanos>
<sequence>4</sequence>
<logger>com.example.demo.DemoApplication</logger>
<level>SEVERE</level>
<class>com.example.demo.DemoApplication</class>
<method>main</method>
<thread>1</thread>
<message>Array Index is out of bounds.</message>
<exception>
<message>java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3</message>
<frame>
<class>com.example.demo.DemoApplication</class>
<method>main</method>
<line>45</line>
</frame>
</exception>
</record>
</log>