Log4j – Log4j 2 API
Overview
The Log4j 2 API provides the interface that applications should code to and provides the adapter components required for implementers to create a logging implementation. Although Log4j 2 is broken up between an API and an implementation, the primary purpose of doing so was not to allow multiple implementations, although that is certainly possible, but to clearly define what classes and methods are safe to use in "normal" application code.(log4j 2 API提供应用程序应该编码的接口 和为实施者提供了创建一个日志实现所需的适配器组件。尽管log4j 2破碎API和实现之间的关系,这样做的主要目的是不允许多个实现,虽然这样做是可以实现的,但要在“正常”的应用程序代码中明确界定哪些类和方法的使用是安全的。)
Hello World!
No introduction would be complete without the customary Hello, World example. Here is ours. First, a Logger with the name "HelloWorld" is obtained from the LogManager. Next, the logger is used to write the "Hello, World!" message, however the message will be written only if the Logger is configured to allow informational messages.(使用大家惯用的Hello World来做一个介绍。首先,从LogManager中获取一个叫“HelloWorld”的日志记录器。其次,这个日志记录器是用来写“你好,世界!”的消息,但消息只有在Logger被配置为允许该消息级别的日志时才会被打印。)
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; public class HelloWorld {
private static final Logger logger = LogManager.getLogger("HelloWorld");
public static void main(String[] args) {
logger.info("Hello, World!");
}
}
The output from the call to logger.info() will vary significantly depending on the configuration used. See the Configuration section for more details.( logger.info() 的输出依赖于使用的配置信息。详细的信息查看 Configuration 部分)
Substituting Parameters(替代参数)
Frequently the purpose of logging is to provide information about what is happening in the system, which requires including information about the objects being manipulated. In Log4j 1.x this could be accomplished by doing:(Log4j 1.x 中使用的格式:)
if (logger.isDebugEnabled()) {
logger.debug("Logging in user " + user.getName() + " with birthday " + user.getBirthdayCalendar());
}
Doing this repeatedly has the effect of making the code feel like it is more about logging than the actual task at hand. In addition, it results in the logging level being checked twice; once on the call to isDebugEnabled and once on the debug method. A better alternative would be:(重复这样做会使代码感觉更像是在做日志记录,而不是手头上的实际任务。此外,它的导致记录级别被检查两次;一次是调用logger.isDebugEnabled() ,一次是调用logger.debug()。更好的选择是:)
logger.debug("Logging in user {} with birthday {}", user.getName(), user.getBirthdayCalendar());
With the code above the logging level will only be checked once and the String construction will only occur when debug logging is enabled.(上面的代码只会检查一次日志级别,只有当debug 日志级别启用时,构造字符串的操作才会发生。)
Formatting Parameters(格式化参数)
Formatter Loggers leave formatting up to you if toString() is not what you want. To facilitate formatting, you can use the same format strings as Java's Formatter. For example:(如果使用 toString() 不是想要的格式,可以使用格式化日志记录器自定义格式。为了方便格式化,您可以使用 和java里面的 Formatter 类相同的格式字符串。例如)
public static Logger logger = LogManager.getFormatterLogger("Foo"); logger.debug("Logging in user %s with birthday %s", user.getName(), user.getBirthdayCalendar());
logger.debug("Logging in user %1$s with birthday %2$tm %2$te,%2$tY", user.getName(), user.getBirthdayCalendar());
logger.debug("Integer.MAX_VALUE = %,d", Integer.MAX_VALUE);
logger.debug("Long.MAX_VALUE = %,d", Long.MAX_VALUE);
To use a formatter Logger, you must call one of the LogManager getFormatterLogger methods. The output for this example shows that Calendar toString() is verbose compared to custom formatting:(为了使用一个格式化日志记录器,你一定要调用LogManager.getFormatterLogger()方法获取Logger 。下面的信息显示了Calendar.toString() 比自定义的格式冗长de多 )
2012-12-12 11:56:19,633 [main] DEBUG: User John Smith with birthday java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=false,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/New_York",offset=-18000000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/New_York,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=?,YEAR=1995,MONTH=4,WEEK_OF_YEAR=?,WEEK_OF_MONTH=?,DAY_OF_MONTH=23,DAY_OF_YEAR=?,DAY_OF_WEEK=?,DAY_OF_WEEK_IN_MONTH=?,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=?,ZONE_OFFSET=?,DST_OFFSET=?]
2012-12-12 11:56:19,643 [main] DEBUG: User John Smith with birthday 05 23, 1995
2012-12-12 11:56:19,643 [main] DEBUG: Integer.MAX_VALUE = 2,147,483,647
2012-12-12 11:56:19,643 [main] DEBUG: Long.MAX_VALUE = 9,223,372,036,854,775,807
Mixing Loggers with Formatter Loggers(将Loggers 和 Formatter Loggers混合使用)
Formatter loggers give fine-grained control over the output format, but have the drawback that the correct type must be specified (for example, passing anything other than a decimal integer for a %d format parameter gives an exception).(Formatter loggers提供了细粒度的输出格式控制,但是you一个缺点----必须指定正确的类型(例如,给%d传递一个非数字的值将会抛出异常)。)
If your main usage is to use {}-style parameters, but occasionally you need fine-grained control over the output format, you can use the printf method:(如果你主要使用{} 风格的参数,但是偶尔地你也需要细粒度的控制输出格式,你可以使用printf 方法)
public static Logger logger = LogManager.getLogger("Foo"); logger.debug("Opening connection to {}...", someDataSource);
logger.printf(Level.INFO, "Logging in user %1$s with birthday %2$tm %2$te,%2$tY", user.getName(), user.getBirthdayCalendar());
Java 8 lambda support for lazy logging(Java 8的lambda 可以让你更懒的记录日志)
In release 2.4, the Logger interface added support for lambda expressions. This allows client code to lazily log messages without explicitly checking if the requested log level is enabled. For example, previously you would write:(在2.4版中,日志记录器接口添加了对lambda表达式的支持。这允许客户端代码不需要明确检查请求的日志级别是否启用,可以更懒的打印日志消息。例如,以前你会写:)
// pre-Java 8 style optimization: explicitly check the log level
// to make sure the expensiveOperation() method is only called if necessary
if (logger.isTraceEnabled()) {
logger.trace("Some long-running operation returned {}", expensiveOperation());
}
With Java 8 you can achieve the same effect with a lambda expression. You no longer need to explicitly check the log level:(使用Java 8的lambda 表达式,不再需要明确检查日志级别: )
// Java-8 style optimization: no need to explicitly check the log level:
// the lambda expression is not evaluated if the TRACE level is not enabled
logger.trace("Some long-running operation returned {}", () -> expensiveOperation());
Logger Names(日志记录器的名称)
Most logging implementations use a hierarchical scheme for matching logger names with logging configuration. In this scheme the logger name hierarchy is represented by '.' characters in the logger name, in a fashion very similar to the hierarchy used for Java package names. For example, org.apache.logging.appender and org.apache.logging.filter both have org.apache.logging as their parent. In most cases, applications name their loggers by passing the current class's name to LogManager.getLogger. Because this usage is so common, Log4j 2 provides that as the default when the logger name parameter is either omitted or is null. For example, in all examples below the Logger will have a name of "org.apache.test.MyTest".(大多数日志实现都使用一个分层的方案来匹配日志名称。在这个方案中,日志名称的层次关系是通过’.'来实现的,就像java包名的层次一样。例如,org.apache.logging.appender和org.apache.logging.filter都org.apache.logging作为他们的父级。在大多数情况下,应用程序通过传递当前类的名字给LogManager.getLogger()来命名它们的日志器。因为这种用法用的普遍,log4j 2将它设置为默认参数。例如,在下面所有的日志记录器的名称都会命名为 "org.apache.test.MyTest"。)
package org.apache.test; public class MyTest {
private static final Logger logger = LogManager.getLogger(MyTest.class);
}
package org.apache.test; public class MyTest {
private static final Logger logger = LogManager.getLogger(MyTest.class.getName());
}
package org.apache.test; public class MyTest {
private static final Logger logger = LogManager.getLogger();
}
Log4j – Log4j 2 API的更多相关文章
- 使用log4j实现日志API
添加SLF4J依赖,用于提供日志API, 使用log4j作为实现 1.pom.xml添加SLF4J依赖 <!-- SLF4J --> <dependency> <grou ...
- [log4j]log4j简单配置
步骤: 1.导入jar包:log4j-1.2.17.jar 2.编写log4j配置文件:log4j.properties ### set log levels - for more verbose l ...
- Log4j写日志文件使用详解
Log4j输出到控制台成功,写入文件失败 - Log4j和commons log的整合 一.今天在使用commongs-logging.jar和log4j.properties来输出系统日志的时候,发 ...
- java日志框架slf4j与log4j
日志记录自然是非常重要的,但恐怕能记住slf4j与log4j等日志框架配置的人就很少了,这个东西不难,只是配置好后很少会去动它,开发新项目一般也是从其他项目拷贝,或者参照文档 废话不多说,先说log4 ...
- Log4j、slf4j
1.Log4j 1.1 Log4j简介 Log4j有三个主要的组件:Loggers(记录器),Appenders (输出位置)和Layouts(布局).这里可简单理解为日志类别,日志要输出的地方和日志 ...
- Java 标准日志工具 Log4j 的使用(附源代码)
源代码下载 Log4j 是事实上的 Java 标准日志工具.会不会用 Log4j 在一定程度上可以说是衡量一个开发人员是否是一位合格的 Java 程序员的标准.如果你是一名 Java 程序员,如果你还 ...
- 使用Log4j进行日志操作
使用Log4j进行日志操作 一.Log4j简介 (1)概述 Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接字服 ...
- 日志:slf4j+log4j+maven配置
代码的日志输出,前前后后折腾了我好几次. 本着会用,快速配置的原则,还是将配置过程记录下来,以便复用. 参考:http://blog.csdn.net/anialy/article/details/8 ...
- 跨过slf4j和logback,直接晋级log4j 2
今年一直关注log4j 2,但至今还没有出正式版.等不及了,今天正式向大家介绍一下log4j的升级框架,log4j 2. log4j,相信大家都熟悉,至今对java影响最大的logging系统,至今仍 ...
随机推荐
- Java基础——集合源码解析 List List 接口
今天我们来学习集合的第一大体系 List. List 是一个接口,定义了一组元素是有序的.可重复的集合. List 继承自 Collection,较之 Collection,List 还添加了以下操作 ...
- JSP入门 分页
<div> <% Integer pageNo = (Integer) request.getAttribute("pageNo"); ...
- #翻译#原文来自Database.System.Concepts(6th.Edition.2010)2.6Relational Operations,原文作者Abraham Silberschaz , Henry F. Korth , S. Sudarshan
2.6关系操作 所有的过程关系查询语言都提供一组操作,这些操作可以应用于单个关系或一对关系.这些操作具有良好的和期望的属性,它们的结果总是一个单一的关系.这个属性允许一个以模块化的方式组合其中的几个操 ...
- 『诡异的』VL10B创建外向交货单出错解决全过程
一直觉得SAP STO的业务模式配置起来还是挺简单的,无非就是关联一下采购单与交货单的关系,以及相应工厂的装运数据,其他像主数据的设置也没有什么特别的.相比ICS模式,它少了IDOC的配置,所以还是很 ...
- 自测-5 Shuffling Machine
Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...
- JavaWeb(三)JSP之3个指令、6个动作、9个内置对象和4大作用域
前言 前面大概介绍了什么是JSP,今天我给大家介绍一下JSP的三个指令.6个动作以及它的9大内置对象.接下来我们就直接进入正题 一.JSP的3个指令 JSP指令(directive)是为JSP引擎而设 ...
- Tomcat服务器如何读取本地磁盘数据?
实际问题: 如何让用户下载本地磁盘的资源文件呢? 在server.xml文件中配置虚拟路径如下(以下代码放在Host标签之中即可): 例如: 具体含义: 把本地磁盘目录 "D:\uploa ...
- Python系列之内置函数
内置函数 一.数学运算类: abs(a):求绝对值如果参数是个复数则返回复数的模. a = abs(-1) print(a) >>>1 compilex([real[, imag]] ...
- python装饰器 & flask 通过装饰器 实现 单点登录验证
首先介绍装饰器,以下是一段标注了特殊输出的代码.用于帮助理解装饰器的调用过程. import time def Decorator_one(arg1): info = "\033[1;31; ...
- 传统 HTML 表单数据的“整存整取”
在日常开发中,涉及表单的处理司空见惯.过往,在取值和赋值的过程中,借助 jQuery 常常只是逐个控件进行操作,可惜这样开发效率并不高.那么能不能批量获取整个表单的值呢,以及批量为表单赋值. 一.取值 ...