Introduction:
- It is a logging framework which provides faster implementation than log4j.
- It supports the creation of custom classes for formatting log messages.
- It supports 3 components loggers, appender, layout for holding, transferring, formatting the log messages.
- It uses SLF$J as its native interface.
- The levels of logging are TRACE, DEBUG, INFO, WARN, ERROR.
- It writes all log messages in the DEBUG level, does not require any configuration.
Here is my java class
package com.logs; import org.apache.log4j.xml.DOMConfigurator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * @author kalpana podamekala */ public class LogbackExample { /** * */ final static Logger logger = LoggerFactory.getLogger(LogbackExample.class); public static void main(String[] args) { //DOMConfigurator.configure("logback.xml"); logger.info("Information message"); logger.warn("Warning message"); logger.error("Error message"); logger.debug("Debug message"); } }
Here is my pom.xml
Here is my log-back.xml file.
just copy all files into your project.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany</groupId> <artifactId>Logback</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> <type>jar</type> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> <type>jar</type> </dependency> </dependencies> </project>
<!--<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} | %-5p | [%thread] %logger{5}:%L - %msg%n</pattern> </encoder> </appender> <appender name="FILE" class="ch.qos.logback.core.FileAppender"> <file>logFile.log</file> <append>true</append> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} | %-5p | [%thread] %logger{5}:%L - %msg%n</pattern> </encoder> </appender> <logger name="com.memorynotfound" level="TRACE"/> <root level="DEBUG"> <appender-ref ref="STDOUT" /> <appender-ref ref="FILE" /> </root> </configuration>-->