diff --git a/logging-modules/README.md b/logging-modules/README.md index 6de71adb43..a589b1245c 100644 --- a/logging-modules/README.md +++ b/logging-modules/README.md @@ -5,3 +5,4 @@ - [Creating a Custom Logback Appender](http://www.baeldung.com/custom-logback-appender) - [Get Log Output in JSON Format](http://www.baeldung.com/java-log-json-output) +- [A Guide To Logback](http://www.baeldung.com/a-guide-to-logback) diff --git a/logging-modules/logback/src/test/java/com/baeldung/logback/LogbackTests.java b/logging-modules/logback/src/test/java/com/baeldung/logback/LogbackTests.java new file mode 100644 index 0000000000..85201965dc --- /dev/null +++ b/logging-modules/logback/src/test/java/com/baeldung/logback/LogbackTests.java @@ -0,0 +1,89 @@ +package com.baeldung.logback; + +import ch.qos.logback.classic.Level; +import org.junit.Test; + +import ch.qos.logback.classic.Logger; +import org.slf4j.LoggerFactory; + +public class LogbackTests { + + @Test + public void givenLogHierarchy_MessagesFiltered() { + + ch.qos.logback.classic.Logger parentLogger = + (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("com.baeldung.logback"); + + parentLogger.setLevel(Level.INFO); + + Logger childlogger = (ch.qos.logback.classic.Logger)LoggerFactory.getLogger("com.baeldung.logback.tests"); + + parentLogger.warn("This message is logged because WARN > INFO."); + + // This request is disabled, because DEBUG < INFO. + parentLogger.debug("This message is not logged because DEBUG < INFO."); + + childlogger.info("INFO == INFO"); + + childlogger.debug("DEBUG < INFO"); + + } + + @Test + public void givenRootLevel_MessagesFiltered() { + + ch.qos.logback.classic.Logger logger = + (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("com.baeldung.logback"); + + logger.debug("Hi there!"); + + Logger rootLogger = (ch.qos.logback.classic.Logger)LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME); + + logger.debug("This message is logged because DEBUG == DEBUG."); + + rootLogger.setLevel(Level.ERROR); + logger.warn("This message is not logged because WARN < ERROR."); + + logger.error("This is logged."); + + } + + @Test + public void givenParameters_ValuesLogged() { + + Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(LogbackTests.class); + + String message = "This is a String"; + Integer zero = 0; + + try { + logger.debug("Logging message: {}", message); + logger.debug("Going to divide {} by {}", 42, zero); + int result = 42 / zero; + } catch (Exception e) { + logger.error("Error dividing {} by {} ", 42, zero, e); + } + } + + @Test + public void givenConfig_MessageFiltered() { + + Logger foobar = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("com.baeldung.foobar"); + Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("com.baeldung.logback"); + Logger testslogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("com.baeldung.logback.tests"); + + foobar.debug("This is logged from foobar"); + logger.debug("This is not logged from logger"); + logger.info("This is logged from logger"); + testslogger.info("This is not logged from tests"); + testslogger.warn("This is logged from tests"); + + + } + + + + + + +} diff --git a/logging-modules/logback/src/test/resources/logback-guide.xml b/logging-modules/logback/src/test/resources/logback-guide.xml new file mode 100644 index 0000000000..8331f1cb93 --- /dev/null +++ b/logging-modules/logback/src/test/resources/logback-guide.xml @@ -0,0 +1,32 @@ + + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + tests.log + true + + true + + + %-4relative [%thread] %-5level %logger{35} - %msg%n + + + + + + + + + + + + + \ No newline at end of file