9 Replies Latest reply on Jul 4, 2013 9:44 AM by sfcoy

    Log4j logging problem

    gmgallo

      Hi guys

       

      I have finally started to use jboss7 (previously I used jboss4 and jboss5) and now I don't know how to use an external file for log4j configuration. In the specific I have a simple ejb project with a starter class that has the following code:

       

      [code]

      @Startup

      @Singleton

      public class Starter {

       

       

                public static final Logger LOGGER = Logger.getLogger(Starter.class);

       

       

                @PostConstruct

                void init() {

       

       

                          System.out.println("Starting MSH backend");

       

       

                          System.out.println("Configuring properties");

       

       

                          try {

       

       

                                    PropertiesManager.loadProperties();

                                    System.out.println("Properties loaded, configuring loggers");

                                    DOMConfigurator.configureAndWatch("/opt/ejb/ejbLog4j.xml"));

                                    System.out.println("Loggers configured");

       

                                    LOGGER.info("MSH EJB STARTED");

       

       

                          }

                          catch(IOException e) {

       

       

                                    System.out.println("ERROR! Props file not found!");

       

       

                          }

                          catch(PropertyNotFoundException e) {

       

                                    System.out.println("ERROR! Logger filename property not found");

       

                          }

       

       

                }

       

       

      }

       

      [/code]

       

      Simply I use DomConfigurator to read an external log4j configuration file and to configure the logger properly. This is the content of the xml file:

       

      [code]

       

      <?xml version="1.0" encoding="UTF-8"?>

       

       

      <!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">

      <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

         

          <appender name="mshEjb" class="org.apache.log4j.DailyRollingFileAppender">

              <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>

              <param name="Append" value="true"/>

              <param name="File" value="/var/opt/msh/msh_ejb.log"/>

              <layout class="org.apache.log4j.PatternLayout">

                  <param name="ConversionPattern" value="%d{dd.MM.yyyy HH:mm:ss.SSS} [%C{1}] - %m%n"/>

              </layout>

          </appender>

         

          <category name="com.stonesoft.msh.ejb" additivity="false">

              <priority value="DEBUG"/>

              <appender-ref ref="mshEjb"/>

          </category>

      </log4j:configuration>

       

      [/code]

       

      The problem is that the application creates the log file correctly but nothing is written in it. In the server.log instead I can find both the standard print and the log print. I've tried all the ways I found in internet without luck. Any idea?

       

      Thanks

        • 1. Re: Log4j logging problem
          erasmomarciano

          Hi

           

          In Jboss7 it's better to use org.apache.commons.logging.Log;

           

           

          You might see this link

           

          https://community.jboss.org/wiki/HowToMakeAWeb-appWithLoggingAndHowToConfigureJBossAs7CoreLoggingStepByStep

          • 2. Re: Log4j logging problem
            gmgallo

            Hi

             

            I've read your link but now I have some more questions:

             

            a) will log4j not be used more?

            b) is the link solution useful also for an ejb project?

            c) instead of putting the step3 solution in standalone.xml can i put it in an external file? For example in my ejbLog4j.xml?

            d) should i also add some java code to pick up the configuration?

             

            Thanks

            • 3. Re: Log4j logging problem
              wdfink

              I prefere to use Logger inside the app (log4j slf4j commons-logging ...).

              The configuration I add to the server config file so you can controll the logging behaviour per installation without change the app files.

              • 4. Re: Log4j logging problem
                gmgallo

                you have centered the focal point. I need to have external config files (general properties file, log properties file, wsdl and so on) because there is the need to change at runtime some configs, avoiding a jboss restart or a change in jboss conf (substantially our systemists want always a clean jboss).

                • 5. Re: Log4j logging problem
                  gmgallo

                  Tried the solution of the second post but still no luck

                  • 6. Re: Log4j logging problem
                    sfcoy

                    gmgallo wrote:

                     

                    ...substantially our systemists want always a clean jboss...

                    Personally, I'm really resisting the urge to use some quite colourful language here.

                     

                    How do your "systemists" propose that:

                    • database connections are configured
                    • database connection pools be configured
                    • thread pools be configured
                    • security be configured
                    • bind addresses be configured
                    • transaction timeouts be configured
                    • etc

                    ?

                     

                    If your application is managing all of the above for itself then there's no point in using an application server. You may as well just use Tomcat.

                     

                    These things are a significant part of the "raison d'être" of a full blown application server.

                    • 7. Re: Log4j logging problem
                      sfcoy

                      This article describes how JBossAS/WildFly logging can be configured at runtime: AS71 CLI Recipes - Toggle root logger level during runtime without bouncing the server.

                      • 8. Re: Log4j logging problem
                        gmgallo

                        You're right, think that our systemist don't want also that the application writes temp files in the app server. I'll answer to you by points:

                         

                        - database connections pools / database connections: in jb5 they use a normal datasource deployed with the application; in jb7 they create a new module for the driver and in the standalone.xml they add a driver and a datasource. Datasources are created by dba;

                        - thread pools: I don't know how they manage it;

                        - security: they manage it via truststore/keystore pointed directly from the configuration;

                        - addresses: they configure the machine not the single application server;

                        - transaction timeouts: managed by the datasources (created by dba).

                        In other words systemists think that all the configurations that never change along the time can be configured directly in jboss and managed by the application server, all the configurations that can change in the future must be managed by the application. In this case logger configurations can change (for example the dir of the log file, the name of the log file or the log level) so must be managed by the application.

                         

                        Thanks for the link, I'll try your solution

                        • 9. Re: Log4j logging problem
                          sfcoy

                          FWIW all the big commercial application server vendors (WebSphere and WebLogic) provide an interface for dynamically configuring logging.