You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Dave Boyer <db...@ecal.com> on 2000/05/08 19:29:05 UTC

Logging to File

I have a set of classes in my WEB-INF\classes directory
which I am using in my jsp pages.

I have a Logger class which I use to write logging data to
different outStreams.  One of my other classes creates an
instance of the Logger when it is initialized and adds the
log file "test.log" no path and writes a couple of strings to
the file.

When I create an instance of this class through tomcat
I can't find the file.  When I create an instance of this class
through a main function and write to it, it works fine.

I also tried setting the log file name to "c:\\test.log".  Again
commandline it's fine, in tomcat NOTHING.

Any thoughts?
Are there any implicit security constraints which would prevent
objects created in JSP pages from writing back to the server?
If so is there a way to turn off this behavior?




Re: Logging to File

Posted by Steve Weiss <sw...@aamc.org>.
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

Re: Logging to File

Posted by Dave Boyer <db...@ecal.com>.
Thanks alot Steve and Juan, that's exactly what I needed :)

David Boyer




Re: Logging to File

Posted by Juan Alvarez Ferrando <ja...@oviedo.syseca.es>.
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...
> 
> 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.
> 
> 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.
> 
> 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?

Hi,

I've built a JDBC connection pool I use in my applications, to
initialize
it I read configuration from a properties file from a static method in
one of my classes. To force execution of this code and read the
properties
file, and for cleanup on server shutdown, I use a servlet that's loaded
on startup (configure this at server.xml).

Juan Alvarez Ferrando

> 
> --------------------------------------------------------------------------
> To unsubscribe, email: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commmands, email: tomcat-user-help@jakarta.apache.org

Re: Logging to File

Posted by Dave Boyer <db...@ecal.com>.

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...

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.

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.

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?



Re: Logging to File

Posted by Steve Weiss <sw...@aamc.org>.
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).

-Steve

Dave Boyer wrote:
> 
> I have a set of classes in my WEB-INF\classes directory
> which I am using in my jsp pages.
> 
> I have a Logger class which I use to write logging data to
> different outStreams.  One of my other classes creates an
> instance of the Logger when it is initialized and adds the
> log file "test.log" no path and writes a couple of strings to
> the file.
> 
> When I create an instance of this class through tomcat
> I can't find the file.  When I create an instance of this class
> through a main function and write to it, it works fine.
> 
> I also tried setting the log file name to "c:\\test.log".  Again
> commandline it's fine, in tomcat NOTHING.
> 
> Any thoughts?
> Are there any implicit security constraints which would prevent
> objects created in JSP pages from writing back to the server?
> If so is there a way to turn off this behavior?
> 
> --------------------------------------------------------------------------
> To unsubscribe, email: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commmands, email: tomcat-user-help@jakarta.apache.org

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