SLF4J support

This commit is contained in:
Bruce Eckel 2016-08-16 21:43:15 -06:00
parent 0345f21650
commit e7bf71ea3a
4 changed files with 37 additions and 64 deletions

View File

@ -299,8 +299,8 @@ configure(subprojects - project(':onjava')) {
compile project(':onjava')
compile group: 'com.google.guava', name: 'guava', version: '19.0'
compile "org.openjdk.jmh:jmh-core:${jmh.jmhVersion}"
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.6.2'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.6.2'
//compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.6.2'
//compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.6.2'
}
}

View File

@ -1,62 +0,0 @@
// verifying/LoggingLevels.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// {ErrorOutputExpected}
import org.apache.logging.log4j.*;
import org.apache.logging.log4j.spi.*;
import org.apache.logging.log4j.core.config.Configurator;
public class LoggingLevels {
private static Logger
lgr = LogManager.getLogger("com"),
lgr2 = LogManager.getLogger("com.mindviewinc"),
util= LogManager.getLogger("onjava"),
test= LogManager.getLogger("com.mindviewinc.test"),
rand = LogManager.getLogger("random");
private static void logMessages() {
lgr.info("com : info");
lgr2.info("com.mindviewinc : info");
util.info("util : info");
test.fatal("test : fatal");
rand.info("random : info");
}
public static void main(String[] args) {
Configurator.setLevel("com", Level.FATAL);
System.out.println("com level: FATAL");
logMessages();
Configurator.setLevel("onjava", Level.TRACE);
Configurator.setLevel("com.mindviewinc.test", Level.TRACE);
Configurator.setLevel("random", Level.TRACE);
System.out.println(
"individual loggers set to TRACE");
logMessages();
Configurator.setLevel("com", Level.FATAL);
System.out.println("com level: FATAL");
logMessages();
}
}
/* Output:
com level: SEVERE
individual loggers set to FINEST
com level: SEVERE
___[ Error Output ]___
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
INFO: util : info
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
SEVERE: test : severe
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
INFO: random : info
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
INFO: util : info
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
SEVERE: test : severe
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
INFO: random : info
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
INFO: util : info
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
SEVERE: test : severe
Jul 27, 2016 10:50:42 AM LoggingLevels logMessages
INFO: random : info
*/

View File

@ -0,0 +1,23 @@
// verifying/SLF4JLevels.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
import org.slf4j.*;
public class SLF4JLevels {
private static final Logger logger =
LoggerFactory.getLogger(SLF4JLevels.class);
public static void main(String[] args) {
logger.trace("level: trace");
logger.debug("level: debug");
logger.info("level: info");
logger.warn("level: warn");
logger.error("level: error");
}
}
/* Output:
20:09:34.499 [main] DEBUG SLF4JLevels - level: debug
20:09:34.502 [main] INFO SLF4JLevels - level: info
20:09:34.502 [main] WARN SLF4JLevels - level: warn
20:09:34.502 [main] ERROR SLF4JLevels - level: error
*/

12
verifying/logback.xml Normal file
View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd'T'HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n
</pattern>
</encoder>
</appender>
<root level="TRACE">
<appender-ref ref="STDOUT" />
</root>
</configuration>