Log4j entry and example

Source: Internet
Author: User

Log4j Introduction
1 consists of three major components:
Logger: determines what log information should be output and what log information should be ignored.
Eg. logrj. Logger. helloapplogger = warn
The code above defines a logger component named helloapplogger and assigns it a log level warn (a total of five log levels: fatal, error, warn, info, debug)
Its log level is warn. In the program port, its fatal (), error (), and warn () methods will be executed. the info () and debug () methods are not executed.
Appender: specify where the log information should be output (console, file, network device)
Layout: Specify the log output format.

A logger can have multiple appender. Each appender corresponds to a layout

2 usage
A configuration file (properties. LCF );
A log4j JAR file (log4j-1.2.8.jar );
Three steps are involved:
Logger. getlogger (); // obtain the logger
Propertyconfigurator. Configure ("properties. LCF"); // read the configuration file
Logger. debug ()/info ()/fatal (); // insert log information
(The above method is not initial.
The following error is reported:
Log4j: Error cocould not read configuration file [properties. LCF].
Java. Io. filenotfoundexception: properties. LCF (the specified file cannot be found .)
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 cocould be found for logger (helloapplogger ).
Log4j: Warn please initialize the log4j system properly .)
3. for example (for the source, see my space), there is an initialized servlet more than 2.
Add mypack. log4jservlet. Java to SRC.

(This servlet is loaded and initialized when the web application is started. This is configured in 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 );
}

}

Add the jar package of log4j
Add log4j. properites to the Web-INF Port

# 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. leleappender

# 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/project name/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
Add the following statements:
<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>
Example: 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> <input type = "text" name = "username"> </TD> </tr>
<Tr> <TD> <Div align = "right"> password: </div> </TD> <input type = "password" name = "password"> </TD> </tr>
<Tr> <TD> </TD> <input type = "Submit" name = "Submit" value = "Submit"> </TD> </tr>
</Table>
</Form>

</Body>
</Html>

Run http: // localhost: 8080/project name/login. jsp

Console output result

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

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.