You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@jakarta.apache.org by Jeff Haynes <Je...@techno.ca> on 2000/02/25 17:27:38 UTC

Re: Is it possible to log errors to server's log file in Tomcat 3.0?

Over the past couple of years I've worked with a number of different
servlet engines - IBM Websphere (webfear), Jrun, Jserv,
Weblogic (great but mega $$$) and most recently Tomcat.  One of the
things I've found about moving code between these various servers
is the inconsistency in logging - some of them support System logging, 
and some don't.  The simple solution to this problem was to use a 
server-independent logging class that could be used anywhere. 

I wrote a very simple singleton class that will allow me to 
log to a central file from any servlet (or any other Java code,
including JSP pages). I use a few of these across different 
applications on my site in order to keep logs for each section 
of the site in one place.  For example, I have a separate Member 
log and Msgboard log for those respective application elements.  
I find using this simple class makes life a lot easier when it 
comes to debugging and monitoring applications, not to mention 
the savings in time & effort having a logging system that works 
anywhere.

The following is an example of the logging class I'm currently using
with Tomcat.  This was a very quick hack meant to get logging up
and running while I'm troubleshooting my application.  There are
two issues I still have with this code:

1)	The code uses the java.io.PrintStream(java.io.OutputStream)
	constructor, which is giving me a deprecation warning
	when I compile using IBM JDK 1.1.8.  It still works, but 
	I need to update the code.
2)	There is no facility to roll the log when you restart the
	server.  This means any old logs will be blown away.  If
	you want to keep old logs, I would suggest renaming the
	old log first (like tomcatlog.1) in the init method before
	opening up the file for logging.  I plan on implementing 
	this when I get around to it.

If anyone works on these issues, could you please post the code
back to the list?  

This should be enough to get you going.  Hope it helps.


Jeff


--

To use this class:

- compile and put it in your classpath (I am master of the obvious)
- make sure Tomcat has write permissions to your log file (unless you're
  running it as root, which is not advised in a production environment)
- Add the following code snippet anywhere you need it (make sure you
  import CustomLog into any class using it):

	CustomLog log = CustomLog.instance();
	log.println("Logging to the custom log");






/*
 *      CustomLog
 *
 *      Copyright 2000 Tone Network Inc.
 *
 *      Author:         Jeff Haynes <je...@techno.ca>
 *      Written:        February 19, 2000
 *      Version:        1.0
 */
         
import java.io.*;
import java.net.*;
import java.util.*;
         
public class CustomLog {

        private static PrintStream log;
        private static CustomLog _instance = null;
	 private static final String logFileString = "/var/log/tomcat.log";

        /*
         * Construct the only CustomLog object.
         */
        private CustomLog()
        {
                init();
        }
                
                 
        /*
         * Initializes the singleton by opening the log file for
         * printing to it.
         */
        private void init()
        {
	         Date now = new java.util.Date();

                try
                {
                        log = new PrintStream(new
FileOutputStream(logFileString));
                        log.println(now.toString() + " Starting custom
logging.");
                        System.out.println(now.toString()
                                + " Opened up custom log: " + logFileString);
                }
                catch (IOException e)
                {
                        System.out.println("Can't open custom log: "
                                + logFileString);
                }
        }  

       /**
         * Returns the instance of this class.
         */
        public static CustomLog instance ()   
        {
                if (_instance == null)
                        _instance = new CustomLog();
                return _instance;
        }
                        
                                
        /*
         * Prints the passed string to the log file.
         *
         * @param s     string to print in the log
         */
        public static void println(String s)
        {  
                log.println(s);
        }

}




At 03:24 PM 2/25/00 +0530, you wrote:
>Hi all,
>
>    I'm using Tomcat 3.0 with Apache 1.3.9(jdk1.2.2). My question is it
>possible to log errors that occurs in servlets  and JSPs to Apache's error
>log file?
>
>Thanks,
>Sudheer.
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: general-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: general-help@jakarta.apache.org
>
>

Re: Is it possible to log errors to server's log file inTomcat 3.0?

Posted by Anil Vijendran <An...@eng.sun.com>.
Jose Alberto Fernandez wrote:

> So how does this work if I am running multiple VMs in the same or
> separate
> machines? Does every VM gets its own log file?

In Tomcat right now every VM (in the multiple VM case) gets its own log file.
When I implemented logging, I thought about having a logging backend into which
you could make remote calls to from any VM/process and that would generate entries
in one configurable log file (system-wide or per process or whatever) but it
seemed too heavy weight for now.

> Logging should be a done by the container. Because loggin is dependent
> of the execution environment. For example, with your class all requests
> are competing to get access to the same logging object which means your
> logging is a bottle neck to scalability. A smart container must try
> very hard to avoid having threads wayting for the log. You should
> probably need a queue of logging messages and a logger thread that takes
> the care of it.

This is exactly how it works currently.

--
Peace, Anil +<:-)




Re: Is it possible to log errors to server's log file inTomcat 3.0?

Posted by Jose Alberto Fernandez <jo...@us.oracle.com>.
So how does this work if I am running multiple VMs in the same or
separate 
machines? Does every VM gets its own log file?

Logging should be a done by the container. Because loggin is dependent
of the execution environment. For example, with your class all requests
are competing to get access to the same logging object which means your
logging is a bottle neck to scalability. A smart container must try
very hard to avoid having threads wayting for the log. You should
probably need a queue of logging messages and a logger thread that takes
the care of it. 

The problem may be worst if you have a distribute container and even
worst
if HTTPSessions can migrate between different VMs from request to
request.

Jeff Haynes wrote:
> 
> Over the past couple of years I've worked with a number of different
> servlet engines - IBM Websphere (webfear), Jrun, Jserv,
> Weblogic (great but mega $$$) and most recently Tomcat.  One of the
> things I've found about moving code between these various servers
> is the inconsistency in logging - some of them support System logging,
> and some don't.  The simple solution to this problem was to use a
> server-independent logging class that could be used anywhere.
> 
> I wrote a very simple singleton class that will allow me to
> log to a central file from any servlet (or any other Java code,
> including JSP pages). I use a few of these across different
> applications on my site in order to keep logs for each section
> of the site in one place.  For example, I have a separate Member
> log and Msgboard log for those respective application elements.
> I find using this simple class makes life a lot easier when it
> comes to debugging and monitoring applications, not to mention
> the savings in time & effort having a logging system that works
> anywhere.
> 
> The following is an example of the logging class I'm currently using
> with Tomcat.  This was a very quick hack meant to get logging up
> and running while I'm troubleshooting my application.  There are
> two issues I still have with this code:
> 
> 1)      The code uses the java.io.PrintStream(java.io.OutputStream)
>         constructor, which is giving me a deprecation warning
>         when I compile using IBM JDK 1.1.8.  It still works, but
>         I need to update the code.
> 2)      There is no facility to roll the log when you restart the
>         server.  This means any old logs will be blown away.  If
>         you want to keep old logs, I would suggest renaming the
>         old log first (like tomcatlog.1) in the init method before
>         opening up the file for logging.  I plan on implementing
>         this when I get around to it.
> 
> If anyone works on these issues, could you please post the code
> back to the list?
> 
> This should be enough to get you going.  Hope it helps.
> 
> Jeff
> 
> --
> 
> To use this class:
> 
> - compile and put it in your classpath (I am master of the obvious)
> - make sure Tomcat has write permissions to your log file (unless you're
>   running it as root, which is not advised in a production environment)
> - Add the following code snippet anywhere you need it (make sure you
>   import CustomLog into any class using it):
> 
>         CustomLog log = CustomLog.instance();
>         log.println("Logging to the custom log");
> 
> /*
>  *      CustomLog
>  *
>  *      Copyright 2000 Tone Network Inc.
>  *
>  *      Author:         Jeff Haynes <je...@techno.ca>
>  *      Written:        February 19, 2000
>  *      Version:        1.0
>  */
> 
> import java.io.*;
> import java.net.*;
> import java.util.*;
> 
> public class CustomLog {
> 
>         private static PrintStream log;
>         private static CustomLog _instance = null;
>          private static final String logFileString = "/var/log/tomcat.log";
> 
>         /*
>          * Construct the only CustomLog object.
>          */
>         private CustomLog()
>         {
>                 init();
>         }
> 
> 
>         /*
>          * Initializes the singleton by opening the log file for
>          * printing to it.
>          */
>         private void init()
>         {
>                  Date now = new java.util.Date();
> 
>                 try
>                 {
>                         log = new PrintStream(new
> FileOutputStream(logFileString));
>                         log.println(now.toString() + " Starting custom
> logging.");
>                         System.out.println(now.toString()
>                                 + " Opened up custom log: " + logFileString);
>                 }
>                 catch (IOException e)
>                 {
>                         System.out.println("Can't open custom log: "
>                                 + logFileString);
>                 }
>         }
> 
>        /**
>          * Returns the instance of this class.
>          */
>         public static CustomLog instance ()
>         {
>                 if (_instance == null)
>                         _instance = new CustomLog();
>                 return _instance;
>         }
> 
> 
>         /*
>          * Prints the passed string to the log file.
>          *
>          * @param s     string to print in the log
>          */
>         public static void println(String s)
>         {
>                 log.println(s);
>         }
> 
> }
> 
> At 03:24 PM 2/25/00 +0530, you wrote:
> >Hi all,
> >
> >    I'm using Tomcat 3.0 with Apache 1.3.9(jdk1.2.2). My question is it
> >possible to log errors that occurs in servlets  and JSPs to Apache's error
> >log file?
> >
> >Thanks,
> >Sudheer.
> >
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: general-unsubscribe@jakarta.apache.org
> >For additional commands, e-mail: general-help@jakarta.apache.org
> >
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: general-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: general-help@jakarta.apache.org

RE: Is it possible to log errors to server's log file in Tomcat 3.0?

Posted by caius van nouhuys <ca...@respondtv.com>.
I've written similar stuff, but I've also written an kludge to deal with
writing to the server's logs when I really needed to do that. I wrote a
class that makes a request to the server using HttpURLConnection, and the
server, obviously, will log that.

$#@---$#@@#$---$#@@#$---$#@@#$---@#$
Caius van Nouhuys
RespondTV -- 415 522 5260 x 207