tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Steve Weiss <swe...@aamc.org>
Subject Re: Logging to File
Date Mon, 08 May 2000 22:02:10 GMT
Dave Boyer wrote:
> 
> Steve Weiss wrote:
> 
> > I'm doing something very similar, I put this in my application's
> > web.xml:
> >
> >     <context-param>
> >         <param-name>SYSLOG_FILE</param-name>
> >
> > <param-value>/home/steve/dev/java/webapps/currmit/logs/web.log</param-value>
> >     </context-param>
> >
> > Then I read in the SYSLOG_FILE parameter when my servlet is initialized
> > and open the file. Works fine for me (on Linux).
> >
> 
> When you say you read in the SYSLOG_FILE parameter, what exactly do you mean?
> 
> Class and function preferrably and where you read and process this parameter...
> 

You could do it anywhere, I created an abstract servlet base and put
code more or less like the following in the init() method:

        ServletContext _context =
getServletConfig().getServletContext();

        Properties _properties = new java.util.Properties();  //
Properties object is a static global

        Enumeration e = context.getInitParameterNames();

        while( e.hasMoreElements() ) {
            String name = (String)e.nextElement();
            System.out.println( "name = " + name + ", value = " +
                                context.getInitParameter( name ) );
            _properties.put( name, context.getInitParameter( name ) );
        }

        // Create our LogManager (a singleton)
        _logMgr = LogManager.getLogManager( _properties );

In constructor for LogManager.java:

   String syslog_filename = props.getProperty( "SYSLOG_FILE" ).trim();

Then use "syslog_filename" to create the log file. The file can be
anywhere on the system where the user running Tomcat has write
priveledges. Since I'm that user (for development) I can create the file
anywhere in my home directory. The file does *not* have to be in the
same directory heirarchy as either the Tomcat installation
(/usr/local/jakarta/tomcat in my case) or my web application. For
example, my web application is in /home/steve/dev/java/webapps/currmit,
and I can put the following in web.xml:

    <context-param>
        <param-name>SYSLOG_FILE</param-name>
        <param-value>/home/steve/currmit/logs/web.log</param-value>
    </context-param>

and it works fine.

> Am I correct that the tomcat engine limits the access of a servlet to the system
> it's
> located on.  In other words, the classes are loaded within a sandbox of some
> type
> which limits the access of those files to system resources?
> So, I can't write files all over the System the servlet runs on.
> 

As far as I can see based on the above, the only "sandbox" in effect
here is the OS's filesystem permissions.

> Am I correct that all JSP pages and all servlets within my web-app directory
> have access to the same ServletContext instance? If so this is the place to put
> my Logger.
> 
Yes, this is what the <context-param></context-param> does. You can have
as many of these as you want to set up global initialization parameters
for your servlets/JSPs.

> I would like to initialize some Application wide objects the first time any
> Servlet or JSP page is loaded.  I would like to create one instance of the
> logger, and a connection pool for interacting with an XML datasource.  Is there
> a way to do this without placing code in every single Servlet and JSP page to
> ensure that initialization has taken place?
> 

That's exactly what I am doing. I have a servlet that gets loaded when
Tomcat starts, that servlet will read in the configuration info from
web.xml, and create various static global objects, including my
LogManager. In a different servlet/JSP, I have to do is: LogManager
logMgr = LogManager.getLogManager(). The LogManager class is a
singleton, it's constructor is private so it can't be instantiated by
any other classes. Classes must call the static method getLogManager(),
which returns a new LogManager (if it's the first time being called) or
the existing LogManager if it exists:

  public class LogManager {
	private static LogManager _logMagr = null;

        //... and so on...

        // the Properties object is passed in from the servlet...
        static LogManager getLogManager( Properties props ) {
	    if ( _logMgr == null ) {
               _logManager = new LogManager( props ); 
            }
            return _logMgr;
        }
   }

And that's about it.

-Steve

-- 
Steve Weiss      Association of American Medical Colleges
(202)828-0428    mailto:sweiss@aamc.org    http://www.aamc.org

Mime
View raw message