Java Logging Techniques Summary(Logback Introduction)

本文介绍Logback的日志记录配置流程及其实现方式,并通过一个简单示例展示如何在Maven项目中集成Logback。文章详细说明了pom.xml文件中的依赖引入、logback.xml配置文件的具体设置以及测试案例中的日志输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 Summary

    1) Logback is not an independent implementation of Logging.

        It depends on SLF4J.

 

1. Logback configuration work flow


 

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值