当前位置: 首页 > news >正文

Spring-boot读书笔记一Introduction of logging framework of Log4j2

Log4j2 is a powerful and flexible logging framework for Java applications. Here's an overview of its key components and features:

Core Components

  • Logger - The main interface for logging messages. Loggers are named entities that capture log events.
  • Appender - Defines where log messages are sent (console, files, databases, etc.).
  • Layout - Controls the format of log messages (JSON, XML, pattern-based, etc.).
  • Configuration - Defines loggers, appenders, and their relationships through XML, JSON, YAML, or properties files.

Key Features

  • Asynchronous Logging - High-performance logging with minimal impact on application threads
  • Automatic Reloading - Configuration changes are detected and applied without restarting
  • Plugin Architecture - Extensible system for custom appenders, layouts, and filters
  • Lambda Support - Lazy evaluation of log messages for better performance
  • Garbage-Free Logging - Reduces memory allocation and GC pressure

Log Levels (Hierarchy)
FATAL > ERROR > WARN > INFO > DEBUG > TRACE

Basic Configuration Example
For your Spring Boot application, you can configure Log4j2 using log4j2.xml:


<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN"><Appenders><Console name="Console" target="SYSTEM_OUT"><PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/></Console><File name="FileAppender" fileName="logs/application.log"><PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n"/></File></Appenders><Loggers><Logger name="com.example" level="DEBUG"/><Root level="INFO"><AppenderRef ref="Console"/><AppenderRef ref="FileAppender"/></Root></Loggers>
</Configuration>

Common Appenders

  • ConsoleAppender - Outputs to console/terminal
  • FileAppender - Writes to files
  • RollingFileAppender - Rotates log files based on size/time
  • AsyncAppender - Wraps other appenders for asynchronous logging
  • JDBCAppender - Logs to databases
  • SMTPAppender - Sends logs via email

Performance Benefits

  • Log4j2 offers significant performance improvements over Log4j 1.x:
  • 10x faster than Log4j 1.x and Logback
  • Garbage-free logging in steady state
  • Asynchronous loggers for high-throughput applications

For Spring Boot applications, Log4j2 integrates seamlessly and can replace the default Logback implementation by excluding it from dependencies and adding Log4j2 starter.

http://www.jsqmd.com/news/168069/

相关文章:

  • Miniconda-Python3.10镜像如何支持多租户GPU算力售卖
  • 使用 K-Means 聚类进行图像分割
  • JLink驱动安装日志分析方法:快速定位错误原因
  • IAR安装教程(STM32):手把手带你完成环境搭建
  • 【2025最新】基于SpringBoot+Vue的乡村养老服务管理系统管理系统源码+MyBatis+MySQL
  • Miniconda-Python3.10镜像如何实现按需付费的Token模式
  • cc2530串口通信项目应用:IAR平台操作指南
  • STM32 USART波特率超详细版配置流程说明
  • CCS20冗余架构设计原理:图解说明
  • 使用Miniconda为大模型推理服务配置负载均衡
  • Jupyter内核配置错误修复:确保Miniconda-Python3.10正确注册Python环境
  • Miniconda-Python3.10镜像在儿童教育大模型中的适配优化
  • Java Web 箱包存储系统系统源码-SpringBoot2+Vue3+MyBatis-Plus+MySQL8.0【含文档】
  • Miniconda-Python3.10环境下安装DGL进行图神经网络研究
  • 从Anaconda迁移到Miniconda-Python3.10:节省70%磁盘空间的方法
  • LTspice仿真错误排查技巧:常见报错通俗解释
  • 使用Miniconda实现PyTorch模型的金丝雀发布
  • 通过jflash实现安全启动配置:工业应用
  • PyTorch安装教程GPU版:Miniconda-Python3.10环境下一键部署深度学习模型
  • Miniconda-Python3.10镜像在诗歌生成大模型中的创意应用
  • Java SpringBoot+Vue3+MyBatis 项目申报管理系统系统源码|前后端分离+MySQL数据库
  • STM32波形发生器相位累加器实现:核心要点
  • Jupyter Notebook直连开发环境:Miniconda-Python3.10镜像使用图文教程
  • 使用Miniconda-Python3.10镜像批量部署百台服务器AI环境
  • Miniconda-Python3.10结合Gunicorn部署高可用模型服务
  • Miniconda-Python3.10镜像在虚拟偶像对话系统中的应用
  • Miniconda-Python3.10镜像如何支持合规性审计的Token记录
  • Miniconda-Python3.10镜像在法律文书生成大模型中的应用
  • Java SpringBoot+Vue3+MyBatis 销售项目流程化管理系统系统源码|前后端分离+MySQL数据库
  • Zynq AXI数据总线通道的valid和ready信号