9 Replies Latest reply: Sep 12, 2011 3:55 AM by jaikiran pai RSS

JBoss & Log4J

Tom Simmons Newbie

I'm afraid I'm having a bit of trouble understanding Log4J in JBoss and how to achieve what I want.

 

Previously with GlassFish and Tomcat in my project I had a prperties file that defines appenders and loggers, I had one appender and then lots of loggers so I could have a logger for each area of the system and control their logging level independently.  That was as things matured I could reduce the logging, then when a new feature was added, or a problem was reported, I could turn the logging up for that area rather than drowning in output from the whole system.

 

Now as I understand in the jboss-log4j.xml file has a file and console appender, various JBoss sub systems have their logging level set in the here, and the last section in the file the 'root' bit catches all the logging from all the apps and sends it to both the appenders.  I can change the root logging level, but this affects the level for all apps hosted on the instance.

 

First question...is the logging level assigned to root hot changeable?  ie Can it be sat at WARN, then if a problem occurs change it to DEBUG and see more out put without having to restart JBoss?

 

The application I'm working on I inheritted.  At the top of a class there is a line

 

static final Logger logger = Logger.getLogger(<class name>.class);

 

This I'm assuming is basically setting up a logger for this class, rather than having the logger defined in a properties file?

 

Did you see question sneakily hidden there?!

 

The final question.  I would like (unless there is a better way) to set up logging similar to how I used to work it under Tomcat as described above.  How would I go about doing this?

 

Sorry if these are daft questions, I'm afraid I'm not very proficient with JBoss - hence the next discussion I'm about to create...

 

 

Thanks for any help...

 

Tom

 

 

PS I should probably have said, I'm using JBOss 5.1.0 GA

  • 1. Re: JBoss & Log4J
    Tom Simmons Newbie

    OK, no response, maybe I wasn't clear....?

     

    What I want is my own application specific log4j xml or propeties file stored in my project where I can configure several loggers to a common appender (or even use the standard appender of JBoss).

     

    I want several loggers so I can have distinct loggers for different areas of the application and so I can control the granularity of the loggin on each of these independently.

     

    I also would like this to be hot configurable so I can increase or reduce the logging level on the fly.

     

    I've done a lot of Googling, and I'm getting more confused rather than finding a solution, there is so much info out there for older version of JBoss, for adding RepositorySelector functionality, that the RepositorySelector functionality may now be included...

     

    So if anyone can point me in the direction of a clear, easy to understand guide/example on how to do this with JBoss 5.1.0 GA, I would be a very happy man.

     

     

    Tom

  • 2. Re: JBoss & Log4J
    Max Rydahl Andersen Master

    This seem to be a question for AS runtime configuration - best to ask in the AS userforum?

  • 3. Re: JBoss & Log4J
    Tom Simmons Newbie

    OK, I figured out how to move it, and hopefully I've got it in the right place now.

     

     

    Tom

  • 4. Re: JBoss & Log4J
    Tom Simmons Newbie

    Have I still got this in the wrong forum?

     

    What I'm trying to achieve would seem not too much to ask and fairly common requirement.

     

     

    Tom

  • 5. Re: JBoss & Log4J
    jaikiran pai Master

    Tom Simmons wrote:

     

     

    First question...is the logging level assigned to root hot changeable?  ie Can it be sat at WARN, then if a problem occurs change it to DEBUG and see more out put without having to restart JBoss?

     

    Yes, that's possible. Changes to that jboss-log4j.xml are picked up (by default) every 60 seconds and you don't need a restart.

     

     

    Tom Simmons wrote:

     

     

    Previously with GlassFish and Tomcat in my project I had a prperties file that defines appenders and loggers,

    What was it named? log4j.properties?

     

     

    Tom Simmons wrote:

     

    The application I'm working on I inheritted.  At the top of a class there is a line

     

    static final Logger logger = Logger.getLogger(<class name>.class);
    

     

    This I'm assuming is basically setting up a logger for this class, rather than having the logger defined in a properties file?

     

    Did you see question sneakily hidden there?!

     

    The log4j reference manual will give you some detailed knowledge about what that line of code does and how it relates to the log4j configurations that you have in the log4j.properties/log4j.xml file http://logging.apache.org/log4j/1.2/manual.html. Anyway, in short, that line of code gets hold of a "named" logger whose name will correspond to the fully qualified classname of the class that you passed to the getLogger method. So you will have a logger named foo.bar.SomeClass. Later on when you issue logging statements on that logger (via logger.debug(), logger.info() etc..), log4j will look for the logging level of foo.bar.SomeClass category or its parent category (foo.bar) in case that specific category is absent. If the level allows that message to be logged, then it forwards that message to all applicable appenders for that logger. Each appender (based on its threshold level) can further decide whether to log that message or not.

     

     

    Tom Simmons wrote:

     

    The final question.  I would like (unless there is a better way) to set up logging similar to how I used to work it under Tomcat as described above.  How would I go about doing this?

     

    You should be able to do that in the jboss-log4j.xml file. i.e. adding new logger categories, changing their level, adding appenders etc... These changes will be picked up dynamically (every 60 seconds) or you can force the changes from a MBean in the jmx-console. Either way, this should not require any server restart.

     

    Overall, doing this in JBoss isn't a big deal. You just have to understand how and what to configure in the xml and to understand that piece, you'll have to check the log4j documentation.

  • 6. Re: JBoss & Log4J
    Tom Simmons Newbie

    Jaikiran

     

    Thanks for your reply.

     

    As you can see this question has been around for a while, in which I have found answers to 90% of it.

     

    The one bit that still stumps me is how to control the logging level on a logger by logger basis.  You are correcting in thinking it was a log4j.properties file.  From trying to find an answer though I believe there are some issues with using these rather than XML files when using JBoss because it can only reference one and it takes priority over the JBoss set or something?

     

    The answer you provide on the individual loggers is for configuring the jboss-log4j.xml, I don't really want to be modifying the JBoss configuration for each release etc, I was hoping to have the xml/properties file as part of my project.

     

     

    Tom

  • 7. Re: JBoss & Log4J
    jaikiran pai Master

    Tom Simmons wrote:

     

     

    The one bit that still stumps me is how to control the logging level on a logger by logger basis. 

    It will look something like this in the jboss-log4j.xml:

     

      

    <category name="org.myapp.db">
          <priority value="DEBUG"/>
       </category>
    
       <category name="org.myapp.engine">
          <priority value="INFO"/>
       </category>
    

     

    instead of its .properties equivalent (which you currently have):

     

    log4j.logger.org.myapp.db=DEBUG
    log4j.logger.org.myapp.engine=INFO
    

     

     

     

    Tom Simmons wrote:

     

    From trying to find an answer though I believe there are some issues with using these rather than XML files when using JBoss because it can only reference one and it takes priority over the JBoss set or something?

     

    The answer you provide on the individual loggers is for configuring the jboss-log4j.xml, I don't really want to be modifying the JBoss configuration for each release etc, I was hoping to have the xml/properties file as part of my project.

     

    You can use your own log4j.properties/log4j.xml within your application packaging. But you won't then have the ability to see the changes to those files dynamically picked up (unless you come up with some code to do that). However, if you still want to package those files in your app, I can provide you a link on how to do that.

  • 8. Re: JBoss & Log4J
    Tom Simmons Newbie

    jaikiran pai wrote:

    You can use your own log4j.properties/log4j.xml within your application packaging. But you won't then have the ability to see the changes to those files dynamically picked up (unless you come up with some code to do that). However, if you still want to package those files in your app, I can provide you a link on how to do that.

     

    If you could send me a link on this I'll have a read.

     

    Thanks for you help

     

    Tom

  • 9. Re: JBoss & Log4J
    jaikiran pai Master

    This was for JBoss AS 4.2.x, but applies for JBoss AS5 too http://community.jboss.org/message/198690#198690