10 Replies Latest reply on Jan 30, 2017 5:44 PM by James Perkins

    I want to have my own logging. Why I have to disable per-deployment logging?

    Tung Tran Newbie

      Hi all,

      I want my application has its own logging configuration(patterns, format, level, file). I googled a lot and It's very confused when I see many peoples disable the system property:

      -Dorg.jboss.as.logging.per-deployment=false

       

      As I understand, My requirement is so-called "per-deployment" logging configuration, right? It's very weird when I see that peoples say that we have to disable per-deployment logging in order to make their own logging configuration works.

      By the way,

      Can I use jboss-deploy-structure.xml to configure my so-called "per-deployment" logging?

      Why I have to exclude the dependency of log4j?

      This is my jboss-deploy-structure.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <jboss-deployment-structure>
          <deployment>
              <!-- Exclusions allow you to prevent the server from automatically adding 
                  some dependencies -->
              <exclusions>
                  <module name="org.apache.log4j" />
              </exclusions>
          </deployment>
          <!-- <subsystem xmlns="urn:jboss:domain:logging:1.0">
              <console-handler name="CONSOLE" autoflush="true">
                  <level name="DEBUG" />
                  <formatter>
                      <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n" />
                  </formatter>
              </console-handler>
              <periodic-rotating-file-handler name="FILE" autoflush="true">
                  <formatter>
                      <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n" />
                  </formatter>
                  <file relative-to="jboss.server.log.dir" path="server.log" />
                  <suffix value=".yyyy-MM-dd" />
              </periodic-rotating-file-handler>
              <logger category="com.tung">
                  <level name="INFO" />
              </logger>
              <root-logger>
                  <level name="DEBUG" />
                  <handlers>
                      <handler name="CONSOLE" />
                      <handler name="FILE" />
                  </handlers>
              </root-logger>
          </subsystem> -->
      </jboss-deployment-structure>
      

       

      My project structure:

      ps.PNG

       

      Any idea?

        • 1. Re: I want to have my own logging. Why I have to disable per-deployment logging?
          Tung Tran Newbie

          P/S: I read this post jboss-eap-6.1 + log4j configuration

          jamezp said log4j should work out of the box with JBoss EAP 6.1, everything we have to do is putting log4j.xml in WEB-INF/classes or META-INF. But It does not work for me...

          • 2. Re: I want to have my own logging. Why I have to disable per-deployment logging?
            James Perkins Master

            If all you want to do is use your own log4j.xml then just include the configuration in your WEB-INF/classes in your WAR and don't include a log4j dependency. You don't want to add the system property org.jboss.as.logging.per-deployment=false. Leave that off and it should just work. Assuming of course you're using log4j to log.

             

            --

            James R. Perkins

            • 3. Re: Re: I want to have my own logging. Why I have to disable per-deployment logging?
              Tung Tran Newbie

              Thank James for the reply.

              My project is jar so I put the log4j.xml in META-INF folder. But that make log totally disappear. I attach my project to hope you guys could understand my case.

              By the way, could you explain for me why setting per-deployment=false while we want to have the logging configuration for each deployment?

              • 4. Re: Re: I want to have my own logging. Why I have to disable per-deployment logging?
                James Perkins Master

                I can't seem to build the project because it can't find a bunch of the Switchyard dependencies.

                 

                With regards to the per-deployment=false it really just depends on what you want to do there. If that value is not set the logging subsystem will look for a log4j.xml configuration file and attempt to configure logging for your application.If you set that value to false, then you also need to include log4j in your deployment and exclude the log4j dependency the server adds by default.

                 

                I'd also advise against using the ConsoleAppender as it could result in deadlocks unless you disable the console-handler on the server.

                 

                --

                James R. Perkins

                • 5. Re: I want to have my own logging. Why I have to disable per-deployment logging?
                  Menelaus Perdikeas Newbie

                  I am sorry but this is not clear at all. I find myself having to use JBoss EAP 6.2 and have wasted a huge amount of time trying to configure per-deployment logging (not to mention that sometimes I am even failing to see plain System.out.println output - as reported here: http://stackoverflow.com/q/41905471/274677)

                  Can you please explain the following points:

                  1. Where you say "If that value is not set" what exactly do you mean? not set to true or not set to false ? The wording you've chosen is ambiguous.
                  2. Subsequently you write "If you set that value to false". Is this a juxtaposition with the previous case or are we still in the same case?
                  3. Further on, you say "then you also need to". I take that "also" to mean "in addition to having a log4j.xml configuration file in my war". So I still need that log4j.xml file in my WAR even though the case where "the logging subsystem will look for a log4j.xml configuration" is the other case! (asuming we are in a juxtaposition) Now I am really confused.
                  4. Can you please rephrase for clarity and indicate how to "exclude the log4j dependency the server adds by default.". Is this done presumably by some "jboss-deployment-structure.xm" file? Where is that file placed and can you provide a sample?
                  5. The part where you "advise against using the ConsoleAppender" is very alarming. Why would that be a problem? Is it because of issues in JBoss EAP 6.x logging subsystem or is normal behaviour somehow? Where can one read more on that?
                  • 6. Re: I want to have my own logging. Why I have to disable per-deployment logging?
                    James Perkins Master

                    What exactly are you trying to do? In your other post you refer to using System.out to write messages. Are you including a log4j.xml or log4j.properties file in your deployment? If you're not then log4j becomes irrelevant.

                     

                    With regards to using the ConsoleAppender. The issues is you'd have two different log managers writing to the same output stream. In most cases it should work, it's just not a good practice to have two different objects writing to the same output stream.

                     

                    --

                    James R. Perkins

                    • 7. Re: I want to have my own logging. Why I have to disable per-deployment logging?
                      Menelaus Perdikeas Newbie

                      Thank you for your prompt response. I am trying to understand how logging works in JBoss EAP 6.2 and to understand your answer. I have noticed several weird behaviors. I have managed to configure my own logging using log4j by including a log4j.xml file in WEB-INF/classes. However, the situation is the following:

                      * when log4j.xml is present, any Log4j logging (e.g. via logger.info) ends up in the configured file BUT all `System.out.println` is utterly lost (doesn't even show up in `standalone/log/server.log`)

                      * when log4j.xml is not present, System.out.println ends up in `standalone/log/server.log` but all log4j logging is lost.

                       

                      What I am trying to achieve is to have BOTH of the following:

                      * my log4j logging to show up both in my application-specific file AND in `standalone/log/server.log`

                      AND

                      * all plan System.out.println to show up in `standalone/log/server.log`

                       

                      I am also trying to understand your previous answer on this thread and like I said some things are not super clear in the phrasing you've used.

                      • 8. Re: I want to have my own logging. Why I have to disable per-deployment logging?
                        James Perkins Master

                        * when log4j.xml is present, any Log4j logging (e.g. via logger.info) ends up in the configured file BUT all `System.out.println` is utterly lost (doesn't even show up in `standalone/log/server.log`)

                        There was a big in JBoss EAP 6.2, fixed in 6.2.3, where System.out messages were not getting logged Bug 1070453 – [GSS] (6.2.x) System.out.println() doesn't work when using per-deployment logging .

                         

                        * when log4j.xml is not present, System.out.println ends up in `standalone/log/server.log` but all log4j logging is lost.

                        This seems odd. Using log4j with no log4j configuration should work out of the box. There were some issues with this early on in the EAP 6 releases however. Do you by chance have a dependency that tries to configure log4j for you? I seem to recall seeing issues with Spring doing this occasionally.

                         

                        What I am trying to achieve is to have BOTH of the following:

                        * my log4j logging to show up both in my application-specific file AND in `standalone/log/server.log`

                        AND

                        * all plan System.out.println to show up in `standalone/log/server.log`

                        This could create a problem if you use a log4j configuration file. You don't want two different objects writing to the server.log as they would not be using a shared lock. You could end up with unexpected results if both log4j and JBoss EAP are both writing to the server.log.

                         

                        One option would be to use the logging subsystem and add a handler (you can use a log4j appender as a custom-handler if desired) for your application-specific file. Then you would add a logger with the lowest possible name to the subsystem and add the handler to that logger. For example if you use a pattern like Logger.getLogger(com.example.MyClass.class) to create your log4j loggers you'd just add a logger on the subsystem for com.example.

                         

                        Example CLI commands to add a logger and a file-handler

                        /subsystem=logging/file-handler=my-app:add(append=true, autoflush=true, file={relative-to=jboss.server.log.dir, path=myapp.log})
                        /subsystem=logging/logger=com.example:add(level=INFO, handlers=[my-app])
                        

                         

                        Here is how JBoss EAP 6 handles logging if the logging subsystem is present (which it is by default).

                        1. Logging dependencies are added to deployments. The following modules are added to all deployments
                          • org.jboss.logging
                          • org.apache.log4j
                          • org.apache.commons.logging
                          • org.slf4j
                        2. The deployment is scanned for the following configuration files
                          • log4j.xml
                          • log4j.properties
                          • jboss-log4j.properties
                          • logging.properties
                          • jboss-logging.properties
                        3. If a configuration file is found a new logging context will be created and associated with the deployment. The new logging context will be configured based on the logging configuration file.
                        4. The MANIFEST.MF file checked to see if a logging profile is being used. If it is then the associated.

                         

                        You can circumvent 2 and 3 by adding the org.jboss.as.logging.per-deployment system property and setting it to false. You can also use your own version of log4j if desired

                         

                        One thing to note is JBoss EAP 6 does use a forked version of log4j. The reason for this is so the log4j LogManager will delegate to the JBoss Log Manager so we can have different log contexts. There were some issues with EAP 6.1 and I think 6.2 (it's been a while ). They should be fixed in 6.4 for sure and likely in the 6.2 micro-releases.

                         

                        How to configure logging should be covered in the documentation as well.

                         

                        --

                        James R. Perkins

                        • 9. Re: I want to have my own logging. Why I have to disable per-deployment logging?
                          Menelaus Perdikeas Newbie

                          Thanks a lot! The CLI commands you gave worked and I could see the changes they effected in standalone/configuration/standalone-full.xml. One last question if I may: What is the version of log4j that JBoss EAP 6.2 uses so that I may compile my code against it? The log4j API underwent deep changes from 1.x to 2.x. Looking in `modules/system/layers/base/org/apache/log4j/main/module.xml` there is no mention of the version used.