java 在 JBoss 7.1.1 下使用应用程序的 Log4J 配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14182257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:23:31  来源:igfitidea点击:

Using application's Log4J configuration under JBoss 7.1.1

javaloggingjbosslog4jjboss7.x

提问by RedEagle

I'm am having trouble logging using my appenders defined on my XML log4j configuration file.

我在使用 XML log4j 配置文件中定义的附加程序进行日志记录时遇到问题。

I created the jboss-deployment-structure.xmlon my EAR's META-INF folder with no sucess.

我在EAR 的 META-INF 文件夹中创建了jboss-deployment-structure.xml,但没有成功。

The jboss-deployment-structure.xmlstructure is:

JBoss的部署,structure.xml结构为:

<jboss-deployment-structure>
<ear-subdeployments-isolated>false</ear-subdeployments-isolated>
    <deployment>
        <exclusions>
            <module name="org.apache.log4j" slot="main"/>
        </exclusions>
    </deployment>
</jboss-deployment-structure>

I have even tried to edit my standalone.conf.batfile adding the following line:

我什至尝试编辑我的standalone.conf.bat文件,添加以下行:

set "JAVA_OPTS=%JAVA_OPTS% -Dorg.jboss.as.logging.per-deployment=false"

My application deployment is like this:

我的应用部署是这样的:

-> MyAppEAR.ear
   -> META-INF
      -> MANIFEST.MF
   -> MyAoo.war
      -> META-INF
         -> MANIFEST.MF
         -> jboss-deployment-structure.xml
      -> WEB-INF
         -> web.xml
         -> lib
            -> log4j-1.2.17.jar
            -> ---
         -> classes
            -> log4j.xml
            -> ...

I've noticed the following error:

我注意到以下错误:

  • jboss-deployment-structure.xml in subdeployment ignored. jboss-deployment-structure.xml is only parsed for top level deployments.
  • 忽略子部署中的 jboss-deployment-structure.xml。jboss-deployment-structure.xml 仅针对顶级部署进行解析。

I even tried migrating from JBOSS 7.1.0 to 7.1.1

我什至尝试从 JBOSS 7.1.0 迁移到 7.1.1

Some help please!

请一些帮助!

Thanks

谢谢



Thanks for the repply James.

感谢詹姆斯的回复。

I did what you said and moved the jboss-deployment-structure.xmlfile to MyAppEAR.ear/META-INF.

我做了你所说的并将jboss-deployment-structure.xml文件移动到MyAppEAR.ear/META-INF

I've noticed that this way the exception:

我注意到这种方式异常:

jboss-deployment-structure.xml in subdeployment ignored. jboss-deployment-structure.xml is only parsed for top level deployments.

... doesn't occur. I don't know if that means that the file was parsed... how can I tell?

......不会发生。我不知道这是否意味着文件被解析了......我怎么知道?

Despite of this new behaviour my log4j.xmlconfiguration file still isn't loaded and the logger used still is Log4J's.

尽管有这种新行为,我的log4j.xml配置文件仍然没有加载,并且使用的记录器仍然是 Log4J 的。

I know this becaused I wrote to the console:

我知道这是因为我写信给控制台:

System.out.println(Logger.getRootLogger().getClass().toString())

...and got:

...并得到:

class org.jboss.logmanager.log4j.BridgeLogger

I've also tried:

我也试过:

  • moving my log4j.xmlto MyAppEAR.ear/META-INF.
  • removing the unecessary -Dorg.jboss.as.logging.per-deployment=falsefrom standalone.conf.bat
  • removing slotfrom my jboss-deployment-structure.xml
  • 将我的log4j.xml移动到MyAppEAR.ear/META-INF
  • standalone.conf.bat中删除不必要的-Dorg.jboss.as.logging.per-deployment=false
  • 从我的jboss-deployment-structure.xml 中删除插槽

Any more ideas?

还有更多想法吗?

Thanks

谢谢

采纳答案by gYanI

Hi RedEagle see the following configuration which i have tested and its working fine...
Step-1Create a new module as

嗨 RedEagle 看到我测试过的以下配置并且它工作正常......
Step-1创建一个新模块作为

jboss-as-7.1.1.Final/modules/com/company/mylog/main/

jboss-as-7.1.1.Final/modules/com/company/mylog/main/

                          -module.xml
                          -log4j-1.2.14.jar

Content of  module.xml


 <?xml version="1.0" encoding="UTF-8"?>
      <module xmlns="urn:jboss:module:1.0" name="com.company.mylog">
    <resources>
      <resource-root path="log4j-1.2.14.jar"/>
    </resources>
    <dependencies>
      <module name="javax.api"/>
         </dependencies>
      </module>

Step-2Now IN my.ear/META-INF/

第 2 步现在在 my.ear/META-INF/

          -jboss-deployment-structure.xml
          -MANIFEST.MF

content of jboss-deployment-structure.xml

jboss-deployment-structure.xml 的内容

 <jboss-deployment-structure>
              <deployment>

                  <exclusions>
                  <module name="org.apache.log4j" />
                  </exclusions>

              </deployment>


              <sub-deployment name="MyWeb.war">
                    <exclusions>
                    <module name="org.apache.log4j" />
                    </exclusions>
              </sub-deployment>

              <sub-deployment name="MyBeans.jar">
                <exclusions>
                  <module name="org.apache.log4j" />
                </exclusions>
              </sub-deployment>


              </jboss-deployment-structure>

Content of MANIFEST.MF

MANIFEST.MF 的内容

        Manifest-Version: 1.0
        Dependencies: com.company.mylog

Step-3Content of MyLogger.java

MyLogger.java 的Step-3内容

public static Logger getLogger(String name) {

    Logger  logger= LogManager.getLogger(name);
    PropertyConfigurator.configure("log4j.properties"); //Path to log4j.properties as many option available in my case for testing i used static path /home/gyani/log4j.properties
    return logger;
}

Step-4Here is log4j.properties

步骤 4这是 log4j.properties

log4j.rootLogger=info,gyani
log4j.appender.gyani=org.apache.log4j.RollingFileAppender
log4j.appender.gyani.File=/home/gyani/myserverlog.log
log4j.appender.gyani.Append=true
log4j.appender.gyani.MaxFileSize=100000KB
log4j.appender.gyani.MaxBackupIndex=10
log4j.appender.gyani.layout=org.apache.log4j.PatternLayout
log4j.appender.gyani.layout.ConversionPattern=[%d{MMM d HH:mm:ss yyyy}] [%-5p] [%c]: %m%n

回答by James R. Perkins

Your jboss-deployment-structure.xmlneeds to be in your MyAppEAR.ear/META-INFdirectory. There is also no need for the slotattribute for this case.

jboss-deployment-structure.xml需要在您的MyAppEAR.ear/META-INF目录中。slot这种情况下也不需要属性。

Also the -Dorg.jboss.as.logging.per-deployment=falseisn't used in JBoss AS 7.1.1.Final, but it doesn't hurt anything either.

另外,-Dorg.jboss.as.logging.per-deployment=false是不是JBoss AS中7.1.1.Final使用,但它不会伤害任何东西。