Java Logging Techniques Summary(Logback Introduction)

 Summary

    1) Logback is not an independent implementation of Logging.

        It depends on SLF4J.

 

1. Logback configuration work flow

Java Logging Techniques Summary(Logback Introduction)
 

2. A simple example of using Logback

   1) pom.xml

<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>edu.xmu.logging</groupId>
	<artifactId>Logging-JDKLogging</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>Logging-JDKLogging</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.8.2</version>
			<scope>test</scope>
		</dependency>
		<!-- Dependency for Logback -->
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>1.0.13</version>
		</dependency>
	</dependencies>
</project>

   This will import logback-classic.jar, logback-core.jar and slf4j-api.jar automatically.

    2) logback.xml in classpath <Pay attention to filter tag as it's not the same with JUL/Log4j>

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
			<level>WARN</level>
		</filter>
		<encoder>
			<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
			</pattern>
		</encoder>
	</appender>
	<appender name="FILE" class="ch.qos.logback.core.FileAppender">
		<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
			<level>ERROR</level>
		</filter>
		<file>myApp.log</file>
		<encoder>
			<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n
			</pattern>
		</encoder>
		<append>false</append>
	</appender>
	<root level="DEBUG">
		<appender-ref ref="FILE" />
		<appender-ref ref="STDOUT" />
	</root>
</configuration>

    3) Test case

package edu.xmu.logging.Logging_JDKLogging;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AppTest
{
	private static Logger logger = LoggerFactory.getLogger(AppTest.class);

	@Test
	public void loggerTest()
	{
		logger.trace("[TRACE] Trace Logging");
		logger.debug("[DEBUG] Debug Logging");
		logger.info("[INFO]Info Logging");
		logger.warn("[WARN]Warn Logging");
		logger.error("[ERROR]Error Logging");
	}
}

 

3. We can config Logger & Appender & Encoder in config file or in program.

   But it's recommonded we config in config file as config in program will cause the code to dependent on the relization(Logback/Log4j/JUL).

 

Reference Links:

    1) http://logback.qos.ch/manual/ Logback user manual.

你可能感兴趣的:(java,logback,logging)