| Author |
logging lifecyle events of servlet
|
naveen yadav
Ranch Hand
Joined: Oct 23, 2011
Posts: 380
|
|
hi ranchers ,
i am new to Servlet . and i just exploring the basic things.As a part of this i want to create a small servlet program in which i log all the life cycle events of a servlet .
One idea is to just create a file using java.io api and write the event into that file .But i don't want to use that since there must be some standard way like tool aor api to do that.
please guide
|
 |
honey sachdeva
Greenhorn
Joined: Feb 05, 2011
Posts: 6
|
|
hi,
you can use System.out.println().
this will write in web server log file .
|
 |
naveen yadav
Ranch Hand
Joined: Oct 23, 2011
Posts: 380
|
|
log(String message) in GenericServlet class :: Writes the specified message to a servlet log file, usually an event log. The name and type of the servlet log file is specific to the servlet container
but The name and type of the servlet log file is specific to the servlet container. how can i do that ?
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56157
|
|
honey sachdeva wrote:you can use System.out.println().
Not the best of ideas.
Most professionals would use Common Logging with one of java.util.logging or Log4J.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
honey sachdeva
Greenhorn
Joined: Feb 05, 2011
Posts: 6
|
|
yes you are write about java.util.logging or Log4J.
i just answer System.out.println() because he says " i am new to Servlet "
|
 |
naveen yadav
Ranch Hand
Joined: Oct 23, 2011
Posts: 380
|
|
i am currently using the GlassFish server. will Log4j work with GlassFish Server ?
|
 |
Kumaravadivel Subramani
Ranch Hand
Joined: Jul 05, 2008
Posts: 162
|
|
|
It's a common logging tool, work well with GlassFish.
|
No pain, No gain.
OCJP 1.6
|
 |
naveen yadav
Ranch Hand
Joined: Oct 23, 2011
Posts: 380
|
|
i am using Log4j with GlassFish Server.
i am trying to log the message that "servlet context is initialized." when the servlet of LifeCycle application is invoked
code goes as :
i have added the required log4j-1.2.16 file to build path of application.
GlaasFish server cannot deploy the application(LifeCycle) and show following message :
cannot Deploy LifeCycle
Deployment Error for module: LifeCycle: Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.lang.NoClassDefFoundError: org/apache/log4j/Logger.
help me out.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
You must also add that JAR file to your web application's class path.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Vic Hood
Ranch Hand
Joined: Jan 05, 2011
Posts: 477
|
|
Also, I would suggest you use something like SLF4J, since you say you are a beginner.
|
Learning and Learning!-- Java all the way!
|
 |
naveen yadav
Ranch Hand
Joined: Oct 23, 2011
Posts: 380
|
|
thanks Rob.
i place the log4j-1.2.16.jar file inside WEB-INF\lib\ directory and it work fine.
one more thing it is related to Log4j :
BasicConfigurator. configure() method will log all the messages on the console. so according to this "ServletContext is Initialized" must be on console.
But the message is written into server log file and then server log file is displayed on console. how so ?
|
 |
naveen yadav
Ranch Hand
Joined: Oct 23, 2011
Posts: 380
|
|
hi ranchers ,
i created a small application in which i track all the life cycle events of LifeServlet servlet. And use the MyLogger class to log the event using the Log4j tool.
everything is working fine .But when is see the log file of server i get this :
[#|2011-12-08T11:23:06.078-0800|INFO|oracle-glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=10;_ThreadName=Thread-2;|0 [main] INFO org.MyLogger - ServletContext object is Initialized
|#]
[#|2011-12-08T11:23:06.093-0800|INFO|oracle-glassfish3.1.1|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=10;_ThreadName=Thread-2;|WEB0671: Loading application [LifeCycle] at [/LifeCycle]|#]
[#|2011-12-08T11:23:06.093-0800|INFO|oracle-glassfish3.1.1|javax.enterprise.system.core.com.sun.enterprise.v3.server|_ThreadID=10;_ThreadName=Thread-2;|CORE10010: Loading application LifeCycle done in 453 ms|#]
[#|2011-12-08T11:23:06.093-0800|INFO|oracle-glassfish3.1.1|javax.enterprise.system.core.com.sun.enterprise.v3.server|_ThreadID=10;_ThreadName=Thread-2;|Oracle GlassFish Server 3.1.1 (12) startup time : Felix (1,219ms), startup services(3,093ms), total(4,312ms)|#]
[#|2011-12-08T11:23:06.125-0800|INFO|oracle-glassfish3.1.1|javax.enterprise.system.tools.admin.com.sun.enterprise.container.common|_ThreadID=16;_ThreadName=Thread-2;|User [admin] from host localhost does not have administration access|#]
[#|2011-12-08T11:23:06.265-0800|INFO|oracle-glassfish3.1.1|javax.enterprise.system.tools.admin.org.glassfish.server|_ThreadID=17;_ThreadName=Thread-2;|JMXStartupService: Started JMXConnector, JMXService URL = service:jmx:rmi://sunmicro-fe959d:8686/jndi/rmi://sunmicro-fe959d:8686/jmxrmi|#]
[#|2011-12-08T11:23:07.765-0800|INFO|oracle-glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=18;_ThreadName=Thread-2;|1687 [http-thread-pool-8080(2)] INFO org.MyLogger - intit() is called
|#]
[#|2011-12-08T11:23:07.765-0800|INFO|oracle-glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=18;_ThreadName=Thread-2;|1687 [http-thread-pool-8080(2)] INFO org.MyLogger - intit() is called
|#]
[#|2011-12-08T11:23:07.765-0800|INFO|oracle-glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=18;_ThreadName=Thread-2;|1687 [http-thread-pool-8080(2)] INFO org.MyLogger - service() is called
|#]
[#|2011-12-08T11:23:07.765-0800|INFO|oracle-glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=18;_ThreadName=Thread-2;|1687 [http-thread-pool-8080(2)] INFO org.MyLogger - service() is called
|#]
[#|2011-12-08T11:23:07.765-0800|INFO|oracle-glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=18;_ThreadName=Thread-2;|1687 [http-thread-pool-8080(2)] INFO org.MyLogger - ServletRequest object is Initialized
|#]
[#|2011-12-08T11:23:07.765-0800|INFO|oracle-glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=18;_ThreadName=Thread-2;|1687 [http-thread-pool-8080(2)] INFO org.MyLogger - ServletRequest object is Initialized
|#]
[#|2011-12-08T11:23:07.765-0800|INFO|oracle-glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=18;_ThreadName=Thread-2;|1687 [http-thread-pool-8080(2)] INFO org.MyLogger - ServletResponse object is Initialized
|#]
[#|2011-12-08T11:23:07.765-0800|INFO|oracle-glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=18;_ThreadName=Thread-2;|1687 [http-thread-pool-8080(2)] INFO org.MyLogger - ServletResponse object is Initialized
|#]
I have two question
Q1 : why ServletContext is initialized before LifeCycle application is loaded ? shouln't be other ways around ?
Q2: why init() and service() is called twice?
for more understating here is my code of LifeServlet
|
 |
 |
|
|
subject: logging lifecyle events of servlet
|
|
|