当前位置: 首页 > 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.rkmt.cn/news/183780.html

相关文章:

  • Miniconda-Python3.10镜像如何支持多租户GPU算力售卖
  • 使用Miniconda为大模型推理服务配置负载均衡
  • Jupyter内核配置错误修复:确保Miniconda-Python3.10正确注册Python环境
  • PyTorch安装教程GPU版:Miniconda-Python3.10环境下一键部署深度学习模型
  • Miniconda-Python3.10结合Gunicorn部署高可用模型服务
  • STM32波形发生器相位累加器实现:核心要点
  • 使用Miniconda-Python3.10镜像批量部署百台服务器AI环境
  • Miniconda-Python3.10镜像如何支持合规性审计的Token记录
  • Java SpringBoot+Vue3+MyBatis 销售项目流程化管理系统系统源码|前后端分离+MySQL数据库
  • 前后端分离校园生活服务平台系统|SpringBoot+Vue+MyBatis+MySQL完整源码+部署教程
  • Token消耗过大?通过Miniconda-Python3.10优化大模型推理内存占用
  • Miniconda-Python3.10环境下使用conda clean清理缓存
  • Nuo-Math-Compiler
  • 核心要点:工业控制PCB布线电流承载能力计算
  • Miniconda-Python3.10镜像如何优化GPU资源调度策略
  • 使用 JMeter 从 Fiddler 捕获请求并生成测试脚本(上)
  • 全面讲解USB转串口硬件接线与软件配置
  • Miniconda-Python3.10镜像在剧本创作大模型中的尝试
  • 入门必看:AUTOSAR架构图各层功能通俗解读
  • Docker run命令如何启动AI开发容器?Miniconda-Python3.10镜像模板分享
  • 基于Java+SpringBoot+SpringBoot博物馆文创系统(源码+LW+调试文档+讲解等)/博物馆文创产品/博物馆数字化系统/博物馆创意设计系统/博物馆文化衍生品系统/博物馆文创平台
  • Miniconda-Python3.10环境下使用html报告监控训练进度
  • 基于Java+SpringBoot+SpringBoot咖啡店点餐系统(源码+LW+调试文档+讲解等)/咖啡店点单系统/咖啡厅点餐系统/咖啡厅点单系统/咖啡店自助点餐/咖啡店扫码点餐/咖啡店智能点餐
  • STM32开发第一步:IAR编译器安装的系统化教程
  • allegro导出gerber文件常见问题:新手避坑指南
  • STM32上I2C HID中断处理机制解析
  • Miniconda-Python3.10镜像如何支撑高并发Token计费接口
  • Miniconda-Python3.10结合Nginx反向代理保护模型接口
  • cp2102usb to uart bridge波特率配置驱动层解析
  • vivado2018.3下SPI接口实现:深度剖析与时序分析