1 2 Previous Next 18 Replies Latest reply on Feb 13, 2012 2:16 PM by James Perkins

    Log4j support questions

    Joseph Fouts Novice

      I'm currently trying to migrate our app from jboss 5 to jboss 7 and want to support log4j.


      Getting JBoss 7 to recognize and use log4j does not seem to be my problem.  My problem is getting my messages to produced in the file appender that I created.


      Before I give an overview of my test case I do have a question about the jndi.properties file.  Can it still be used and if yes then were should it be located?  I have it located in

      the root directory of my jar file but yet it seems as though its not finding it.


      I currently have one in my app.jar file in the following structure:






      I have the following defined in it but yet I don't see my apps log request being displayed on the console.  Am I missing something?


      log4j.rootCategory=DEBUG, O


      # Stdout



      log4j.appender.O.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n



      So then I tried to create my own log4j  fileAppender in a custom logger app that resides on the server.  The code is able to create the appender ffile defined in the appender fine.  But when I post a message to it nothing ever gets written to it.  Log4j seems to be using the BridgeLogger for this task.

      Attached is snippet from my logger code.


      Here is the simple call from my session bean:


      Main_Logger.debug("In add method of bean");




      I realize that I must be missing something.  Any help is greatly appreciated.



        • 1. Re: Log4j support questions
          Joseph Fouts Novice

          Please note that there was a typo in the attached code that was fixed but still produced the same problem as described.



          public static void debug(String msg)




          • 2. Re: Log4j support questions
            Tomaz Cerar Master

            how does your application structure looks like? you have log4.jar in your lib?


            try removing it and add Dependancy: org.apache.org to your manifest.mf and it should then just use server configuration(standalone/domain.xml) for logging.


            as for jndi.properties goes, it is not used anymore or at least not in same manner, what are you trying to achive?



            i recomend reading:







            • 3. Re: Log4j support questions
              Joseph Fouts Novice

              Hi Tomaz,


              Thanks for the reply.


              I'm using Eclipse.  I added the the log4j.1.2.16.jar file to the classpath via pointing it to the jboss 7 modules/ structure.

              I also add the Dependancy that you described.


              As far as what am I trying to achive....


              I want to create a parent for the log4j logger and then have modules in our application call our parent logger. 

              In the parent logger we create a log4j file appender with the log file name and put it in the user path.  As I

              mentioned the file gets created fine.  When our modules  do a logger.debug("xxxxx") the message never gets written to the

              defined appender log file.  No errors are comming up anywhere so I'm not sure where the message is going.  That is why I asked

              about the log4j.properties file.  I thought that possibly something was overriding my code.



              • 4. Re: Log4j support questions
                Tomaz Cerar Master



                this is different scenario. You cannot have imported parent log4j logger (one from modules) and your own appender in your application and expect it to work...

                in that case you must put log4j jar inside your application and bootstrap it manualy it should work without any problems.




                • 5. Re: Log4j support questions
                  Joseph Fouts Novice

                  How do I go about adding the log4j jar file as a bootstrap manually?  Sorry but I'm not familar with that type of setup.



                  • 6. Re: Log4j support questions
                    Tomaz Cerar Master

                    In this case log4j configuration has noting to do with app server you configure and use it inside your application.

                    Take a look at manual http://logging.apache.org/log4j/1.2/manual.html chapter Configuration

                    but basicly, you do something like this PropertyConfiguration.configure(your-properties.file.or.url) when starting your application.




                    • 7. Re: Log4j support questions
                      Joseph Fouts Novice

                      I did try that.


                      In my bean I had the following:




                      Kept getting error that it could not find the file.


                      It was at the root of my jar file.







                      Any idea on why it can't find the jndi.properties file?












                      I had

                      • 8. Re: Log4j support questions
                        Joseph Fouts Novice

                        One more thing to mention is that this code example that I included did work with Jboss5 when it used log4j by default.

                        • 9. Re: Log4j support questions
                          Tomaz Cerar Master

                          in your example your are referencing log4j.properties but in comment your are taking about jndi.properties.


                          and your  code that is trying load log4j.properties it is trying to load it with classloader from MyBean class that means that log4j.properties should be in same package that your "MyClass.class"


                          it it is in root you should load it trough Thread.currentThread().getContextClassLoader().getResource("log4j.properties")...



                          Jboss as < 7 was too forgiving about classloading, now there is proper isolated classloading. this why it does not work anymore, in truth it shouldn't work in first place.




                          • 10. Re: Log4j support questions
                            Joseph Fouts Novice

                            Sorry I was referrring to the log4j.properties file.


                            With you sample I was able to get the read the properties file but it still does not seem to be using it.

                            I have all messages set to go to the console and yet I don't see my message on the console.


                            My real goal was to creat a dynamic file appender to write the log info to and not use the log4j.properties file.

                            But that does not seem to be working.


                            So if my module can create an org.apche.log4j.FileAppender without error does this not mean that I have the log4j jar

                            file setup correctly in jboss?  And if this is true then it sounds like the FileAppender is not working correclty with my

                            setup.  As I mentioned I am able to create the file appender and then add it to my logger.  If I look in the directory I see

                            that the file is created.  So this tells me that log4j is doing that part right.  But then when I try and write a .debut("xxxx")

                            message it never is writen to my file appender. 


                            Not sure if I'm missing something from log4j or could this be a bug in log4j?


                            This should not be this difficult.


                            My company wants us to use Websphere but I wanted to give JBoss a try.  Thought it would be easier.  I guess not.


                            Thanks again for all of you help. 

                            • 11. Re: Log4j support questions
                              Tomaz Cerar Master

                              Then do you need your custom appender or not? As for your use case i don't see any reason for your custom appender...


                              what you can do is just configure jboss logging in standalone.xml and remove all your custom log4j startup and custom configuration and just import log4j via manifest.mf Dependencies

                              after that you just follow https://docs.jboss.org/author/display/AS71/Logging+configuration and you can setup any logging scenario you need/want adding extra appender to some custom file and log

                              there  just some special categories is really easy to do...




                              • 12. Re: Log4j support questions
                                Joseph Fouts Novice



                                Still confused.


                                My company wants to use log4j.


                                I don't really care how I get the logs our I just need to get them out.


                                I have tried 2 ways. 


                                Reading in the log4j.properties file.  - This does not seem to work.  Based on your sugestion I am able to read in the log4j.properties file but I am unable to have it log to the appedners defined in it.  With the following definition would you not expect my debug log messages to go to the console?  Well they do not.


                                log4j.rootCategory=DEBUG, O


                                # Stdout



                                log4j.appender.O.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n



                                The second was to dynamically create a log4j file appender and use that to write my log messages to.  This does not work either.  My code creates the log4j fileappender and then adds it to my logger.  My logger then does a .debug("xxxx") and it does not get written to my file appender.  Not sure why this does not work.


                                If I could get either one of these to work I would fine but unfortunaltey I can't get either to work.


                                Based on your responses it sounds like I should be.  What am I missing?




                                • 13. Re: Log4j support questions
                                  James Perkins Master


                                  You will need to exclude the log4j dependency in the jboss-deployment-structure.xml.


                                  <?xml version="1.0" encoding="UTF-8"?> 
                                         <module name="org.apache.log4j" />


                                  Then add the log4j dependency to your deployment. You'll also need to initialize log4j somewhere in your app to find the log4j configuration.


                                  Currently providing log4j as a default dependency, but the appenders don't work since log4j is not used for logging JBoss AS7. There is a plan to automatically scan deployments for logging configurations, log4j and logging.properties, but that will not make it in until 7.1.1.




                                  James R. Perkins

                                  • 14. Re: Log4j support questions
                                    Joseph Fouts Novice

                                    Thanks James. Couple of questions.


                                    So will your suggestion allow me to use both the log4j.properties file and create a log4j file appender in a java program?


                                    Where is the jboss-deployment-structure.xml file?  I can't seem to find it.  If I have to create it where will it go?


                                    When you say initialize log4j somewhere do you mean -->  PropertyConfigurator.configure(Thread.currentThread().getContextClassLoader().getResource("log4j.properties"));


                                    Once I get the info on where the jboss-deployment-structure.xml file is I will give it at try.



                                    1 2 Previous Next