`
本来不想注册
  • 浏览: 198676 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

log4j 介绍(4)-- Layout篇

阅读更多
Layout of the log file
More often than not, users wish to customize not only the output destination but also the output format. This is accomplished by associating a layout with an appender. The layout is responsible for formatting the logging request according to the user's wishes, whereas an appender takes care of sending the formatted output to its destination.

你经常会在log4j.properties 里看到这样两行:
log4j.appender.EVENTS.layout=org.apache.log4j.PatternLayout
log4j.appender.EVENTS.layout.ConversionPattern=%d{dd-MM-yyyy}, %d{HH:mm:ss}, %-5.5p, %m %n

The best up-to-date documentation about available layouts can be found in the API documentation:
http://logging.apache.org/log4j/docs/api/org/apache/log4j/Layout.html
There you can see that we have DateLayout, HTMLLayout, PatternLayout, SimpleLayout, XMLLayout as options.


The PatternLayout, part of the standard log4j distribution, lets the user specify the output format according to conversion patterns similar to the C language printf function.

For example, the PatternLayout with the conversion pattern "%r [%t] %-5p %c - %m%n" will output something akin to:



176 [main] INFO  org.foo.Bar - Located nearest gas station.

The first field is the number of milliseconds elapsed since the start of the program. The second field is the thread making the log request. The third field is the level of the log statement. The fourth field is the name of the logger associated with the log request. The text after the '-' is the message of the statement.

Just as importantly, log4j will render the content of the log message according to user specified criteria. For example, if you frequently need to log Oranges, an object type used in your current project, then you can register an OrangeRenderer that will be invoked whenever an orange needs to be logged.

Object rendering follows the class hierarchy. For example, assuming oranges are fruits, if you register an FruitRenderer, all fruits including oranges will be rendered by the FruitRenderer, unless of course you registered an orange specific OrangeRenderer.

Object renderers have to implement the ObjectRenderer interface.


Layout最好理解了,看看就好了。
分享到:
评论

相关推荐

    log4j 介绍(6)-- tutorial 参考

    标题“log4j 介绍(6)-- tutorial 参考”指出,这是一篇关于log4j日志框架的教程性文章,可能是系列教程的第六部分,重点是提供学习和参考的指导。描述中提到的"log4j-tutorial-en.pdf"是一个英文版的PDF文档,可能...

    hibernate3.3.1接口实现包 slf4j-log4j12-1.5.2

    本篇将详细解析Hibernate3.3.1与SLF4J-Log4j12-1.5.2的集成及其在实际应用中的作用。 **Hibernate3.3.1详解** Hibernate3.3.1是Hibernate系列的一个稳定版本,它提供了丰富的功能,如对象关系映射、事务管理、查询...

    log4j2 -2.11.1.zip

    在本篇中,我们将深入探讨Log4j2的核心特性、其API与Core组件的功能,以及如何在实际项目中应用这些文件。 Log4j2是一个继Log4j之后的日志系统,旨在提供更高效的性能、丰富的日志配置选项和动态日志管理。2.11.1...

    log4j-1.2.11jar和log4j.properties配置文件

    本篇将详细介绍Log4j 1.2.11版本以及其核心组件——`log4j.properties`配置文件。 **一、Log4j简介** Log4j是基于组件的日志框架,它提供了一种灵活的方式来记录应用程序中的事件。这些事件可以是错误、警告、调试...

    log4j案例代码

    本篇将基于“log4j案例代码”进行深入讲解。 **1. Log4j简介** Log4j是一个用于记录应用日志的灵活框架,它提供了多种级别的日志记录,如DEBUG、INFO、WARN、ERROR和FATAL,以及自定义级别。Log4j的灵活性体现在它...

    Log4j将System.out搞到log4j中输出四

    在《Log4j将System.out搞到log4j中输出四》这篇博文中,作者可能详细讨论了这些步骤,并可能分享了一些实战经验。通过学习这篇博文,读者可以更深入地了解如何在实际项目中实现这一转换,提升日志管理的效率。 总结...

    log4j(二):动态配置日志输出路径

    在Java开发中,日志记录是一项至关重要的任务,它帮助开发者追踪程序运行状态,调试问题,以及进行性能分析。...通过阅读《log4j(二):动态配置日志输出路径》这篇博文,你可以获得更详细的操作步骤和实践指导。

    log4j 使用介绍

    这篇介绍旨在帮助初学者理解 log4j 的核心概念和使用方法。 **1. 简介** 日志记录在软件开发中扮演着重要角色,因为它有助于调试、监控和问题排查。log4j 提供了一个分级的日志系统,允许开发者根据事件的重要性...

    log4j多个简单实例

    本篇文章将深入探讨Log4j的多个简单实例,帮助你理解和掌握其基本用法。 首先,Log4j由三个主要组件构成:配置器(Configuration)、日志器(Logger)和布局(Layout)。配置器定义了日志信息的输出格式和位置;...

    log4j中文版的API

    本篇将基于提供的"Log4j中文版API",深入讲解其核心概念、配置及使用方法。 一、Log4j的基本组件 1. **Logger**: 日志器是Log4j的核心组件,负责生成日志事件。每个类通常都有一个对应的Logger实例,用于记录与该...

    J2EE复习积累(四)-Log4J

    10. **Log4J 2.x 更新** - 如果涉及Log4J的新版本,可能会介绍新的特性,如性能提升、线程安全的配置更新、更丰富的Appender和Layout选择等。 通过以上知识点,开发者可以更好地理解和运用Log4J,提高J2EE应用程序...

    JAVA-log4j参数

    本篇文章将深入探讨Log4j的参数配置方法,以及ConversionPattern参数的格式含义。 首先,Log4j的核心在于其配置文件,一般命名为`log4j.properties`或`log4j.xml`。配置文件定义了日志记录的级别、输出目的地和格式...

    log4j简述(架构、用法等)

    本篇文章旨在深入探讨 Log4j 的架构设计、基本用法以及性能特点。 #### 二、Log4j 的核心组件 Log4j 的设计围绕三大核心组件展开:**Logger**、**Appender** 和 **Layout**。这三个组件相互协作,确保日志记录既...

    log4j配置文件以及配套jar包.rar

    本篇将围绕“log4j配置文件以及配套jar包”进行详细介绍。 首先,我们要了解Log4j的基本结构。Log4j主要由三个部分组成:Logger(日志器)、Appender(输出端)和Layout(布局)。Logger负责记录日志事件,Appender...

    Log4j配置文件详细说明

    本篇将详细介绍Log4j的配置文件及其常用属性,旨在帮助初学者快速掌握Log4j的配置方法。 首先,Log4j的配置文件通常是`log4j.properties`或`log4j.xml`,用于定义日志输出的行为。这里我们主要讨论基于文本格式的`...

    log4j测试代码

    本篇将深入探讨Log4j的基本概念、配置以及如何通过实际测试代码来理解和运用它。 1. **Log4j概述** Log4j是一个灵活且功能强大的日志记录工具,它允许开发者调整日志级别、指定日志输出位置、格式化输出信息等。...

    Log4j 把不同包的日志打印到不同位置

    这篇博客"Log4j 把不同包的日志打印到不同位置"可能详细解释了如何利用Log4j配置来实现特定包的日志定向输出,以便于管理和分析来自不同模块的日志信息。 在Java应用中,我们常常会遇到多个模块或者不同包的类同时...

    Tomcat 8.0.30 log4j配置

    本篇文章将深入探讨如何在Tomcat 8.0.30版本中配置Log4j,实现日志自动按天分割并保存。 1. **Log4j介绍** Log4j是Apache软件基金会的一个项目,提供了一个强大的日志API,允许开发者以简单的方式控制日志信息的...

    Kafka+Log4j实现日志集中管理

    <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/> </layout> ``` 然后在日志生产端(即使用Log4j的应用程序)中,...

    WebSphere V5 配置log4j

    本篇文章将详细探讨如何在WebSphere V5环境中配置log4j,以及如何结合iBATIS框架输出SQL语句。 首先,我们要理解log4j是什么。log4j是Apache的一个开源项目,提供了一个灵活且强大的日志系统。它允许程序员以声明式...

Global site tag (gtag.js) - Google Analytics