9 Replies Latest reply on Jul 23, 2013 5:55 PM by James Perkins

    Add new loggers in runtime

    flp05 Newbie

      With the new logging module of Jboss 7, how can I use log4j to create new loggers in runtime and log to different files dynamically created.

       

      In previous versions I used to do something like this (code not tested):

       

       

       

                  Layout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p - %m%n");
                         
                  String fileTolog = "C:\\temp\\externalLog.log";
                  Logger logger = Logger.getLogger(fileTolog);
                    logger.setLevel(Level.INFO);
      
                     File file = new File(fileTolog);
                     FileAppender appender = new FileAppender(layout, file.getAbsolutePath(), false);
                     logger.addAppender(appender);
      
                     logger.error("TEST");
      

       

      I have a logger mechanism implemented which allows me to log different files in different places of the application. The code is based on the code presented here.

       

      I don't get any error, it just outputs the log into the main log, but not into the new files as it is supposed to. It creates the files but they always stay blank.

       

      My previous version of Jboss was Jboss AS 4.0.4

      My new version of Jboss is AS 7.1.1´

       

      Thanks in advance

      Luís Oliveira

        • 1. Re: Add new loggers in runtime
          Stephen Coy Master

          You have fundamentally the same problem as jboss log info twice.

           

          Personally, I recommend that you remove all of this logging support stuff from your application and manage it in the server. Less code is better code!

          • 2. Re: Add new loggers in runtime
            flp05 Newbie

            Thanks Stephen.

             

            My loggers have to be created dynalically, therefore they need to be managed in the code.

             

            In the server configuration I have the main log, which receives all the logs from the application. But, then, I have many classes which need to log into different files every time they run. It's impossible to define that on the server configuration, because only on runtime it can be defined.

             

            How can I do that? Any ideas?

            • 3. Re: Add new loggers in runtime
              Stephen Coy Master

              Check out a source copy of the version of log4j that you're using, change the base package name (from org.log4j to [say] com.yourcompany.log4j - I believe that the licence permits this, but check for yourself) and use this version for your dynamic logging requirements.

              • 4. Re: Add new loggers in runtime
                James Perkins Master

                You could also exclude log4j from being added to the deployment and package it with your application as well. That should allow you to create loggers programmatically.

                 

                --

                James R. Perkins

                • 5. Re: Add new loggers in runtime
                  Stephen Coy Master

                  I think Luis wants to use both regular JBoss logging (the "main log") plus create loggers dynamically from within his code...

                  • 6. Re: Add new loggers in runtime
                    flp05 Newbie

                    Thank you Stephen and James.

                     

                    In the past couple of days, I tested a few more ideas, but none of them allow me to do exactly what i want,

                     

                    Stephen Coy, that was a good suggestion but the problem is the logging is not made explicitily by me, but by a framework (internal from my company). I can not change the framework as it's shared from multiple applications.

                     

                    James Perkins, I already tried to remove the logging from Jboss (by excluding log4j and/or commonsloggings), but nothing seems to work. It may be related to the framework I'm using, as the framework jars are placed in the EAR/lib. Also, I have to keep a "centralized" logging system wihtin the server, as multiple applications share the same log.

                     

                    So, to let perfectly clear what I want to do:

                    • I want to have a main logging system which serves multiple applications and the configurations are placed in the server (either on standalone.xml or in a log4j.xml).
                    • I also want to be able to, during execution, create loggers dynamically (to do this I use the framework that I talked about, which uses log4j).
                    • This was working perfectly in Jboss 4 in two very big and complex applications. Changes in the code are not easy to perform. I aready tried multiple solutions based on multiples posts I read only about jboss 7 logging.

                     

                    I have a question that may help me finding a solution? Can I completely disable Jboss logging system, and still let the log4j.xml file in the Jboss server, using the same file from all the applications?

                     

                     

                    Thanks again,

                    Luís Oliveira

                    • 7. Re: Add new loggers in runtime
                      James Perkins Master

                      I want to have a main logging system which serves multiple applications and the configurations are placed in the server (either on standalone.xml or in a log4j.xml).

                      With standalone.xml you can do this with no problems. You could do it with log4j.xml for your deployments only, but not the server logging. For example org.jboss.as.* will not be logged through the log4j log manager.

                       

                       

                      I also want to be able to, during execution, create loggers dynamically (to do this I use the framework that I talked about, which uses log4j).

                      This is not possible with the logging subsystem (central logging configuration). While you could do that now, in the future we will likely lock down changes via code for at least the server level log manager configuration.

                       

                       

                      This was working perfectly in Jboss 4 in two very big and complex applications. Changes in the code are not easy to perform. I aready tried multiple solutions based on multiples posts I read only about jboss 7 logging.

                      There have been quite a few changes from JBoss AS 4 to JBoss AS 7. The last release for AS 4 was just over 5 years ago

                       

                       

                      In general I would suggest using the logging subsystem. It allows for runtime configuration changes.

                       

                      --

                      James R. Perkins

                      1 of 1 people found this helpful
                      • 8. Re: Add new loggers in runtime
                        flp05 Newbie

                        James, basically you are saying that there is no solution to use the jboss logging system and also create loggers dynamically in runtime?

                        • 9. Re: Add new loggers in runtime
                          James Perkins Master

                          I'll take the cheap way out and say that it should work like that for now, assuming you use either JBoss Log Manager or JUL, but we might break that in the future You could however, note this is not portable, use the management API's available to configure logging in your code though.

                           

                          --

                          James R. Perkins