Inside J2EE using log4j

2010-02-16  来源:本站原创  分类:Java  人气:606 

Basically, the project is currently no development is not to the log management, and the present day management of most of the projects have adopted the apache's log4j, Here's a brief look at log4j in J2EE development of the basic application of

Step one: Configure log4j, first create a log4j.properties file, usually on the class path following, the file reads as follows, specifically about a relatively simple parameter is not detailed instructions, their own Baidu, goolge search click

#log4j.rootLogger = [ level ] , appenderName, appenderName, ...
log4j.rootLogger = DEBUG, console, R
#level=INFO,all can be output
#console is set to be a ConsoleAppender
log4j.appender.console = org.apache.log4j.ConsoleAppender
#console have four patterns
#org.apache.log4j.HTMLLayout
#org.apache.log4j.PatternLayout
#org.apache.log4j.SimpleLayout
#org.apache.log4j.TTCCLayout
log4j.appender.console.layout = org.apache.log4j.PatternLayout
#define the output type
log4j.appender.console.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n
#file is set to output to a extra file
log4j.appender.R = org.apache.log4j.RollingFileAppender
#the absolute route of the log4j file
log4j.appender.R.File = ${webapp.root}/WEB-INF/logs/kingcat.log
#the size
log4j.appender.R.MaxFileSize = 500KB
#back up a file
log4j.appender.R.MaxBackupIndex = 1
log4j.appender.R.layout = org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%-d{yyyy-MM-dd HH\:mm\:ss} [%c]-[%p] - %m%n

The second step: the establishment of a good document, the following configuration in web.xml file, click the file, reads as follows

<!--  Configure Log4J  -->
        <context-param>
                <param-name>log4jConfigLocation</param-name>
                <param-value>/WEB-INF/config/log4j.properties</param-value>
        </context-param>
        <context-param>
                <param-name>log4jRefreshInterval</param-name>
                <param-value>1800000</param-value>
        </context-param>
        <listener>
                <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
        </listener>

Note:

<param-value>/WEB-INF/config/log4j.properties</param-value>

For the log4j configuration file path, the default root directory from the project start, 18 million for the refresh interval Log4j configuration file, the unit of millisecond, so to re-modify the log4j configuration file without restarting the server after the

Step three: Now you can use the inside in the class, simple as, first of all, or a Logger object, and then call the method can be

private static Logger logger = Logger.getLogger(PurviewAction.class);
 logger.error(" An exception occurred while all permissions  ");

Here are some of the Internet on the log4j Note:

First need to be declared in web.xml:
"! - If you do not define webAppRootKey parameters, then webAppRootKey is the default" webapp.root "->
<context-param>
<param-name> webAppRootKey </ param-name>
<param-value> ssh.root </ param-value>
</ context-param>

Ssh here is the http://localhost:8080/ssh/
The root is the system development directory ssh / root

This configuration files and log files can help in the spring, into the development environment anywhere in the

"! - By Sprng load Log4j configuration file location -"
<context-param>
<param-name> log4jConfigLocation </ param-name>
<param-value> / WEB-INF/log4j.properties </ param-value>
</ context-param>

Here locate the configuration file is required is an absolute path starting from the root

<! - Spring default refresh interval Log4j configuration file, the unit for the millisecond ->
<context-param>
<param-name> log4jRefreshInterval </ param-name>
<param-value> 60000 </ param-value>
</ context-param>

<! - Spring log4j Config loader ->
<listener>
<listener-class> org.springframework.web.util.Log4jConfigListener </ listener-class>
</ listener>

Setting Listener

/////////////////
After that we can configure the log4j configuration file of

# First set the level of
log4j.rootCategory = INFO, file
log4j.appender.file = org.apache.log4j.RollingFileAppender
# Set here the location of the log need to be stored, where the variable is that we set in the web.xml inside
log4j.appender.file.File = $ (ssh.root) / WEB-INF/logs/subject.log
log4j.appender.file.MaxFileSize = 100KB
log4j.appender.file.MaxBackupIndex = 0
log4j.appender.file.layout = org.apache.log4j.SimpleLayout
log4j.appender.file.layout.ConversionPattern = [ssh]% p% t% c -% m% n

With the above configuration, we can view the log in!

相关文章
  • Inside J2EE using log4j 2010-02-16

    Basically, the project is currently no development is not to the log management, and the present day management of most of the projects have adopted the apache's log4j, Here's a brief look at log4j in J2EE development of the basic application of Step

  • [J2EE系列]log4j日志输出分级别或模块到不同文件 2012-03-29

    一. 实现分模块,输出到不同的日志文件, 以下是log4j.properties的实现 log4j.appender.file=org.apache.log4j.DailyRollingFileAppender log4j.appender.file.file=src\\log\\all.log log4j.appender.file.DatePattern='.'yyyy-MM-dd log4j.appender.file.layout=org.apache.log4j.PatternLayo

  • Inside J2EE technology using the velocity 2010-04-05

    Template technology in modern software development has an important position, but two of the most popular Template afraid I have to freemarker and velocity of the operator, webwork2.2 good support on both, meaning that you can in webwork2 arbitrarily

  • log4j and J2EE integration (from Baidu Encyclopedia) 2010-07-13

    Official site: http://logging.apache.org/log4j/ How do we look at development in the J2EE application log4j. In fact, the application of Web application is very simple log4j, and log4j in the application the application is different from all the appl

  • log4j configuration instructions (found a relatively long) 2008-07-21

    Log4j is an open-source Apache project, through the use of Log4j, we can control the destination of the log information transmission is the console, file, GUI components, and even socket servers, NT event logger, UNIX Syslog daemon, etc. ; we can als

  • Report class "org.apache.log4j.PropertyConfigurator" 's signer information does not ... 2010-07-02

    Today, the project will be placed on tomcat6.0 tomcat5.5 years running, reports: class "org.apache.log4j.PropertyConfigurator" 's signer information does not. . . . . Depressed, and a close look at the problem seems to be log4j, it is not true,

  • VI. Dwr filters and log configuration 2009-04-08

    DWR filters and log configuration 1, filters use DWR has provided us with the use of filters, as long as we realize the DWR has provided us with a filter interface, we can use at DWR filter. DWR look of the document org.directwebremoting Interface Aj

  • log4j and spring configuration file location ---- J2EE 2010-08-16

    In the J2EE development environment, we hope that the default configuration files are unified as far as possible be placed under WEB-INF. This article explained how to change the SPRING and LOG4J configuration file location. When configuring web.xml,

  • J2EE Architect road 2009-02-27

    J2EE Architect road Looking casual, working into the fifth year, the discovery came from the Java programmer to architect J2EE course. Found that computer to install a wide range of J2EE tools: JBuilder, WSAD, Eclipse, Rose, Together, Weblogic, Jtest

  • Development of commonly used open source J2EE project 2009-03-08

    Read other people's records, feel good, on the collection here, Original Source: http://blog.csdn.net/fenixshadow/archive/2007/11/17/1890010.aspx Persistence Layer 1: 1) Hibernate This need not introduce, and used very frequently, used more are mappe

  • Uncover the hood of j2ee clustering 2009-04-09

    Source article: http://www.theserverside.com/tt/articles/article.tss?l=J2EEClustering Article discussion: http://www.theserverside.com/news/thread.tss?thread_id=35831 Preface More and more mission-critical and large scale applications are now running

  • j2ee overall study plan 2009-04-27

    J2EE overall study plan Part I: Basic knowledge of JAVA language. Including abnormal, IO streams, multithreading, collections, databases. (Bear in mind the basic knowledge must always want to consolidate) Requirements: on the java package, the life-c

  • J2EE project data persistence layer design 2009-04-28

    J2EE project data persistence layer design Data Persistence Layer design goal is to provide a high level throughout the project, unity, security, and concurrent data persistence mechanism. Achieve the various data persistence programming job, and for

  • J2EE development process 2009-08-07

    Starting point In the "J2EE" The abbreviations have been introduced to the world the first time, perhaps few people can be expected in the future of its unique history. That was in June 1999 in the JavaOne conference, Sun Company, then director

  • Inefficient J2EE ------- Why so users can not participate in the development 2009-11-21

    It is found from the Internet to the definition of a J2EE. Quote J2EE is a Java 2 platform, the use of enterprise solutions to simplify development, deployment and management of complex issues related to architecture. J2EE technology is the core Java

  • JBoss Log4j conflict resolution methods 2010-03-29

    Boss built Log4j, so if in their own projects to use Log4j, configured in the log4j.xml within the project will be conflict. Because in the JBoss unified management concept is the log, so all of the logs are output to the system log inside. JBoss pro

  • Share 2 java j2ee training ppt 2010-03-29

    A few years ago written in-house training ppt. An introduction j2ee base, including Servlet / Filter / Session Listener to start-up and preparation methods, and why they exist in the world the truth. This ppt copy of the ancestors of some knowledge,

  • Use log4j 2010-03-29

    While emphasizing the development of reusable components, today, in addition to its own from start to finish development of a reusable logging operations outside classes, Apache provides us with a powerful logging operations package-Log4j. Log4j is a

  • 2007 Summary Records of the Historian of the company's largest J2EE technology 2010-03-29

    2007 Great Records of the Historian's J2EE technology (Postscript: At that time quite like the "big Historical Records" series Bloopers, recommended to see) Probably sum up the current development of our company to be acquired some knowledge poi

  • log4j.properties configuration Xiang Jie 2010-03-29

    While emphasizing the development of reusable components, today, in addition to its own from start to finish development of a reusable logging operations outside classes, Apache provides us with a powerful logging operations package-Log4j. As the Apa