Link to home
Start Free TrialLog in
Avatar of zerg2000
zerg2000

asked on

Tomcat 4: using Catalina logger

Hi,
I am using Tomcat 4.1 and I am able to set my servlet processing log message to catalina log file


<Context path="/demo10" docBase="D:\demo10" debug="0"><Logger className="org.apache.catalina.logger.FileLogger"        prefix="demo10" suffix=".txt" timestamp="true"/>             </Context>


My log file output:

2003-03-26 15:06:30 StandardManager[/demo31]: Seeding of random number generator has been completed2003-03-26 15:06:30 StandardWrapper[/demo31:default]: Loading container servlet default2003-03-26 15:06:30 StandardWrapper[/demo31:invoker]: Loading container servlet invoker2003-03-26 15:07:59 StandardWrapperValve[F13TierServlet]: Servlet.service() for servlet F13TierServlet threw exceptionjavax.servlet.ServletException: Servlet execution threw an exception     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:260)

My Question:
How do I output my application debug message to the same catalina log file. For example, instead of using System.out.println("vvv") to command console, I would like to send the message to the above log file.
Please teach me. Thank you.
Avatar of victorli
victorli
Flag of China image

From my understanding, Tomcat has 2 log levels.

(1) Tomcat system logs level is handled by class "org.apache.catalina.logger.FileLogger" and logs goto the file you configured above.

(2) You application log messages are handled by class "org.apache.catalina.logger.SystemOutLogger" and
"org.apache.catalina.logger.SystemErrLogger". The default log file is "catalina.out" which is located at "../jarktaTomcat/logs/" directory. So if you use "System.err" or "System.out" print your log messages, you will see them in "catalina.out" file.

So please check your "catalina.out" file. This is where you look at your applicaiton logs.
Avatar of zerg2000
zerg2000

ASKER

Hi Victorli,

Thank you for your advice.
However, I am not sure how to proceed after your reading your instruction.

Let's say I have a Java Bean class in my server side, how do I the initialization of the catalina logger in order to redirect my System.out.println() to the log file?

// what to import?
public class test
{
     public test()
     {
        // what to do??
     }
}

Thank you very much.
// need not do any import.

public class test
{
    public test()
    {
         try {
              ...
         } catch (Exception ex) {
               System.out.println("my error message"+ex.getMessage()); etc...
  //or use System.err.println("...")  
         }          
    }
}

Then your applicaiton error message will be stored in the default file "catalina.out". I check this file regularly in my company's website. It is very useful.

In my case, I did not configure any log setting in the "server.xml". I am not sure if you can configure Tomcat to let it output application messages to another file instead of "catalina.out". I donot think it is necessary.

Using "System.out" or "System.err" to print debug information is not that good. One of Java 1.4's new features is "Logger" class. Use this class may be better.
Hi Victorli,

Thank you for your help.
But we have some misunderstanding. Currently, my System.out.println() will print the message to console, not catalina.out default file.

So, my question is, how to make my System.out.println() to output catalina.out default file, like what happen in your system.

Thanks again.
ASKER CERTIFIED SOLUTION
Avatar of victorli
victorli
Flag of China image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi vitorli,

Thanks for your effort. I will try to digest what you said.

Here is your dollars.

Thanks
Hi victorli,

!!You need to test and run them inside Tomcat instead of command line or IDE!!

I think the problem is from here. What do you mean by the above statement? I started my Tomcat by double-clicking the startup.bat from my Windows Explorer. In this way, a command console will be brought up and all the messages from System.out.println() will appear on the Command console.

Please help me. Thanks
Hi victorli,

!!You need to test and run them inside Tomcat instead of command line or IDE!!

I think the problem is from here. What do you mean by the above statement? I started my Tomcat by double-clicking the startup.bat from my Windows Explorer. In this way, a command console will be brought up and all the messages from System.out.println() will appear on the Command console.

Please help me. Thanks
Ooooops! I forget about your Tomcat is running in Windows. Very sorry about that.

You should not accept my answer at this stage.

Anyway, I just checked the Windows edition of Tomcat4.1.24 and found "catalina.out" does not exist(in Linux it is always there). Maybe we need to confugure the "server.xml" and put the "<Logger ClassName="org.apache.catalina.logger.SystemOutLogger ... to somewhere.

Let me figure it out...



Thanks alot victorli,

I tried your method. Below is part of my server.xml:

<Context path="/demo" docBase="D:\demo" debug="0">
        <Logger className="org.apache.catalina.logger.SystemOutLogger"
                     prefix="demo" suffix=".txt" timestamp="true"/>            
        </Context>

More output to the console this time. And System.out.println is output to the console also. My demo log file still only contains servelt engine log message, not my application message. Please help.

WebappLoader[/demo]: Deploying class repositories to work directory D:\Tomcat\
work\Standalone\localhost\demo
WebappLoader[/demo]: Deploy class files /WEB-INF/classes to D:\demo\WEB-INF\classes
StandardManager[/demo]: Seeding random number generator class java.security.Se
cureRandom
StandardManager[/demo]: Seeding of random number generator has been completed
StandardWrapper[/demo:default]: Loading container servlet default
StandardWrapper[/demo:invoker]: Loading container servlet invoker
Hi zerg2000,
I don't know if you're still watching this topic, however
if you need to print messages to the log file you just need to use the
javax.servlet.GenericServlet.log( String ) method!!!

This method 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 (in the server.xml file if you're using Tomcat).

Hope this can help you.