Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

logging lifecyle events of servlet

 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

you can use System.out.println().

this will write in web server log file .

 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 65223
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
honey sachdeva
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i am currently using the GlassFish server. will Log4j work with GlassFish Server ?

 
Kumaravadivel Subramani
Ranch Hand
Posts: 171
Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a common logging tool, work well with GlassFish.
 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must also add that JAR file to your web application's class path.
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, I would suggest you use something like SLF4J, since you say you are a beginner.
 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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







 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic