开发者

how to have separate tomcat logs for a single war

开发者 https://www.devze.com 2023-04-12 22:01 出处:网络
I am using a single war which is used in different way depending on the resources (ie. the URL by which it is called).

I am using a single war which is used in different way depending on the resources (ie. the URL by which it is called).

The resources are accessed from outside the webapps.

I need to maintain separate logs so that all the logs can be maintained separately.So i need to have a folder logs like the resources fo开发者_运维百科lder outside the webapps for this.


Okay there is a way to do this using log4j. I'm not saying this is the best way, but this method should work. Here's a starting point.

Download log4j.jar and put it in your WEB-INF/lib

Have a log4j.properties in WEB-INF/classes. It will look like this

log4j.rootLogger=DEBUG, console

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{MM-dd@HH:mm:ss} %-5p %c{1}]: %m%n


log4j.logger.myoldsitelogger=DEBUG,myoldsite

# myoldsite writes to file
log4j.appender.myoldsite=org.apache.log4j.RollingFileAppender
log4j.appender.myoldsite.File=${catalina.base}/logs/tomcatlog4j.log
log4j.appender.myoldsite.MaxFileSize=100KB
log4j.appender.myoldsite.layout=org.apache.log4j.PatternLayout
log4j.appender.myoldsite.layout.ConversionPattern=%d{MM-dd@HH:mm:ss} %-5p %c{1}]: %m%n

log4j.logger.mynewsitelogger=DEBUG,mynewsite


# mynewsite writes to file
log4j.appender.mynewsite=org.apache.log4j.RollingFileAppender
log4j.appender.mynewsite.File=${catalina.base}/logs/tomcatlog4j_newsite.log
log4j.appender.mynewsite.MaxFileSize=100KB
log4j.appender.mynewsite.layout=org.apache.log4j.PatternLayout
log4j.appender.mynewsite.layout.ConversionPattern=%d{MM-dd@HH:mm:ss} %-5p %c{1}]: %m%n

Now from your app, get a handle to the right logger

Logger logger = Logger.getLogger("myoldsitelogger");
Logger logger2 = Logger.getLogger("mynewsitelogger");

if(request.getParameter("oldsite") != null)
    logger.info("In my.jsp from oldsite");
else
    logger2.info("In my.jsp from newsite");

In your logs folder, you should have the 2 log files named tomcatlog4j.log and tomcatlog4j_newsite.log with the right messages.

There could be a way to do this in Tomcat even without log4j, but I dont have a working sample yet

0

精彩评论

暂无评论...
验证码 换一张
取 消