Log4J Architecture - Introduction to the Log4J architecture
Three main component of Log4J
The architecture of Log4J framework is layered and consists of three
main components. There components of the Log4J are:
<script type="text/javascript"><!--
google_ad_client = "pub-0714075272818912";
/* 120x90, created 1/11/09120x90-Link-1 */
google_ad_slot = "4632423086";
google_ad_width = 120;
google_ad_height = 90;
//-->
</script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">
</script><script>google_protectAndRun("ads_core.google_render_ad", google_handleError, google_render_ad);</script>
- Logger
- Appender
- Layout
1. Logger
Logger is the most essential component of the logging
process. It is responsible for capturing the logging information. There are 5
different log levels of the Logger. The level of Logger are as following:
There are 5 normal levels of logger:
-
DEBUG
:
Most useful to debug an application
.
-
INFO
:
It provides
informational messages.
-
WARN
: It provides that application may have harmful events.
-
ERROR
: It provides that application having error events but that might allow it
to continue running.
-
FATAL
:
It denotes the
severe error events which lead the application
to abort.
In addition, there are two special levels also
they are:
-
ALL
: It is intended to turn on all logging
-
OFF
: It is intended to turn off logging
You
can also set the logger level by putting a simple code like
logger.setLevel((Level)Level.WARN);
|
2. Appenders in Log4J:
The Appender is
responsible for publishing the log to a destination. It controls how the logging
provides the output.
There are few list of
appenders listed below:
-
ConsoleAppender
-
DailyRollingFileAppender
-
FileAppender
-
RollingFileAppender
-
WriterAppender
-
SMTPAppender
-
SocketAppender
-
SocketHubAppender
-
SyslogAppender
sends
-
TelnetAppender
ConsoleAppender
is used as:
ConsoleAppender
appender = new ConsoleAppender(new PatternLayout());
|
FileAppender
is used as:
FileAppender appender
= new FileAppender(new PatternLayout(),"filename");
|
WriterAppender
is used as:
appender = new
WriterAppender(new PatternLayout(),new FileOutputStream("filename"));
|
3. Layouts in Log4J:
For each Appender it needs to have an associated Layout,
which guides how to format the output. The Layout is responsible for formatting
the log output in different layouts. User can control the output format by
modifying the Log4J configuration file.
There are basically three types of Layout:
-
HTMLLayout
:
It formats the output in the HTML table
-
PatternLayout
: It formats the output in the conversion pattern
-
SimpleLayout
:
It formats the output in a simple manner, it prints the
level then place a dash and then the user specified log message.
Configuring Log4J
For running application using Log4J you need to download
the latest version log4j jar
file and then add this to the classpath.
There are two ways to configure the Log4J one is by using
properties file and other by using xml
file. Using xml for Log4J is quite
popular and it is recommended also.
In the next section we will download Log4J
libraries, install
and then run a small program.
log4j.xml Example
log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE}
%5p %c{1}:%L - %m%n"/>
</layout>
</appender>
<root>
<priority value="debug"></priority>
<appender-ref ref="stdout"/>
</root>
</log4j:configuration>
|
Log4j looks for log4j.xml
file first and then go for
log4j.properties
hence you must place both the files in your folder. We use
log4j.xml
since properties file does not provide some advanced configuration
options such as Filter
, some of ErrorHandlers,
and few advanced Appenders
.
log4j.properties
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger=debug, stdout
Here is our SimpleLog
class code:
SimpleLog.java
import org.apache.log4j.*;
public class SimpleLog {
static Logger logger = Logger.getLogger ( "SimpleLog.class" ) ;
public static void main ( String [] args ) {
logger.debug ( "Welcome to Log4j" ) ;
}
}
|
Layouts in Log4j
Users more often wish to customize their output
according to their output format. Layout can be set by attaching it with the
appender. The Layout is responsible for formatting the logging output.
There are following layout classes in Log4j:
-
PatternLayout
-
SimpleLayout
-
HTMLLayout
-
XMLLayout
-
TTCCLayout
Pattern Layout
:
In our this section we are describing about most
general layout PatternLayout
. It is a flexible layout configurable with
its pattern string. A conversion pattern consists of literal text and format
control expressions. Here is the list of some of the conversion characters used
for formatting:
Character
|
Effect
|
c
|
Used to output the category of logging
event
|
C
|
Used to output the fully qualified
class name of the caller issuing the logging request
|
d
|
Used to output date of the logging
event
|
m
|
Used to output the application
supplied message associated with the logging event
|
n
|
Outputs the platform
dependent line
separator character or characters
|
p
|
Used to output the priority of the
logging event
|
r
|
Used to output the number of
milliseconds elapsed from the construction of the layout until the
creation of the logging event
|
t
|
Used to output name of thread of
logging event
|
F
|
Used to output the file name
|
13:07:40,875 DEBUG
class:5 - Welcome to Log4j
<!-- google_ad_section_start -->
<!-- Top -->
<!-- Left -->
A simple example of log4j for Servlet
his Example shows you how to create a log in a Servlet.
Description of the code:
Logger.getLogger():
Logger class is used for handling the majority of
log operations
and getLogger method is used for return a logger according to the
value of the parameter. If the logger already exists, then the existing instance
will be returned. If the logger is not already exist, then create a new
instance.
log.info():
This method is used to check that the specified
category is
INFO enabled or not, if yes then it converts the massage passed as a
string argument to a string by using appropriate object renderer of
class
ObjectRenderer.
LoggingServlet :
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
public class LoggingServlet extends HttpServlet {
private static Logger logger = Logger.getLogger ( LoggingServlet. class ) ;
protected void processRequest ( HttpServletRequest request,
HttpServletResponse response )
throws ServletException, IOException {
response.setContentType ( "text/html;charset=UTF-8" ) ;
PrintWriter writer = response.getWriter () ;
try {
logger.info ( "invoked the LoggingServlet..." ) ;
writer.println ( "Check your web server console..." ) ;
writer.flush () ;
writer.close () ;
} finally {
writer.close () ;
}
}
protected void doGet ( HttpServletRequest request,
HttpServletResponse response )
throws ServletException, IOException {
processRequest ( request, response ) ;
}
protected void doPost ( HttpServletRequest request,
HttpServletResponse response )
throws ServletException, IOException {
processRequest ( request, response ) ;
}
}
|
|
分享到:
相关推荐
《Java日志记录:Log4j入门教程》 在Java编程中,日志记录是一项至关重要的任务,它有助于开发者在程序运行过程中跟踪...在实际工作中,结合《log4j_tutorial.pdf》文档,读者可以更深入地了解和运用Log4j的各项特性。
- **源码下载**:本教程的相关源码可以从 http://www.laliluna.de/download/log4j-tutorial.zip 下载。 #### 六、结语 #### 通过本文档的学习,我们不仅了解了如何设置和配置 log4j,还掌握了其高级功能及最佳实践...
标题“log4j 介绍(6)-- tutorial 参考”指出,这是一篇关于log4j日志框架的教程性文章,可能是系列教程的第六部分,重点是提供学习和参考的指导。描述中提到的"log4j-tutorial-en.pdf"是一个英文版的PDF文档,可能...
This tutorial explains how to set up log4j with email, files and stdout. It compares XML to properties configuration files, shows how to change LogLevels for a running application. Furthermore, we ...
提供的压缩包文件名为 "log4j_tutorial.pdf" 和 "javLog4J.pdf",很可能分别包含了Log4j的基础教程和关于Java中的Log4j使用细节。这些PDF文档可能涵盖了Log4j的配置、用法示例、最佳实践等内容,以及如何在Java项目...
在"Log4j2Tutorial-master"这个压缩包中,可能包含了源代码、配置文件以及相关的说明文档,便于学习者动手实践和理解Log4j2的工作原理。通过学习和实践这个教程,开发者能够熟练掌握Log4j2的使用,提升其在开发和...
`LJ17_tutorial-LJ.pdf`很可能是详细的教程文档,讲解了Lennard-Jones模拟的设置步骤,包括如何定义粒子、如何设定初始配置、如何控制模拟时间和温度,以及如何分析和解释结果。 Lennard-Jones势能是一种广泛使用的...
记录器性能比较:将slf4j与log4j绑定和log4j2性能进行比较 如何运行样本 要运行所有基准测试: mvn clean install java -jar target/benchmarks.jar # default behavior -wi 20 -i 20 -f 10 java -jar target/...
使用Hibernate annotation 实现了一个典型用例,其中用到了MySQL, slf4j, log4j, json-simple等。 资源包含了用到的库,使用前 需要安装MySQL 数据库并配置 /config/persistence.xml中的用户名和密码。
\[ L = -\sum_{j=1}^K y_j \log(P(y=j|x)) \] 这里,\( y_j \) 是一个one-hot编码的真实标签,如果样本属于第j类,则 \( y_j = 1 \),否则 \( y_j = 0 \)。 优化模型参数通常使用梯度下降法或其变种,如随机梯度...
直接计算DFT的时间复杂度为O(N²),而FFT算法通过巧妙的分解将复杂度降低到O(N log N)。这使得在处理大量数据时,FFT比直接DFT方法更快得多。 3. **Cooley-Tukey算法** Cooley-Tukey算法是FFT的最常见实现,它将...
学习如何使用try-catch语句以及选择合适的日志库(如Log4j或SLF4J)是必要的。 总的来说,MAD_Tutorial3可能覆盖了从基础到进阶的Java开发知识,包括模块化、测试、依赖管理、设计模式以及现代开发实践。通过深入...
此外,还需要安装Struts2框架插件、配置Log4j等日志记录工具,以便在开发过程中进行有效的调试和问题追踪。 在数据库方面,教程中提到了创建数据库的步骤,虽然主要围绕着SQL Server,但作者强调Hibernate的灵活性...
同时,Spring Boot支持多种日志框架,如Logback、Log4j2等,方便进行日志管理和分析。 总结,Spring Boot以其简洁、高效的特性,已经成为现代Java开发的首选框架。通过本教程,你可以深入了解Spring Boot的工作原理...
修改Lennard-Jones参数: python lj.py parm.top 4 -d 0.00 0.1 0.2 0.5 -e 1.00 0.95 0.90 0.80 -c heating.rst -r:1-66 -l:67-123 -hMassRepartitioning -o system .top | tee lj.log 要开始模拟,请执行以下...
讲解 ...Log4J 引导程序 ReactJS 贡献者 Todos los教程从实践到实践,从完成到完成。 托尔多·洛斯·因特萨多斯和亲子游记 详细信息。 El material escrito empleando archivos md(MarkDown)。
com.springsource.org.apache.log4j-sources-1.2.15.jar commons-digester3-3.0-sources.jar commons-fileupload-1.2.2-sources.jar hessian-4.0.7-src.jar spring-security-acl-3.0.3.RELEASE-sources.jar spring-...