12 Replies Latest reply on Oct 18, 2004 1:10 PM by khurle

    log4j does not work for my app

    mferreg

      Where do I have to write log4j configuration for my war?

      I though to could create a log4j.xml into WEB-INF/classes inside my war, but it doesn't generate any log for my own java classes.

      What else do I have to do?

        • 1. Re: log4j does not work for my app
          prabhakar

          Try adding log4j.properties to WEB-INF/classes.

          -prabhakar

          • 2. Re: log4j does not work for my app
            shortpasta

            look in your classpath. the first log4j found is the one used. in 3.2.1 look in your conf folder for example.

            in previous versions the jboss/bin might have been used as well (not sure).

            last resort: make a startup servlet (or any other part of your code you know that runs) & do this
            URL url = getClass().getResource ("/log4j.xml");
            and print the url

            • 3. Re: log4j does not work for my app
              mferreg

              I've found it, thanks, but now the problem is it only logs for classes different from ejbs.

              What can it be?

              • 4. Configuring log4j in 3.2
                droolinggeezer

                There does not appear to be any documentation on log4j config for 3.2 and it's a shame as some good work was done here. Here is a simplified summary of my discoveries. I am assuming that you are assigning logging category names that are equal to your class names: com.yourcompany.utilities.Solver. I wander a bit between category, class tree and package. I mean the same thing.

                log4j configuration is performed in the log4j.xml file which is in the /conf directory.

                The first part of this file in its stock contains a number of appender definitions. Only a few are active (not commented out). If you don't have an XML editor it may be hard to see.

                One of the notable ones is that for the CONSOLE appender (which, as you'll notice) directs output to System.out. It includes its own "level" filter setting which excludes all output below the specified value (comes set to INFO). Another detail to note on this appender instance is the ConversionPattern spec:



                The "[%c{1}] " portion of this prints only the "leaf" element of category names (e.g., it will print : [comp] to identify output from the category "com.salpon.comp". This saves considerable space (that's important for console output) and the same spec can be used to condense your own logs when you specify a specialized log to contain output from just your own stuff (more on that later).

                Another appender is one name "FILE". This specification defines an instance of a file-directed appender. this one is used as the default server-output "device". Its output is directed to:



                which I'm sure you can figure out. Note: it is also, by default, configured:



                This configuration causes the current log file ( log/server.log) to be overwritten each time the server is restarted. That may not be what you want for a production environment so change it to "true". That brings up the rest of the specification which is the configuration of how the current server.log file is "rolled" and renamed every night at midnight. Anyone who struggled with this in early versions of Tomcat will appreciate that this actually works. If you, instead, want the file rolled based on a fixed size threshhold, there is an example of a size-based rolling appender too.

                There are a number of other appender examples including ones to generate SNMP traps and an EMAIL one to email your favorite sysop when your code buggers badly.

                The next section of the file is labeled "Limit Categories" which can be used to very selectively suppress noisy code (like Xerces) while allowing you to select very detailed debug trace from just one of your classes. I found the following entries most valuable:









                These entries will permit you to suppres all the noise from the jboss and apache modules while you turn up the volume on your own modules. Setting these two categories to INFO level allows you to "safely" set the CONSOLE appender to DEBUG. This latter step allows your categories to log everything down to DEBUG through the CONSOLE while keeping a sock in jboss and apache. Say, for example, you have two class trees:

                -com.yourcomany.....
                -web.servlets.....

                Adding the following entries will enable DEBUG output from all classes in your two trees:









                Now say you have a really noisy package in:

                -com.yourcompany.utilities

                which is of little interest. You can selectively suppress debug from just the utility category classes by adding:





                while all your other classes spew debug like Old Faithful.
                Neat.

                In the final section of the document, you find the following definition which selects the logging destinations for the invisible "root" category (you'll have to read some basic stuff on log4j if you don't know about root).


                <appender-ref ref="CONSOLE"/>
                <appender-ref ref="FILE"/>


                This is a special category definition that causes EVERYTHING that is logged ( i.e., that is selected for output based on category Level setting) to be sent to both the CONSOLE appender "device" and the "FILE" appender. It winds up on the console and in the current "log/server.log" file. So what if you don't want the output to go to the console? Try this:


                <!-- <appender-ref ref="CONSOLE"/> -->
                <appender-ref ref="FILE"/>


                You still get a little console output during initialization and a scary message about an invalid console appender and looping but I detect no ill effects and all CONSOLE output ceases. You could, instead, just set the CONSOLE appender Threshhold value to ERROR, completely eliminating all output by any category configured for less than ERROR Level. That shuts 'em up.

                Now what if you want the output from your classes separated into their own log file. Try this:

                1. Make a copy of the appender specification called "FILE"
                2. change the name to "MYLOGGER"
                3. change the file name specification to something other than server.log like "mylog.log".
                4. Change your category specifications (from above) as follows:



                <appender-ref ref="MYLOGGER"/>




                <appender-ref ref="MYLOGGER"/>




                <appender-ref ref="MYLOGGER"/>


                All of your output will go to:

                1. to all of the appenders specified for the "root" (all categories have the root as the most significant part of the category name tree). In the stock version, it all goes to CONSOLE and server.log.
                2. to mylog.log

                The burning question I have seen discussed over and over is: How do I get my logging in my file and the other stuff somewhere else. First note that the real issue is the "root". If there are no appenders specified for the root, all output will not go anywhere EXCEPT the destinations explicitly defined for each. Hence the following series of specifications will come close to that goal for the examples herein:


                <!-- <appender-ref ref="CONSOLE"/> -->
                <!-- <appender-ref ref="FILE"/> -->




                <appender-ref ref="MYLOGGER"/>




                <appender-ref ref="MYLOGGER"/>




                <appender-ref ref="MYLOGGER"/>




                <appender-ref ref="FILE"/>
                <appender-ref ref="CONSOLE"/>



                <appender-ref ref="FILE"/>
                <appender-ref ref="CONSOLE"/>


                Just remember that at each naming node you encounter a similar issue. THat means if you want to send the output from one category in your class tree to its own log file, you must explicitly define destinations for all other categories at that naming node level.

                I hope this will prove helpful. The developers have done a nice job with this. I sure hope some REAL documentation comes out on 3.2 soon.


                • 5. Re: Configuring log4j in 3.2
                  ramgjboss

                  Thanks very much -- droolinggeezer . Your explanation and suggestion helped me out.

                  Thanks again
                  -Ram.g

                  • 6. Re: Configuring log4j in 3.2
                    mahantesh

                    Hi Ram,
                    I have done exactly what droolinggeezer said in his reply. But It did not work out for me.
                    Looks like my file appender definition is OK. When I try to add to Root category, it is perfectly working fine.
                    But it is not working for my own category name.

                    I have been trying to use it in my own ejb class with a seperat log file.

                    Could you please guide me what to do?

                    Madan B

                    • 7. Re: Configuring log4j in 3.2
                      qmqasim

                      If we depend on log4j.xml within JBoss for our logging, doesn't it make our code less portable?.

                      If I give somebody my code to run on her machine, I would have to provide them with the modified log4j.xml file; which the other person would have to put it on her jboss directory to see the righ logged events.

                      • 8. Re: Configuring log4j in 3.2
                        frito

                        Well, at least you are going to configure the logging for your sever instance not for a single application running on this server.

                        Greetings,
                        Frito

                        • 9. Re: Configuring log4j in 3.2
                          qmqasim

                          I use log4j to print data on to the console, but it prints the same message twice.

                          How can I print only once.

                          • 10. Re: Configuring log4j in 3.2
                            urddd

                            I have the same problem but it write the same thing 4 times!

                            Please help someone

                            • 11. Re: Configuring log4j in 3.2
                              jeroenf

                              To solve the dubble output do like this


                              ...


                              This will take care that MyLogger will only perform output to the appenders referenced by your category definition and not to the root

                              Greetz

                              • 12. Re: log4j does not work for my app
                                khurle

                                A previous user writes: "I've found it, thanks, but now the problem is it only logs for classes different from ejbs. "


                                I am having the same difficulty.

                                My POJO's log. But my MBean's and EJB's do not. They all use the same libraries and method calls and are deployed in the same ear.


                                Any help with this?
                                Kevin