You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jerry Malcolm <2n...@gmail.com> on 2011/12/02 03:21:06 UTC

Logging - including host name in log file?

I'm trying to get my hands around the whole tomcat logging system.  I've
read the docs, wikis, samples, etc.  But I still struggling a bit.  I've
been just using System.out.println for years, and it's getting totally out
of control.  So time to learn tomcat logging.

Basically, I host quite a few domains.  I'd like to separate the log files
per host (and possibly further subdivide by webapps).  I can't find any way
to specify the host as part of the logger file handler directory.  This may
be something intuitively obvious.  But I haven't found it yet.

If it can't be configured statically to plug the host name into the log
file name with a variable or something like ${catalina_home}, alternatively
is there a way to change the file name on the fly after getting an instance
of the java.utils.logging.Logger class?

Thanks.

Jerry

RE: Logging - including host name in log file?

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Jerry Malcolm [mailto:2ndgenfilms@gmail.com] 
> Subject: Re: Logging - including host name in log file?

> A context runs in a host.

That is a Tomcat-specific implementation mechanism, not something mandated by the servlet spec.  Consequently, there is nothing in the spec that would let you see a <Host> associated with a webapp.  You could use JMX from inside the webapp to poke through the Tomcat nodes and find the host name during the init() method.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Logging - including host name in log file?

Posted by Mark Eggers <it...@yahoo.com>.
>________________________________
> From: Jerry Malcolm <2n...@gmail.com>
>To: Tomcat Users List <us...@tomcat.apache.org> 
>Sent: Friday, December 2, 2011 6:45 PM
>Subject: Re: Logging - including host name in log file?
> 
>Thanks for all of your help.  I decided it was going to be difficult to
>maintain/update the static properties file for all of the different
>host/apps.  So I decided to pursue dynamic configuration at runtime (with
>success).  Each time I need a logger, I simply instantiate a tomcat
>FileHandler and build the file name from the catalina base, the server
>name, and the context.  This seems fairly straightforward, and again, it is
>working.  But if there is something in this implementation that's going to
>bite me, please let me know now.
>
>I've got it all working now, except for one specific situation... I have
>some servlets that run at startup.  I have googled and looked everywhere i
>can think of.... but I cannot figure out how to find the host name while
>inside a servlet init method.  In other situations, I pull the server name
>from the request object.  But when a servlet is running at startup, there
>is no request object.  I have a ServletConfig and can get the
>ServletContext.  But I can't find the host name in those.  A context runs
>in a host.  It seems to me that the context would report the host it is in,
>independent of whether a request is in progress of not.
>
>This is not a show-stopper on the overall logging transition for me.  But I
>would sure love to find a way to know the host that this context is running
>under without requiring a request object.
>
>Am I missing something obvious?
>
>Thanks again.
>
>Jerry
>
>On Fri, Dec 2, 2011 at 2:04 PM, Christopher Schultz <
>chris@christopherschultz.net> wrote:
>
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Jerry,
>>
>> On 12/2/11 12:46 PM, Jerry Malcolm wrote:
>> > Ok, I'm digging into this.  Slowly but surely.... Just to
>> > confirm...
>> >
>> > So if I have, say 2 hosts, each with 3 web apps, and I want a
>> > different log file for each web app on each host, I need to define
>> > 6 of the following:
>> >
>> > 10host1webapp1.org.apache.juli.FileHandler.level = FINE
>> > 10host1webapp1.org.apache.juli.FileHandler.directory =
>> > ${catalina.base}/logs
>> > 10host1webapp1.org.apache.juli.FileHandler.prefix = Host1App1.
>>
>> Yes, although the format of the property base doesn't need to be that
>> complex. You could do this:
>>
>> Host1App1.level=FINE
>> Host1App1.directory=...
>> Host1App1.prefix=Host1App1.
>>
>> Tomcat has those odd property names so they will be unlikely to
>> conflict with anything you might want to define yourself.
>>
>> The magic happens here, when you actually configure Tomcat's logger(s):
>>
>> >
>> > And then define 6 of the following:
>> >
>> >
>> org.apache.catalina.core.ContainerBase.[Catalina].[host1].[/webapp1].level
>> >
>> >
>> = INFO
>> >
>> org.apache.catalina.core.ContainerBase.[Catalina].[host1].[/webapp1].handlers
>> >
>> >
>> = 10host1webapp1.org.apache.juli.FileHandler
>>
>> You could instead do:
>>
>>
>> org.apache.catalina.core.ContainerBase.[Catalina].[host1].[/webapp1].handlers=Host1App1
>>
>> > and I put all of this in /conf/logging.properties
>>
>> Yes: this will tell Tomcat that, when your webapp uses
>> ServletContext.log(...) that the output should go to those loggers
>> listed in the "handlers" property for that particular webapp.
>>
>> > All of this is required in order to get separate log files per
>> > webapp per host, right?
>>
>> - From ServletContext.log(..), yes.
>>
>> Again, if your webapp is logging in some other way, then Tomcat's
>> configuration does not apply at all. It's fairly common for webapps to
>> use their own logging mechanism rather than logging to the servlet
>> container, so I wouldn't be surprised if you have more work to do.
>>
>> - -chris


Here's something that I came up with for application-level logging. This is based on Apache commons-logging and log4j.

Please note that I'm a systems person, and only write Java code when I need to figure out how things are working (or not working).

I start out with a ServletContextListener, and two pieces of configuration.

In each Tomcat virtual host ($CATALINA_BASE/conf/[hostname]), I create a context.xml.default file. In it, I place a simple resource to retrieve via JNDI.

The context.xml.default file is documented:

http://tomcat.apache.org/tomcat-6.0-doc/config/context.html


The resource documentation is:

http://tomcat.apache.org/tomcat-6.0-doc/config/context.html#Environment Entries


So for example, a context.xml.default file can look like:

<?xml version="1.0" encoding="UTF-8"?>

<Context>
  <WatchedResource>WEB-INF/web.xml</WatchedResource>

  <Environment name="hostname" value="avirtualhost" type="java.lang.String"/>
</Context>

Now, in my application, I set up a base file name in log4j (log4j.xml) for my file appender. I use the Tomcat-defined variables, so the file name ends up looking like:

${catalina.base}/logs/applog.log

Finally, in the ServletContextListener, I do something like the following:
private Log log = LogFactory.getLog(this.getClass());


public void contextInitialized(ServletContextEvent sce) {
StringBuilder jndiValue = new StringBuilder();
StringBuilder nfname = new StringBuilder(128);

// get current log file name - assume that we're working with a
// FileAppender - maybe should check?
                // Hard coding names for now - not really a good idea
Logger wplog = Logger.getLogger("wplog");
FileAppender fa = (FileAppender) wplog.getAppender("FA");
String ofname = fa.getFile();

// get JNDI name - using a resource in context.xml.default
// see the Tomcat documentation for resources
try {
Context ictx = new InitialContext();
Context envctx = (Context) ictx.lookup("java:comp/env");
jndiValue.append((String) envctx.lookup("hostname"));
jndiValue.append("-");
jndiValue.trimToSize();
} catch (NamingException ex) {
ex.printStackTrace(); // ugly
}

// default host if things are horribly broken
if (jndiValue.length() == 0) {
jndiValue.append("foohost-");
jndiValue.trimToSize();
}

// insert the JNDI value plus a - before the file name portion of ofname
nfname.append(ofname);
                // should probably determine system separator here
int lastSep = nfname.lastIndexOf("/");
nfname.insert(lastSep + 1, jndiValue.toString());

// set the new name and activate
fa.setFile(nfname.toString());
fa.activateOptions();

// log in new place
log.info("Logging initialized and reset to new file name");
}

Obviously you can decouple names (a good thing) by using <context-param> elements in web.xml, go through all of the loggers and appenders (checking if the appenders are actually FileAppenders by using instanceof), and in general making this more production-worthy.

This ends up creating a zero-length file with the default name, and then all other logging goes to the new file.

The end result is that you can have multiple copies of the same web application hosted on different virtual hosts inside one CATALINA_BASE (or CATALINA_HOME) all writing to different log files, and all without modifying your source code.

Hope this isn't too ugly.

just my two cents . . . .

/mde/

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Logging - including host name in log file?

Posted by Jerry Malcolm <2n...@gmail.com>.
Thanks for all of your help.  I decided it was going to be difficult to
maintain/update the static properties file for all of the different
host/apps.  So I decided to pursue dynamic configuration at runtime (with
success).  Each time I need a logger, I simply instantiate a tomcat
FileHandler and build the file name from the catalina base, the server
name, and the context.  This seems fairly straightforward, and again, it is
working.  But if there is something in this implementation that's going to
bite me, please let me know now.

I've got it all working now, except for one specific situation... I have
some servlets that run at startup.  I have googled and looked everywhere i
can think of.... but I cannot figure out how to find the host name while
inside a servlet init method.  In other situations, I pull the server name
from the request object.  But when a servlet is running at startup, there
is no request object.  I have a ServletConfig and can get the
ServletContext.  But I can't find the host name in those.  A context runs
in a host.  It seems to me that the context would report the host it is in,
independent of whether a request is in progress of not.

This is not a show-stopper on the overall logging transition for me.  But I
would sure love to find a way to know the host that this context is running
under without requiring a request object.

Am I missing something obvious?

Thanks again.

Jerry

On Fri, Dec 2, 2011 at 2:04 PM, Christopher Schultz <
chris@christopherschultz.net> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Jerry,
>
> On 12/2/11 12:46 PM, Jerry Malcolm wrote:
> > Ok, I'm digging into this.  Slowly but surely.... Just to
> > confirm...
> >
> > So if I have, say 2 hosts, each with 3 web apps, and I want a
> > different log file for each web app on each host, I need to define
> > 6 of the following:
> >
> > 10host1webapp1.org.apache.juli.FileHandler.level = FINE
> > 10host1webapp1.org.apache.juli.FileHandler.directory =
> > ${catalina.base}/logs
> > 10host1webapp1.org.apache.juli.FileHandler.prefix = Host1App1.
>
> Yes, although the format of the property base doesn't need to be that
> complex. You could do this:
>
> Host1App1.level=FINE
> Host1App1.directory=...
> Host1App1.prefix=Host1App1.
>
> Tomcat has those odd property names so they will be unlikely to
> conflict with anything you might want to define yourself.
>
> The magic happens here, when you actually configure Tomcat's logger(s):
>
> >
> > And then define 6 of the following:
> >
> >
> org.apache.catalina.core.ContainerBase.[Catalina].[host1].[/webapp1].level
> >
> >
> = INFO
> >
> org.apache.catalina.core.ContainerBase.[Catalina].[host1].[/webapp1].handlers
> >
> >
> = 10host1webapp1.org.apache.juli.FileHandler
>
> You could instead do:
>
>
> org.apache.catalina.core.ContainerBase.[Catalina].[host1].[/webapp1].handlers=Host1App1
>
> > and I put all of this in /conf/logging.properties
>
> Yes: this will tell Tomcat that, when your webapp uses
> ServletContext.log(...) that the output should go to those loggers
> listed in the "handlers" property for that particular webapp.
>
> > All of this is required in order to get separate log files per
> > webapp per host, right?
>
> - From ServletContext.log(..), yes.
>
> Again, if your webapp is logging in some other way, then Tomcat's
> configuration does not apply at all. It's fairly common for webapps to
> use their own logging mechanism rather than logging to the servlet
> container, so I wouldn't be surprised if you have more work to do.
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
> Comment: GPGTools - http://gpgtools.org
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAk7ZL1QACgkQ9CaO5/Lv0PCOtACgrrhT1KS0QjOOF+Swh+jGqfZS
> bGYAn3sg6wcPwg9HtLrXDcHkMFxXIz3W
> =LSuQ
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: Logging - including host name in log file?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jerry,

On 12/2/11 12:46 PM, Jerry Malcolm wrote:
> Ok, I'm digging into this.  Slowly but surely.... Just to
> confirm...
> 
> So if I have, say 2 hosts, each with 3 web apps, and I want a
> different log file for each web app on each host, I need to define
> 6 of the following:
> 
> 10host1webapp1.org.apache.juli.FileHandler.level = FINE 
> 10host1webapp1.org.apache.juli.FileHandler.directory =
> ${catalina.base}/logs 
> 10host1webapp1.org.apache.juli.FileHandler.prefix = Host1App1.

Yes, although the format of the property base doesn't need to be that
complex. You could do this:

Host1App1.level=FINE
Host1App1.directory=...
Host1App1.prefix=Host1App1.

Tomcat has those odd property names so they will be unlikely to
conflict with anything you might want to define yourself.

The magic happens here, when you actually configure Tomcat's logger(s):

> 
> And then define 6 of the following:
> 
> org.apache.catalina.core.ContainerBase.[Catalina].[host1].[/webapp1].level
>
> 
= INFO
> org.apache.catalina.core.ContainerBase.[Catalina].[host1].[/webapp1].handlers
>
> 
= 10host1webapp1.org.apache.juli.FileHandler

You could instead do:

org.apache.catalina.core.ContainerBase.[Catalina].[host1].[/webapp1].handlers=Host1App1

> and I put all of this in /conf/logging.properties

Yes: this will tell Tomcat that, when your webapp uses
ServletContext.log(...) that the output should go to those loggers
listed in the "handlers" property for that particular webapp.

> All of this is required in order to get separate log files per
> webapp per host, right?

- From ServletContext.log(..), yes.

Again, if your webapp is logging in some other way, then Tomcat's
configuration does not apply at all. It's fairly common for webapps to
use their own logging mechanism rather than logging to the servlet
container, so I wouldn't be surprised if you have more work to do.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk7ZL1QACgkQ9CaO5/Lv0PCOtACgrrhT1KS0QjOOF+Swh+jGqfZS
bGYAn3sg6wcPwg9HtLrXDcHkMFxXIz3W
=LSuQ
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Logging - including host name in log file?

Posted by Jerry Malcolm <2n...@gmail.com>.
Ok, I'm digging into this.  Slowly but surely.... Just to confirm...

So if I have, say 2 hosts, each with 3 web apps, and I want a different log
file for each web app on each host, I need to define 6 of the following:

10host1webapp1.org.apache.juli.FileHandler.level = FINE
10host1webapp1.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
10host1webapp1.org.apache.juli.FileHandler.prefix = Host1App1.

.... through

15host2webapp3.org.apache.juli.FileHandler.level = FINE
15host2webapp3.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
15host2webapp3.org.apache.juli.FileHandler.prefix = Host2App3.

And then define 6 of the following:

org.apache.catalina.core.ContainerBase.[Catalina].[host1].[/webapp1].level
= INFO
org.apache.catalina.core.ContainerBase.[Catalina].[host1].[/webapp1].handlers
= 10host1webapp1.org.apache.juli.FileHandler

...through

org.apache.catalina.core.ContainerBase.[Catalina].[host2].[/webapp3].level
= INFO
org.apache.catalina.core.ContainerBase.[Catalina].[host2].[/webapp3].handlers
= 15host2webapp3.org.apache.juli.FileHandler

and I put all of this in /conf/logging.properties

All of this is required in order to get separate log files per webapp per
host, right?

Thanks.

Jerry

On Thu, Dec 1, 2011 at 8:50 PM, Christopher Schultz <
chris@christopherschultz.net> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Jerry,
>
> On 12/1/11 9:21 PM, Jerry Malcolm wrote:
> > I'm trying to get my hands around the whole tomcat logging system.
> > I've read the docs, wikis, samples, etc.  But I still struggling a
> > bit.  I've been just using System.out.println for years, and it's
> > getting totally out of control.  So time to learn tomcat logging.
>
> Yup. System.out is insanely inflexible.
>
> > Basically, I host quite a few domains.  I'd like to separate the
> > log files per host (and possibly further subdivide by webapps).  I
> > can't find any way to specify the host as part of the logger file
> > handler directory.  This may be something intuitively obvious.  But
> > I haven't found it yet.
>
> Which logging system are you using? Tomcat's default is to use JULI
> which connects commons-logging up to the java.util.logging (or J-U-L
> Interface, hence "JULI").
>
> At any rate, the standard logging.properties file should have examples
> that make this work. For instance, I can see the following in mine:
>
>
> org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level
> = INFO
>
> org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers
> = 3manager.org.apache.juli.FileHandler
>
> All that mess configures a logger that captures the logs for the
> "manager" <Context> that is deployed into the "localhost" <Host> under
> the "Catalina" <Service>.
>
> You can easily create one of these for each of your webapps (or even
> just per host) in the same way: just use the proper Service, Host, and
> context path (contexts don't have names, so you use the context path
> instead -- the above for an example).
>
> So, let's say that you have:
>
> <Service name="Catalina">
>  <Host name="www.awesomehost.com">
>    <Context path="/sweetwebapp" docBase="..." ... />
>  </Host>
> </Service>
>
> (But, of course, you don't have that because you shouldn't put
> <Context>s in server.xml, but I have it here for the sake of brevity).
>
> Anyhow, you can configure a logger for that context like this:
>
> org.apache.catalina.core.ContainerBase.[Catalina].[www.awesomehost.com
> ].[/sweetwebapp].level=INFO
>
> (plus the other configuration you'll need like which file to use, etc.)
>
> > If it can't be configured statically to plug the host name into the
> > log file name with a variable or something like ${catalina_home},
> > alternatively is there a way to change the file name on the fly
> > after getting an instance of the java.utils.logging.Logger class?
>
> If you are using Tomcat's internal logging (which is done by calling
> ServletContext.log(...)) then you should use lib/logging.properties as
> described above.
>
> If you are using java.util.logging directly in your own webapp, then
> you are on your own :(
>
> If you are using AccessLogValve, well then you just need to use "%v"
> to get the name of the local server -- but that's for the actual log
> data, not for the filename.
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
> Comment: GPGTools - http://gpgtools.org
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAk7YPN4ACgkQ9CaO5/Lv0PA0ZgCgq2ckmo/fw88FbeV0UhOVuYTm
> 7uwAn1D/sE+YHVw3juxVWFVZTdMMey6T
> =0R0X
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: Logging - including host name in log file?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jerry,

On 12/1/11 11:34 PM, Jerry Malcolm wrote:
> Where should the properties file be located?  All of the docs say
> it should be in /common/classes.  But in my install, it's in
> /conf.

Something conspicuously missing in your original post was what version
of Tomcat you are using. If you are reading docs that mention
/common/classes, I suspect you're working with something less than
Tomcat 6.x. If that's true, are you able to upgrade? Tomcat 6 and 7
both have significant improvements and should be backward-compatible.

You can work with an older version of Tomcat, but 5.0 EOL'd a long
time ago and I think 5.5's days are numbered. Tomcat 7 is where the
real action is these days.

> At the risk of opening another bag of worms.... in your example,
> you said: (But, of course, you don't have that because you
> shouldn't put <Context>s in server.xml, but I have it here for the
> sake of brevity).  Ok, I DO have context statements in my
> server.xml. I'm obviously not following best practices.... but it's
> worked fine for years. Can you point me to documentation that
> explains the negatives of doing it the way I'm doing it vs. the
> best practices way, and what the best practices way to do that is?

If you are using Tomcat 5.5 or later (which I hope you are), the best
thing to do is to use a file in your webapp's META-INF/context.xml --
that should contain your <Context> element and should contain neither
a "docBase" (because the file was loaded out of the webapp's docBase,
so Tomcat already knows where it is) nor a "path" (because the name of
the WAR file or exploded-WAR-structure already dictates the context path).

If you have an odd configuration where you want your WAR file
somewhere other than the webapps/ auto-deploy directory for your host,
then you can use a file in Tomcat's
conf/[Service]/[Host]/[webappname].xml, and you'll have to specify the
docBase attribute and point it at your webapp's WAR file (or
exploded-WAR-structure).

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk7Y8qgACgkQ9CaO5/Lv0PBuJACdE+kgpshbQyuBcPwnq0rgtkz+
pcUAn2I2xFL7U6kfjnQ8lsdAfHGtjQx3
=2tmu
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Logging - including host name in log file?

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Jerry Malcolm [mailto:2ndgenfilms@gmail.com] 
> Subject: Re: Logging - including host name in log file?

> Where should the properties file be located?  All of the 
> docs say it should be in /common/classes.

What docs are those?  Please be specific, because they're wrong and need correcting.

> I DO have context statements in my server.xml.

Bad practice - even in Tomcat 5.0, predating your version.

Look at the doc for the <Context> element; note that the 7.0 doc is much improved over the ancient version you have, although obviously some of it is not applicable to 5.5.

http://tomcat.apache.org/tomcat-7.0-doc/config/context.html

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Logging - including host name in log file?

Posted by Jerry Malcolm <2n...@gmail.com>.
Thanks so much, Chris.  Yes, I did see those lines in the properties file.
But it didn't have a lot of explanation.  Thanks for the clarification.

Where should the properties file be located?  All of the docs say it should
be in /common/classes.  But in my install, it's in /conf.

At the risk of opening another bag of worms.... in your example, you said:
(But, of course, you don't have that because you shouldn't put <Context>s
in server.xml, but I have it here for the sake of brevity).  Ok, I DO have
context statements in my server.xml. I'm obviously not following best
practices.... but it's worked fine for years. Can you point me to
documentation that explains the negatives of doing it the way I'm doing it
vs. the best practices way, and what the best practices way to do that is?

Thanks again.

Jerry

On Thu, Dec 1, 2011 at 8:50 PM, Christopher Schultz <
chris@christopherschultz.net> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Jerry,
>
> On 12/1/11 9:21 PM, Jerry Malcolm wrote:
> > I'm trying to get my hands around the whole tomcat logging system.
> > I've read the docs, wikis, samples, etc.  But I still struggling a
> > bit.  I've been just using System.out.println for years, and it's
> > getting totally out of control.  So time to learn tomcat logging.
>
> Yup. System.out is insanely inflexible.
>
> > Basically, I host quite a few domains.  I'd like to separate the
> > log files per host (and possibly further subdivide by webapps).  I
> > can't find any way to specify the host as part of the logger file
> > handler directory.  This may be something intuitively obvious.  But
> > I haven't found it yet.
>
> Which logging system are you using? Tomcat's default is to use JULI
> which connects commons-logging up to the java.util.logging (or J-U-L
> Interface, hence "JULI").
>
> At any rate, the standard logging.properties file should have examples
> that make this work. For instance, I can see the following in mine:
>
>
> org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level
> = INFO
>
> org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers
> = 3manager.org.apache.juli.FileHandler
>
> All that mess configures a logger that captures the logs for the
> "manager" <Context> that is deployed into the "localhost" <Host> under
> the "Catalina" <Service>.
>
> You can easily create one of these for each of your webapps (or even
> just per host) in the same way: just use the proper Service, Host, and
> context path (contexts don't have names, so you use the context path
> instead -- the above for an example).
>
> So, let's say that you have:
>
> <Service name="Catalina">
>  <Host name="www.awesomehost.com">
>    <Context path="/sweetwebapp" docBase="..." ... />
>  </Host>
> </Service>
>
> (But, of course, you don't have that because you shouldn't put
> <Context>s in server.xml, but I have it here for the sake of brevity).
>
> Anyhow, you can configure a logger for that context like this:
>
> org.apache.catalina.core.ContainerBase.[Catalina].[www.awesomehost.com
> ].[/sweetwebapp].level=INFO
>
> (plus the other configuration you'll need like which file to use, etc.)
>
> > If it can't be configured statically to plug the host name into the
> > log file name with a variable or something like ${catalina_home},
> > alternatively is there a way to change the file name on the fly
> > after getting an instance of the java.utils.logging.Logger class?
>
> If you are using Tomcat's internal logging (which is done by calling
> ServletContext.log(...)) then you should use lib/logging.properties as
> described above.
>
> If you are using java.util.logging directly in your own webapp, then
> you are on your own :(
>
> If you are using AccessLogValve, well then you just need to use "%v"
> to get the name of the local server -- but that's for the actual log
> data, not for the filename.
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
> Comment: GPGTools - http://gpgtools.org
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAk7YPN4ACgkQ9CaO5/Lv0PA0ZgCgq2ckmo/fw88FbeV0UhOVuYTm
> 7uwAn1D/sE+YHVw3juxVWFVZTdMMey6T
> =0R0X
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: Logging - including host name in log file?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jerry,

On 12/1/11 9:21 PM, Jerry Malcolm wrote:
> I'm trying to get my hands around the whole tomcat logging system.
> I've read the docs, wikis, samples, etc.  But I still struggling a
> bit.  I've been just using System.out.println for years, and it's
> getting totally out of control.  So time to learn tomcat logging.

Yup. System.out is insanely inflexible.

> Basically, I host quite a few domains.  I'd like to separate the
> log files per host (and possibly further subdivide by webapps).  I
> can't find any way to specify the host as part of the logger file
> handler directory.  This may be something intuitively obvious.  But
> I haven't found it yet.

Which logging system are you using? Tomcat's default is to use JULI
which connects commons-logging up to the java.util.logging (or J-U-L
Interface, hence "JULI").

At any rate, the standard logging.properties file should have examples
that make this work. For instance, I can see the following in mine:

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level
= INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers
= 3manager.org.apache.juli.FileHandler

All that mess configures a logger that captures the logs for the
"manager" <Context> that is deployed into the "localhost" <Host> under
the "Catalina" <Service>.

You can easily create one of these for each of your webapps (or even
just per host) in the same way: just use the proper Service, Host, and
context path (contexts don't have names, so you use the context path
instead -- the above for an example).

So, let's say that you have:

<Service name="Catalina">
  <Host name="www.awesomehost.com">
    <Context path="/sweetwebapp" docBase="..." ... />
  </Host>
</Service>

(But, of course, you don't have that because you shouldn't put
<Context>s in server.xml, but I have it here for the sake of brevity).

Anyhow, you can configure a logger for that context like this:

org.apache.catalina.core.ContainerBase.[Catalina].[www.awesomehost.com].[/sweetwebapp].level=INFO

(plus the other configuration you'll need like which file to use, etc.)

> If it can't be configured statically to plug the host name into the
> log file name with a variable or something like ${catalina_home},
> alternatively is there a way to change the file name on the fly
> after getting an instance of the java.utils.logging.Logger class?

If you are using Tomcat's internal logging (which is done by calling
ServletContext.log(...)) then you should use lib/logging.properties as
described above.

If you are using java.util.logging directly in your own webapp, then
you are on your own :(

If you are using AccessLogValve, well then you just need to use "%v"
to get the name of the local server -- but that's for the actual log
data, not for the filename.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk7YPN4ACgkQ9CaO5/Lv0PA0ZgCgq2ckmo/fw88FbeV0UhOVuYTm
7uwAn1D/sE+YHVw3juxVWFVZTdMMey6T
=0R0X
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org