Log4J简介
1 主要由三大组件构成:
Logger:决定什么日志信息应该被输出,什么日志信息应该被忽略
eg. logrj.logger.helloappLogger=WARN
以上代码定义了一个Logger组件,名为helloappLogger,并为它分配了一个日志级别WARN,(一共有5种日志级别:FATAL,ERROR,WARN,INFO,DEBUG)
它的日志级别为WARN,在程序口,它的fatal(),error(),warn()方法才会被执行.而info(),debug()方法不会被执行.
Appender:指定日志信息应该被输出到什么地方(控制台,文件,网络设备)
Layout:指定日志信息的输出格式
一个Logger可以有多个Appender,每个Appender都对应一种Layout
2使用方法
一个配置文件(properties.lcf);
一个Log4J的JAR文件,(log4j-1.2.8.jar);
使用包含三步骤:
Logger.getLogger(); //获得日志记录器
PropertyConfigurator.configure("properties.lcf");//读取配置文件
logger.debug()/info()/fatal(); //插入日志信息
(以上这种方法没初始
报错如下:
log4j:ERROR Could not read configuration file [properties.lcf].
java.io.FileNotFoundException: properties.lcf (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:297)
at org.apache.log4j.PropertyConfigurator.configure(PropertyConfigurator.java:315)
at log.Log4JApp.main(Log4JApp.java:20)
log4j:ERROR Ignoring configuration file [properties.lcf].
log4j:WARN No appenders could be found for logger (helloappLogger).
log4j:WARN Please initialize the log4j system properly.)
3一个例子(源见我的空间),比2多一个初始化的servlet
src中加一个包mypack.Log4JServlet.java
(这个Servlet在Web应用启动的时候就被加载和初始化了,这个是在web.xml中配置做到的)
Log4JServlet.java
package mypack;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import org.apache.log4j.PropertyConfigurator;
public class Log4JServlet extends HttpServlet {
public void init()
throws ServletException {
// Get Fully Qualified Path to Properties File
String path = getServletContext().getRealPath("/");
String propfile = path + getInitParameter("propfile");
// Initialize Properties for All Servlets
PropertyConfigurator.configure(propfile);
}
}
加入log4j的jar包
在web-inf口加入log4j.properites
#define a logger named helloAppLogger
log4j.logger.helloappLogger=WARN,console,file
## APPENDERS ##
# define an appender named console, which is set to be a ConsoleAppender
log4j.appender.console=org.apache.log4j.ConsoleAppender
# define an appender named file, which is set to be a RollingFileAppender
log4j.appender.file=org.apache.log4j.RollingFileAppender
# replace <CATALINA_HOME> with your true path
log4j.appender.file.File=%Tomcat_home%/webapps/工程名/WEB-INF/log.txt
## LAYOUTS ##
# assign a SimpleLayout to console appender
log4j.appender.console.layout=org.apache.log4j.SimpleLayout
# assign a PatternLayout to file appender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%t %p - %m%n
web.xml
加入如下几句话:
<servlet>
<servlet-name>log4j</servlet-name>
<servlet-class>mypack.Log4JServlet</servlet-class>
<init-param>
<param-name>propfile</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
使用例子login.jsp
<%@ page import="org.apache.log4j.Logger" %>
<html>
<head>
<title>helloapp</title>
</head>
<body >
<%
Logger helloappLogger = Logger.getLogger("helloappLogger");
// Log Messages using the Parent Logger
helloappLogger.debug("This is a log message from the " +
helloappLogger.getName());
helloappLogger.info("This is a log message from the " +
helloappLogger.getName());
helloappLogger.warn("This is a log message from the " +
helloappLogger.getName());
helloappLogger.error("This is a log message from the " +
helloappLogger.getName());
helloappLogger.fatal("This is a log message from the " +
helloappLogger.getName());
%>
<br>
<form name="loginForm" method="post" action="dispatcher">
<table>
<tr><td><div align="right">User Name:</div></td><td> <input type="text" name="username"></td></tr>
<tr><td><div align="right">Password:</div></td><td><input type="password" name="password"></td></tr>
<tr><td></td><td><input type="Submit" name="Submit" value="Submit"></td></tr>
</table>
</form>
</body>
</html>
运行http://localhost:8080/工程名/login.jsp
控制台输出结果
WARN - This is a log message from the helloappLogger
ERROR - This is a log message from the helloappLogger
FATAL - This is a log message from the helloappLogger