自定义Formatter和Handler。
一,代码
MyLogFormatter.java
package com.example.demo;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
public class MyLogFormatter extends Formatter {
private static final DateFormat dateFormat = new SimpleDateFormat("yyyy/dd/MM hh:mm:ss");
public String format(LogRecord logRecord) {
StringBuilder logMessage = new StringBuilder();
logMessage.append(dateFormat.format(new Date(logRecord.getMillis()))).append(" - ");
logMessage.append("[").append(logRecord.getLevel()).append("] - ");
logMessage.append("[").append(logRecord.getSourceClassName()).append(".");
logMessage.append(logRecord.getSourceMethodName()).append("] - ");
logMessage.append(formatMessage(logRecord));
logMessage.append("\n");
return logMessage.toString();
}
}
MyCustomLogHandler.java
package com.example.demo;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
public class MyCustomLogHandler extends Handler {
private static final DateFormat dateFormat = new SimpleDateFormat("yyyy/dd/MM hh:mm:ss");
@Override
public void publish(LogRecord logRecord) {
StringBuilder logMessage = new StringBuilder();
logMessage.append(dateFormat.format(new Date(logRecord.getMillis())))
.append(" - ")
.append(logRecord.getSourceClassName())
.append(" ")
.append(logRecord.getSourceMethodName())
.append(" - ")
.append(logRecord.getLevel())
.append(" - ")
.append(logRecord.getMessage());
System.out.println(logMessage.toString());
}
@Override
public void flush() {
}
@Override
public void close() throws SecurityException {
}
}
DemoApplication.java
package com.example.demo;
import org.slf4j.LoggerFactory;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Currency;
import java.util.logging.*;
//public class DemoApplication implements Filter extends Logger {
public class DemoApplication {
//private static final Logger logger = Logger.getLogger(DemoApplication.class.getName());
public static void main(String[] args) throws SecurityException, IOException {
// System.setProperty("java.util.logging.SimpleFormatter.format",
// "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL %4$-7s [%3$s] (%2$s) %5$s %6$s%n");
// ConsoleHandler consoleHandler = new ConsoleHandler();
// consoleHandler.setLevel(Level.FINER);
// consoleHandler.setFormatter(new SimpleFormatter());
Logger logger = Logger.getLogger(DemoApplication.class.getName());
logger.setUseParentHandlers(false);
logger.setLevel(Level.FINER);
logger.addHandler(new MyCustomLogHandler());
logger.log(Level.WARNING, "Warning Message");
logger.finer("Finer Message");
logger.warning("Warning Message");
logger.warning("Debug Message");
logger.info("First Message logged.");
logger.info("Info Message logged.");
logger.severe("Severe Message logged.");
}
}
二,输出
D:\Java\jdk17\bin\java.exe "-javaagent:D:\JetBrains\IntelliJ IDEA Community Edition 2024.3.1.1\lib\idea_rt.jar=9965: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
2025/02/01 09:41:55 - com.example.demo.DemoApplication main - WARNING - Warning Message
2025/02/01 09:41:55 - com.example.demo.DemoApplication main - FINER - Finer Message
2025/02/01 09:41:55 - com.example.demo.DemoApplication main - WARNING - Warning Message
2025/02/01 09:41:55 - com.example.demo.DemoApplication main - WARNING - Debug Message
2025/02/01 09:41:55 - com.example.demo.DemoApplication main - INFO - First Message logged.
2025/02/01 09:41:55 - com.example.demo.DemoApplication main - INFO - Info Message logged.
2025/02/01 09:41:55 - com.example.demo.DemoApplication main - SEVERE - Severe Message logged.
Process finished with exit code 0