Today i add Log4j technology into my program,and meet some problems.After seeking solutions in internet,i have resolved it .Now,let me introduce the step of using Log4j and CommonsLogging , post these problems and the way to resolve them.
Fisrtly: Assure you download "log4j-1.2.15.jar"(you can choose any version as you need) and "commons-logging.jar",and these into /WEB-INF/lib/ of your project.
Secondly: Create “commons-logging.properties” and “log4j.properties” under /WEB-INF/classes/.
write following contents into file of "commons-logging.properties"
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
write following into file of "log4j.properties"
log4j.rootLogger=INFO,console,file
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=E:/project/demonetbeansstructDBLog4jtest/demo_log.txt
log4j.appender.file.MaxFileSize=1000KB
log4j.appender.file.MaxBackupIndex=20
log4j.appender.console.layout=org.apache.log4j.SimpleLayout
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%-5p][%d{yyyy-MM-dd HH:mm:ss}]%m%n
Problem1:
peizhi:org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
警告: Cannot start JMX connector: JmxConnector config: { name = system, Protocol = rmi_jrmp, Address = 0.0.0.0, Port = 8686, AcceptAll = false, AuthRealmName = admin-realm, SecurityEnabled = false}: java.lang.RuntimeException: Port 8686 is not available for the internal rmi registry. This means that a call was made with the same port, without closing earlier registry instance. This has to do with the system jmx connector configuration in admin-service element of the configuration associated with this instance
Solution:do as the first step
Problem2:
Serious: WebModule[/demonetbeansstructDBLog4jtest]StandardWrapper.Throwable
java.lang.NoSuchMethodError: org.apache.log4j.Category.log(Ljava/lang/String;Lorg/apache/log4j/Level;Ljava/lang/Object;Ljava/lang/Throwable;)V
Serious: WebModule[/demonetbeansstructDBLog4jtest]PWC1396: Servlet /demonetbeansstructDBLog4jtest threw load() exception
java.lang.NoSuchMethodError: org.apache.log4j.Category.log(Ljava/lang/String;Lorg/apache/log4j/Level;Ljava/lang/Object;Ljava/lang/Throwable;)V
Serious: ContainerBase.addChild: start:
LifecycleException: java.lang.NoSuchMethodError: org.apache.log4j.Category.log (Ljava/lang/String;Lorg/apache/log4j/Level;Ljava/lang/Object;Ljava/lang/Throwable;)V
Warning: java.lang.IllegalStateException: ContainerBase.addChild: start: LifecycleException: java.lang.NoSuchMethodError: org.apache.log4j.Category.log(Ljava/lang/String;Lorg/apache/log4j/Level;Ljava/lang/Object;Ljava/lang/Throwable;)V
java.lang.IllegalStateException: ContainerBase.addChild: start: LifecycleException: java.lang.NoSuchMethodError: org.apache.log4j.Category.log(Ljava/lang/String;Lorg/apache/log4j/Level;Ljava/lang/Object;Ljava/lang/Throwable;)V
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:926)
Serious: Exception while invoking class com.sun.enterprise.web.WebApplication start method
java.lang.Exception: java.lang.IllegalStateException: ContainerBase.addChild: start: LifecycleException: java.lang.NoSuchMethodError: org.apache.log4j.Category.log(Ljava/lang/String;Lorg/apache/log4j/Level;Ljava/lang/Object;Ljava/lang/Throwable;)V
at com.sun.enterprise.web.WebApplication.start(WebApplication.java:111)
Serious: Exception while deploying the app
java.lang.Exception: java.lang.IllegalStateException: ContainerBase.addChild: start: LifecycleException: java.lang.NoSuchMethodError: org.apache.log4j.Category.log(Ljava/lang/String;Lorg/apache/log4j/Level;Ljava/lang/Object;Ljava/lang/Throwable;)V
at com.sun.enterprise.web.WebApplication.start(WebApplication.java:111)
Solution:
update file of commons-logging.properties,"org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JCategoryLog" replaced by "org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger"
Problem3:
Serious: log4j:ERROR Could not instantiate class [E:/project/demonetbeansstructDBLog4jtest/demo_log.txt].
Serious: java.lang.ClassNotFoundException: E:/project/demonetbeansstructDBLog4jtest/demo_log.txt
Solution:
Take care the statment:"log4j.appender.file.File=E:/project/demonetbeansstructDBLog4jtest/demo_log.txt",be ssure you have write "File"
Lastly:add record codes into Action.
eg.in file of LoginAction,i add two statments:
Logger log=Logger.getLogger(this.getClass());//add a log variable into LoginAction class
log.info("User"+username+"login"); //add into execute() method
If you use username named admin login system,you will find records in "E:/project/demonetbeansstructDBLog4jtest/demo_log.txt" as following
[INFO] [2009-04-03 15:00:05] User admin login.
Reference:
http://jackywcw.bokee.com/viewdiary.176607097.html
http://lavasoft.blog.51cto.com/62575/26134
《开发者突击:java web主流框架整合开发 j2ee+Structs+Hibernate+Spring》 刘中兵编著 ,电子工业出版社