Question

log4j - XML config logging for my package

Asked by: rstaveley

My package is com.mycompany.mypackage - i.e. classes are com.mycompany.MyClass etc. I'd like to log INFO from my package in a dedicated log file, using log4j. I need to use the XML configuration for reasons beyond the scope of this question.

I thought I could set it up thus:

--------8<--------
<?xml version="1.0" encoding= "UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

      <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
            <layout class="org.apache.log4j.PatternLayout">
                  <param name="ConversionPattern" value="%d %p [%c] - &lt;%m&gt;%n"/>
            </layout>
      </appender>

      <appender name="logfile" class="org.apache.log4j.RollingFileAppender">

            <param name="Threshold" value="DEBUG"/>
            <param name="Append" value="true"/>
            <param name="MaxFileSize" value="512KB"/>
            <param name="MaxBackupIndex" value="5"/>
            <param name="ImmediateFlush" value="true"/>

            <!-- file parameter is taken from the XML fragment, which is environment-specific -->
            <param name="File" value="mypackage.log"/>

            <layout class="org.apache.log4j.PatternLayout">
                  <param name="ConversionPattern" value="%d %p [%c] - %m%n"/>
            </layout>
      </appender>

      <logger name="com.mycompany.mypackage">
            <level value="info"/>
            <appender-ref ref="logfile"/>
            <appender-ref ref="stdout"/>
      </logger>

      <root>
            <priority value="warn"/>
            <appender-ref ref="stdout"/>
      </root>

</log4j:configuration>
--------8<--------

However, I don't get enything going into my log file. It remains 0 bytes.

Here's how I initialise the logger in my class:
--------8<--------
package com.mycompany.mypackage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

pubic class MyClass {

    static final Log log = LogFactory.getLog(MyClass.class);

    // ....
}
--------8<--------

What am I doing wrong?

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2007-08-25 at 03:38:06ID22786594
Tags

log4j

,

xml

Topics

Jakarta Struts

,

Java Programming Language

,

J2EE

Participating Experts
2
Points
500
Comments
28

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Log4j fileAppender config through xml problem
    Hi experts, I have just started using log4j and have ConsoleAppender working through an xml config file but can not get a FileAppender to work. The code is as follows: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM &...
  2. xmlns
    Hi All Ive got an xml transformation into xhtml which starts.... <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="org.apache.xalan.xslt.ex...
  3. Tomcat Log4J
    Hello I have a couple of questions regarding Tomcat and Log4J ( I am completely new to this ) ...I have been asked to provde for Log4J support for a web application that uses TomCat and Struts I am looking for ansers for the following questions .. 1.What is Logj4 used for ?...
  4. log4j for logging
    I wrote a simple java program running in IBM RSA to try out log4j logging. It works for me if it gets the configuration from the log4j.properties file. But if I change it to log4j.xml, I keep getting log4j:WARN No appenders could be found for logger log4j:WARN Please initia...
  5. Webapps and Log4j
    Hello I am trying to incorporate Log4J in my web application using JSF.Ofcourse I would want only the Java Comments to be logged .I was wondering 1.Where I should Place my properties file . 2.where will the outputs be logged 3.How does the web app reference the poperties fil...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: marklorenzPosted on 2007-08-25 at 08:08:49ID: 19768125

Use Logger.getLogger( MyClass), as explained here:

Each class in the Java application being logged can have an individual logger assigned to it or share a common logger with other classes. One can create any number of loggers for the application to suit specific logging needs. It is a common practice to create one logger for each class, with a name same as the fully-qualified class name. This practice helps organize log outputs in groups by the classes they originate from, and identify origin of log output, which is useful for debugging.

Log4j provides a default root logger that all user-defined loggers inherit from. Root logger is at the top of the logger hierarchy; in other words, root logger is either parent or ancestor of all logger objects created. If an application class doesn't have a logger assigned to it, it can still be logged using the root logger.

    For example: A class MyClass in com.foo.sampleapp application package can have a logger named com.foo.sampleapp.MyClass instantiated in it by using the Logger.getLogger("com.foo.sampleapp.MyClass") method. This logger will implicitly inherit from its nearest existing ancestor (maybe com.foo.sampleapp or com.foo or ...; or root logger if none exists), follow the same parent-child relationship as the class-subclass they log and have same package hierarchy as these classes.

http://www.developer.com/open/article.php/3097221

 

by: jim_cakalicPosted on 2007-08-27 at 10:00:56ID: 19776731

On first blush, the configuration looks right. You might try adding -Dlog4j.debug=true to your startup and see if anything in the configuration debugging indicates a problem.

What's the environment for this? Is it a standalong Java application or are you running in an application server (Tomcat, JBoss, etc.)?

A silly question, but you are actually logging INFO level messages somewhere, right? Also, have you tried removing commons-logging from the mix and, for test purposes, use the log4j API directly?

 

by: rstaveleyPosted on 2007-08-28 at 00:46:52ID: 19781024

This is running in a JBoss server, which had its own logging going to one server-wide logging file. To get my application to go into its own log, I took a cue from the only other application running on that server which is doing likewise and put its own log4j jar into WEB-INF/lib. The other application is using a log4j.properties file, though, which I wanted to avoid.

Yes, there are some log.info("xxx") messages in there. [Not a silly question, I've made sillier mistakes :-) ]

> -Dlog4j.debug=true

I can't get the SysAdmin to alter system properties, because they impact on all applications. I need to be a good citizen. But because that other application is getting its logging to work with log4j, I'm guessing that's set.

> have you tried removing commons-logging

Oh... it hadn't occurred to me to do that. I've been using commons-logging unthinkingly! I'll bet that's it.

I'll try the following and get back to you:
--------8<--------
package com.mycompany.mypackage;

import org.apache.log4j.Logger;

pubic class MyClass {

    private static org.apache.log4j.Logger log = Logger.getLogger(MyClass.class);

    // ....
}
--------8<--------

 

by: rstaveleyPosted on 2007-08-28 at 05:42:17ID: 19782329

Sorry, I didn't respond to your comment, marklorenz. I didn't see the e-mail notification from EE and didn't spot it when I read jim_cakalic's post.

In any case I've tried your suggestion...

--------8<--------
package com.mycompany.mypackage;

import org.apache.log4j.Logger;

pubic class MyClass {

    private static org.apache.log4j.Logger log = Logger.getLogger(MyClass.class);

    // ....
}
--------8<--------

...but no cigar. I don't get my messages logged.

Somehow com.mycompany.mypackage.MyClass, doesn't seem to inherit from com.mycompany.mypackage.

 

by: jim_cakalicPosted on 2007-08-28 at 05:55:23ID: 19782424

The log4j.debug system property is probably NOT set. That's something that you do only when you're having problems -- like this  ;-)

I know JBoss uses log4j for its own logging ... are you changing the server's conf/log4j.xml to add your configuration or are you using a separate log4j.xml file?

If separate file, where is it? I think it needs to be in WEB-INF/classes and log4j.jar in WEB-INF/lib. Make sure the classloader delegation policy is child-first or parent-last or some such so that the web app classloader will look to resolve class references before asking its parent.

If you're using the conf/log4j.xml, maybe having the log4j.jar in your WEB-INF/lib is causing a problem. Could be that the configuration is happening but your web app can't see it because a fresh unconfigured jar is being loaded by the web app classloader. If you /are/ using the conf/log4j.xml, try removing the log4j.jar from you war.

 

by: rstaveleyPosted on 2007-08-28 at 06:03:49ID: 19782498

It is separate and in WEB-INF/classes. I am getting the 0 byte file being created, which means it must be getting seen to some extent.

> Make sure the classloader delegation policy is child-first or parent-last...

That's not easy to organise, because I don't have sysadmin access

 

by: jim_cakalicPosted on 2007-08-28 at 06:19:59ID: 19782660

See http://docs.jboss.org/process-guide/en/html/logging.html, particularly sections 10.3.6 - 10.3.8.

In some of the browsing I've done on this topic, there are references to not being able to add a second console logger. Maybe you could try removing the console logger from your application-specific configuration and see if that makes any difference?

 

by: rstaveleyPosted on 2007-08-28 at 07:02:11ID: 19783069

That's a great link, Jim. I'll have a go at setting up a RepositorySelector and get back.

 

by: rstaveleyPosted on 2007-08-29 at 02:23:10ID: 19789712

I'm struggling implementing that RepositorySelector. Everything I do seems to prevent the .war from being able to deploy right now.

It's probably a consequence of something in the corporate development environment (clever plugins in Eclipse), I'm failing to understand. I'll get back to you, when I've got it to a useful point (or resolution!).

 

by: jim_cakalicPosted on 2007-08-29 at 06:35:51ID: 19791032

OK. I haven't dealt much with RepositorySelectors. Just a question ... the 0-byte file that is being created ... is it definitely re-created each time you start your application? If you remove the file will it appear again when JBoss is restarted? I ask only because I want to make sure that your log4j.xml configuration is /really/ being read. If so, maybe we could get the debugging we want by adding a load-on-startup servlet that first sets the log4j.debug System property to true and then gets a Logger and logs messages at several different levels. That should bootstrap the local (web app) configuration process if it is happening. Then we can check to see if the file exists and if it has content. We should be able to see log4j output in the console log.

 

by: rstaveleyPosted on 2007-08-29 at 10:24:46ID: 19793371

> Just a question ... the 0-byte file that is being created ... is it definitely re-created each time you start your application?

Good catch. It isn't. I couldn't unlink it, because of permissions (not being a sysadmin), but I didn't create a file with a new name.

I tried hacking a repository selector from http://docs.jboss.org/process-guide/en/html/logging.html 10.3.8, but it prevents my app from loading, when I put the following into web.xml:

      <servlet>
            <servlet-name>logSupportServlet</servlet-name>
            <servlet-class>com.mycompany.mypackage.MyRepositorySelector</servlet-class>
            <load-on-startup>6</load-on-startup>
      </servlet>

It seems to have problems casting my servlet in org.apache.catalina.core.StandardWrapper.loadServlet.

Here's the dump:

--------8<--------
war,id=846668036 WebModule) Servlet /com.mycompany.mypackage threw load() exception
java.lang.ClassCastException: com.mycompany.mypackage.MyRepositorySelector
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1048)
        at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:925)
        at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3857)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4118)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:759)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:739)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:524)
        at sun.reflect.GeneratedMethodAccessor154.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at org.apache.commons.modeler.BaseModelMBean.invoke(BaseModelMBean.java:503)
        at org.jboss.mx.server.RawDynamicInvoker.invoke(RawDynamicInvoker.java:150)
        at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:644)
        at org.apache.catalina.core.StandardContext.init(StandardContext.java:5005)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at org.apache.commons.modeler.BaseModelMBean.invoke(BaseModelMBean.java:503)
        at org.jboss.mx.server.RawDynamicInvoker.invoke(RawDynamicInvoker.java:150)
        at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:644)
        at org.jboss.web.tomcat.tc5.TomcatDeployer.performDeployInternal(TomcatDeployer.java:274)
        at org.jboss.web.tomcat.tc5.TomcatDeployer.performDeploy(TomcatDeployer.java:91)
        at org.jboss.web.AbstractWebDeployer.start(AbstractWebDeployer.java:357)
        at org.jboss.web.WebModule.startModule(WebModule.java:68)
        at org.jboss.web.WebModule.startService(WebModule.java:46)
        at org.jboss.system.ServiceMBeanSupport.jbossInternalStart(ServiceMBeanSupport.java:272)
        at org.jboss.system.ServiceMBeanSupport.jbossInternalLifecycle(ServiceMBeanSupport.java:222)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:141)
        at org.jboss.mx.server.Invocation.dispatch(Invocation.java:80)
        at org.jboss.mx.server.Invocation.invoke(Invocation.java:72)
        at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:249)
        at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:644)
        at org.jboss.system.ServiceController$ServiceProxy.invoke(ServiceController.java:897)
        at $Proxy0.start(Unknown Source)
        at org.jboss.system.ServiceController.start(ServiceController.java:418)
        at sun.reflect.GeneratedMethodAccessor10.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:141)
        at org.jboss.mx.server.Invocation.dispatch(Invocation.java:80)
        at org.jboss.mx.server.Invocation.invoke(Invocation.java:72)
        at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:249)
        at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:644)
        at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:177)
        at $Proxy28.start(Unknown Source)
        at org.jboss.web.AbstractWebContainer.start(AbstractWebContainer.java:394)
        at org.jboss.deployment.MainDeployer.start(MainDeployer.java:964)
        at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:775)
        at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:738)
        at sun.reflect.GeneratedMethodAccessor48.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:141)
        at org.jboss.mx.server.Invocation.dispatch(Invocation.java:80)
        at org.jboss.mx.interceptor.AbstractInterceptor.invoke(AbstractInterceptor.java:121)
        at org.jboss.mx.server.Invocation.invoke(Invocation.java:74)
        at org.jboss.mx.interceptor.ModelMBeanOperationInterceptor.invoke(ModelMBeanOperationInterceptor.java:127)
        at org.jboss.mx.server.Invocation.invoke(Invocation.java:74)
        at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:249)
        at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:644)
        at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:177)
        at $Proxy8.deploy(Unknown Source)
        at org.jboss.deployment.scanner.URLDeploymentScanner.deploy(URLDeploymentScanner.java:325)
        at org.jboss.deployment.scanner.URLDeploymentScanner.scan(URLDeploymentScanner.java:483)
        at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.doScan(AbstractDeploymentScanner.java:204)
        at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.loop(AbstractDeploymentScanner.java:215)
        at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.run(AbstractDeploymentScanner.java:194)
--------8<--------

 

by: rstaveleyPosted on 2007-08-29 at 10:32:04ID: 19793422

Do you reckon that means the servlet needs to extend org.apache.catalina.core.StandardWrapper?

 

by: jim_cakalicPosted on 2007-08-29 at 12:07:58ID: 19794327

I was about to write the whole thing up for you but then I found this page:
http://thegioitinhoccuatoi.blogspot.com/feeds/posts/default

RepositorySelector isn't a servlet. You need a servlet that can instantiate the RepositorySelector and initialize the LogManager with it. The servlet needs to have a low load-on-startup value to ensure that it absolutely loads (and initializes) before any other servlet that might call Logger.getLogger. This is /very/ important. Any call to Logger.getLogger prior to setting the RepositorySelector will cause a default RepositorySelector to be installed which cannot be overridden.

When you navigate to the link above, search on the page for RepositorySelector. There is code for that (which I think is identical to what we'd already found) as well as a servlet implementation that does the necessary configuration. Give this a go.

Regards,
Jim

 

by: rstaveleyPosted on 2007-08-29 at 12:15:31ID: 19794414

> Give this a go

I'm on the case. Many thanks, Jim.

 

by: rstaveleyPosted on 2007-08-29 at 12:45:30ID: 19794714

This is looking better:

java.io.FileNotFoundException: /opt/jboss-4.0.2/bin/log4j.dtd

Just got to find that DTD and I should be in business.

 

by: rstaveleyPosted on 2007-08-29 at 14:57:37ID: 19795818

Well I am generating that 0 byte file now, but that's as far as it goes.

I have a feeling that my problem is that Spring gets loaded before the Servlet does and I have Action classes getting loaded as singletons, which create instances of the logger, before the servlet that initialises the RepositorySelector is able to get in on the act.

<listener>            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Spring loads POJOs, which I guess means that I need to get a ServletConfig to pass to the first bean to call MyRepositorySelector.init(config).

I've spotted http://www.blojsom.com/blog/david/java/2006/03/03/Getting-a-ServletConfig-reference-within-Spring.html which may fit the bill.... but I suspect that this is no good for the singletons. Need to continue this tomorrow.

 

by: rstaveleyPosted on 2007-08-29 at 23:08:29ID: 19797775

New day and a new link. I've found this link: http://wiki.jboss.org/wiki/Wiki.jsp?page=Log4jRepositorySelector

It introduces the idea of a ServletContextListener. Initialising the RepositorySelector from a servlet means that you get in on the action after Spring's listener has initialised its singletons. I'm hoping that having

<listener>
<listener-class>org.jboss.webapp.listener.AppContextListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

i.e. registering org.jboss.webapp.listener.AppContextListener before org.springframework.web.context.ContextLoaderListener will get the RepositorySelector initialised earlier in the food chain.

I'll let you know how I get on.

 

by: jim_cakalicPosted on 2007-08-30 at 05:02:44ID: 19799188

I'm sorry. I didn't realize Spring was part of the equation.

 

by: rstaveleyPosted on 2007-08-30 at 05:06:20ID: 19799211

I hadn't realised it would be relevant. You gave me the pointer to the right thing to get me started. Unfortunately, I'm still having to do battle with it. I'll get a stack dump and see what it is grumbling about now.

 

by: rstaveleyPosted on 2007-08-30 at 05:08:28ID: 19799228

Hmmm no stack dump, but it isn't appending that log file. I'll rename the file and  see if it is still creating it.

 

by: rstaveleyPosted on 2007-08-30 at 05:12:24ID: 19799252

It is creating the file, but it isn't logging to it.

 

by: rstaveleyPosted on 2007-08-30 at 05:33:07ID: 19799399

Weird.

(1) I set up the listener and log a message in it from org.jboss.webapp.listener.AppContextListener (making the class one from my package). Promisingly I see my local log4j.xml working :-)

(2) Spring loads its listener and I log messages, but the logging henceforth seems to be using the regular JBoss logging. :-(

It looks like JBoss overrides the LogManager's RepositorySelector subsequently?!

 

by: rstaveleyPosted on 2007-08-30 at 05:39:41ID: 19799448

Looking at http://logging.apache.org/log4j/docs/api/org/apache/log4j/LogManager.html#setRepositorySelector(org.apache.log4j.spi.RepositorySelector,%20java.lang.Object) it looks like I may be able to set up a guard other than LogManager.getRootLogger() and prevent the RepositorySelector from being subsequently overriden. I'll give it a go.

 

by: jim_cakalicPosted on 2007-08-30 at 05:42:35ID: 19799472

So the message you log from the AppContextListener correctly arrives in your applictation log files but logging from the application gets diverted back to the JBoss log file, correct? Sounds like a possible classloader issue. Like using the AppContextListener with the RepositorySelector is working correctly but it initializes logging in a different classloader than the one used to load your classes ... I haven't yet but I'll take a look at that last link you posted.

 

by: rstaveleyPosted on 2007-08-30 at 05:56:26ID: 19799572

Whoopadidoo!!!

Here's what I had to do to ovoid getting overriden:

--------8<--------
// $Id: ApplicationRepositorySelector.java,v 1.2 2007/08/30 12:45:48 rstaveley Exp $

/*
 * $Log: ApplicationRepositorySelector.java,v $
 * Revision 1.2  2007/08/30 12:45:48  rstaveley
 * Using Guard
 *
 * Revision 1.1  2007/08/30 07:28:51  rstaveley
 * Slightly altered from http://wiki.jboss.org/wiki/Wiki.jsp?page=Log4jRepositorySelector because of SiteSuite finger problems (deleting a file with capitalisation wrong and then trying to recreate it).
 *
 */

/***************************************
 *                                     *
 *  JBoss: The OpenSource J2EE WebOS   *
 *                                     *
 *  Distributable under LGPL license.  *
 *  See terms of license at gnu.org.   *
 *                                     *
 ***************************************/
package org.jboss.repositoryselectorexample;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.log4j.Hierarchy;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.spi.LoggerRepository;
import org.apache.log4j.spi.RepositorySelector;
import org.apache.log4j.spi.RootLogger;
import org.apache.log4j.xml.DOMConfigurator;
import org.w3c.dom.Document;

class Guard {}


/**
 * This RepositorySelector is for use with web applications. It assumes that
 * your log4j.xml file is in the WEB-INF/classes directory.
 *
 * @author Stan Silvert
 */
public class ApplicationRepositorySelector implements RepositorySelector {
            
    private static boolean initialized = false;
    private static Object guard = /*LogManager.getRootLogger()*/ new Guard();
    private static Map repositories = new HashMap();
    private static LoggerRepository defaultRepository;

    public static synchronized void init(ServletConfig servletConfig)
            throws ServletException {
        init(servletConfig.getServletContext());
    }

    public static synchronized void init(ServletContext servletContext)
            throws ServletException {
        if (!initialized) // set the global RepositorySelector
        {
            defaultRepository = LogManager.getLoggerRepository();
            RepositorySelector theSelector = new ApplicationRepositorySelector();
            LogManager.setRepositorySelector(theSelector, guard);
            initialized = true;
        }

        Hierarchy hierarchy = new Hierarchy(new RootLogger(Level.DEBUG));
        loadLog4JConfig(servletContext, hierarchy);
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        repositories.put(loader, hierarchy);
    }

    public static synchronized void removeFromRepository() {
        repositories.remove(Thread.currentThread().getContextClassLoader());
    }

    // load log4j.xml from WEB-INF/classes
    private static void loadLog4JConfig(ServletContext servletContext,
            Hierarchy hierarchy) throws ServletException {
        try {
            String log4jFile = "/WEB-INF/classes/log4j.xml";
            InputStream log4JConfig = servletContext.getResourceAsStream(log4jFile);
            Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(log4JConfig);
            DOMConfigurator conf = new DOMConfigurator();
            conf.doConfigure(doc.getDocumentElement(), hierarchy);
        } catch (Exception e) {
            throw new ServletException(e);
        }
    }

    private ApplicationRepositorySelector() {
    }

    public LoggerRepository getLoggerRepository() {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        LoggerRepository repository = (LoggerRepository) repositories.get(loader);
        if (repository == null) {
            return defaultRepository;
        } else {
            return repository;
        }
    }
}
--------8<--------

Presumably later on in the load sequence, JBoss was installing its own RepositorySelector, using LogManager.getRootLogger() as its guard. All I had to do was to make my guard something different.

Many thanks for hanging in there with me Jim. Credit to you for putting me on the right track in the first place. I hope my rambling helps others, using Spring with JBoss with Log4J.

 

by: jim_cakalicPosted on 2007-08-30 at 05:58:50ID: 19799592

That's great! Glad to be of assistance. :-)

 

by: rstaveleyPosted on 2007-10-30 at 10:45:03ID: 20179390

For the PAQ record, I tried the same approach for a Spring MVC application and found that I needed to have...

  private static Object guard = LogManager.getRootLogger() /*new Guard()*/;

...to get it to work in that application. I guess it installed its own RepositorySelector before my listener, which is puzzling. I thought the listeners would be the first part of the framework to be loaded and my listener was the first. I wonder if the filters do it??

Anyhow, if anyone finds themselves pulling their hair out over this, just let it be known that it is worth experimenting with both approaches for the guard!

 

by: rstaveleyPosted on 2007-10-30 at 11:18:09ID: 20179617

Beware that I had to change my code to use the deprecated RootCategory class instead of RootLogger to get it to compile for JBoss too.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...