org.junit:junit:4.12
public class JULBaseTests {
protected Logger logger;
@After
public void print() {
//日志输出简写形式,有不同的级别,可带参数,其它类似
logger.log(Level.SEVERE, "this is a severe", new Exception("my excep")); //带异常输出
logger.log(Level.WARNING, "this is a warn, {0} {1}", new Object[]{"p1", "p2"});
logger.log(Level.INFO, "this is a info, {0}", "p1");
logger.log(Level.CONFIG, "this is a config");
logger.log(Level.FINE, "this is a fine");
logger.log(Level.FINER, "this is a finer");
logger.log(Level.FINEST, "this is a finest");
//日志输出简写形式,有不同的级别
logger.severe("severe log");
logger.warning("warning log");
logger.info("info log");
logger.config("config log");
logger.fine("fine log");
logger.finer("finer log");
logger.finest("finest log");
}
}
public class JULDefaultConfigTests extends JULBaseTests {
@Test
public void testDefaultLogger() {
// 日志记录器
logger = Logger.getLogger(JULDefaultConfigTests.class.getName());
}
}
默认情况下配置文件路径为$JAVAHOME\jre\lib\logging.properties
# 全局属性
## 添加处理器:这些处理器会在VM启动时安装,所以这些类必须在系统类路径中
handlers= java.util.logging.ConsoleHandler
#handlers= java.util.logging.ConsoleHandler,java.util.logging.FileHandler
## 默认全局级别
.level= INFO
# 处理器特定属性
## 默认文件处理器
java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
## 默认控制台处理器
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
# 自定义格式化器
## Simple格式化器
#java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n
# 日志记录器特定属性
com.xyz.foo.level = SEVERE
properties 配置文件
# 全局属性
## 添加处理器
handlers= java.util.logging.ConsoleHandler, java.util.logging.FileHandler
## 默认全局级别
.level= INFO
# 处理器特定属性
## 默认文件处理器
java.util.logging.FileHandler.pattern = jul-customConfig.log
java.util.logging.FileHandler.encoding = UTF-8
java.util.logging.FileHandler.append = true
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
## 默认控制台处理器
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
# 日志记录器特定属性
com.jasper.logs.jul.hello.handlers = java.util.logging.FileHandler
com.jasper.logs.jul.hello.level = SEVERE
com.jasper.logs.jul.hello.useParentHandlers = false
编码配置全局单例的LogManager,指定使用自定义的配置文件。
public class JULCustomConfigTests extends JULBaseTests {
@Before
public void init() {
// logManager 是全局单例的
LogManager logManager = LogManager.getLogManager();
try {
logManager.readConfiguration(this.getClass().getResourceAsStream("/jul-custom-config.properties"));
} catch (SecurityException | IOException e) {
e.printStackTrace();
}
}
@Test
public void testDefaultLogger() {
// 日志记录器
logger = Logger.getLogger(JULCustomConfigTests.class.getName());
}
}
或者使用系统属性指定-Djava.util.logging.config.file=myfile
原项目使用JUL日志框架(JUL API + JUL配置文件),现在改用log4j2配置(JUL API + Log4j2配置文件)
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-julartifactId>
<version>2.12.1version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-apiartifactId>
<version>2.12.1version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-coreartifactId>
<version>2.12.1version>
dependency>
<Configuration status="WARN">
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout
pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
Console>
<Console name="ERROUT" target="SYSTEM_ERR">
<PatternLayout
pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
Console>
Appenders>
<Loggers>
<Logger name="com.jasper.logs.jul.hello" level="trace" additivity="false">
<AppenderRef ref="STDOUT" />
Logger>
<Root level="ERROR">
<AppenderRef ref="ERROUT" />
Root>
Loggers>
Configuration>
VM启动时,配置-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager
,或者获取logger
前,执行System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager");
。
需要确保IDE支持Lombok,否则编译报错。
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.0version>
dependency>
直接在类上使用注解@Log
,即可引入log
原项目使用JUL
日志框架的API的情况下,转为使用log4j2自定义配置+Lombok日志注解。
/**
* 使用JUL API + JUL默认配置
*/
public class JULDefaultConfigTests {
private static final Logger logger = Logger.getLogger(JULDefaultConfigTests.class.getName());
@Test
public void log() {
//日志输出简写形式,有不同的级别,可带参数,其它类似
logger.log(Level.SEVERE, "this is a severe");
logger.log(Level.WARNING, "this is a warn, {0} {1}", new Object[]{"p1", "p2"});
logger.log(Level.INFO, "this is a info, {0}", "p1");
logger.log(Level.CONFIG, "this is a config");
logger.log(Level.FINE, "this is a fine");
logger.log(Level.FINER, "this is a finer", new Exception("my excep")); //带异常输出
logger.log(Level.FINEST, "this is a finest");
//日志输出简写形式,有不同的级别
logger.severe("severe log");
logger.warning("warning log");
logger.info("info log");
logger.config("config log");
logger.fine("fine log");
logger.finer("finer log");
logger.finest("finest log");
}
}
log4j-jul
适配包,设置系统属性-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager
@Log
注解/**
* 使用JUL API + log4j2自定义配置 (系统属性)+ lombok 日志注解
*/
@Log
public class JULDefaultConfigTests {
@Test
public void log() {
//日志输出简写形式,有不同的级别,可带参数,其它类似
log.log(Level.SEVERE, "this is a severe");
log.log(Level.WARNING, "this is a warn, {0} {1}", new Object[]{"p1", "p2"});
log.log(Level.INFO, "this is a info, {0}", "p1");
log.log(Level.CONFIG, "this is a config");
log.log(Level.FINE, "this is a fine");
log.log(Level.FINER, "this is a finer", new Exception("my excep")); //带异常输出
log.log(Level.FINEST, "this is a finest");
//日志输出简写形式,有不同的级别
log.severe("severe log");
log.warning("warning log");
log.info("info log");
log.config("config log");
log.fine("fine log");
log.finer("finer log");
log.finest("finest log");
}
}