Log4J Getting started

Table of Contents

This document aims to guide you through the most important aspects of logging with Log4j. It is not a comprehensive guide, but it should give you a good starting point.

本文旨在引导你了解 Log4j 中比较重要的内容,而非完全手册,但应该对你使用 Log4j 来讲是一个好的开始。

什么是日志?

我为什么应该使用 Log4j?

Log4j 中包含什么?

Log4j is essentially composed of a logging API called Log4j API, and its reference implementation called Log4j Core. Log4j also bundles several logging bridges to enable Log4j Core consume from foreign logging APIs. Let’s briefly explain these concepts:

Log4j 大体上包括称作 Log4j API 的日志 API 和称作 Log4j Core 的实现。同时也还包含多种桥接到外部日志 API 的桥接库,使外部日志 API 使用 Log4j Core 来记录日志。

日志接口

A logging API is an interface your code or your dependencies directly logs against. It is required at compile-time. It is implementation agnostic to ensure that your application can write logs, but is not tied to a specific logging implementation. Log4j API, SLF4J, JUL (Java Logging), JCL (Apache Commons Logging), JPL (Java Platform Logging) and JBoss Logging are major logging APIs.

日志接口你的代码或代码依赖记录日志时直接调用的接口,它是编译时所必须的依赖。它与具体实现无关,不绑定到特定的日志实现。Log4j API,SLF4J,JUL(Java Logging),JCL(Apache Commons Logging),JPL(Java Platform Logging)和 JBoss Logging

日志实现

A logging implementation is only required at runtime and can be changed without the need to recompile your software. Log4j Core, JUL (Java Logging), Logback are the most well-known logging implementations.

日志桥接

Logging implementations accept input from a single logging API of their preference; Log4j Core from Log4j API, Logback from SLF4J, etc. A logging bridge is a simple logging implementation of a logging API that forwards all messages to a foreign logging API. Logging bridges allow a logging implementation to accept input from other logging APIs that are not their primary logging API. For instance, log4j-slf4j2-impl bridges SLF4J calls to Log4 API and effectively enables Log4j Core to accept input from SLF4J.

安装前需要准备什么?

如何使用 Log4j API 记录日志?

如何在应用中使用 Log4j Core?

如何在应用中配置 Log4j Core?

如何在库中使用 Log4j Core?

如何为测试配置 Log4j Core?

下一步?

Date: Fri Sep 20 16:22:37 2024

Author: xueshumeng

Email: xueshumeng@foxmail.com

Created: 2025-07-05 Sat 15:09