Java 在 Wildfly 8.2 中配置日志的正确方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30476523/
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-08-11 09:41:54  来源:igfitidea点击:

Right way to configure the log in Wildfly 8.2

javaloggingjbosswildflywildfly-8

提问by Vijay Veeraraghavan

I have confusion in setting up the log in Wildfly-8.2.0. Initially I had used my own logging system, with log4j.xml built into the WAR file, all worked very well. But, when I make any changes to the log configuration I need to redeploy the app to make the changes effect. So, I switched to the JBoss logger sub system. The below is configuration I did to the standalone.xmlfrom the jboss-cli

我在 Wildfly-8.2.0 中设置日志时感到困惑。最初我使用自己的日志系统,将 log4j.xml 内置到 WAR 文件中,一切都运行良好。但是,当我对日志配置进行任何更改时,我需要重新部署应用程序以使更改生效。所以,我切换到 JBoss 记录器子系统。下面是配置我做的standalone.xmljboss-cli

/subsystem=logging/custom-handler=myplatform:add(class=org.apache.log4j.RollingFileAppender, module=org.jboss.log4j.logmanager, formatter="%d{.yyyy-MM-dd} %-5p [%c] (%t) %s%E%n", properties={MaxFileSize=1024000,maxBackupIndex=20,file="${jboss.server.log.dir}/myplatform-debug.log"})

so it added the below configuration in standalone.xml

所以它添加了以下配置 standalone.xml

            <custom-handler name="example" class="org.apache.log4j.RollingFileAppender" module="org.jboss.log4j.logmanager">
                <formatter>
                    <pattern-formatter pattern="%d{.yyyy-MM-dd} %-5p [%c] (%t) %s%E%n"/>
                </formatter>
                <properties>
                    <property name="MaxFileSize" value="1024000"/>
                    <property name="maxBackupIndex" value="20"/>
                    <property name="file" value="${jboss.server.log.dir}/ott-platform-log.log"/>
                </properties>
            </custom-handler>

And then a logger for this

然后是一个记录器

<logger category="com.mycompany.project.module1">
  <level name="DEBUG"/>
    <handlers>
      <handler name="myplatform"/>
    </handlers>
</logger>

All works well, but all my application logs are logged into the server log too. And, in the console log too. I don't want this to happen, after all I have configured the logger separately for my project! How do I stop the server log my logs logging into the server.log? Or is there a way to use an appender for this? If so how?

一切正常,但我所有的应用程序日志也都记录到服务器日志中。而且,在控制台日志中也是如此。我不希望这种情况发生,毕竟我已经为我的项目单独配置了记录器!如何停止服务器记录我的日志登录到 server.log?或者有没有办法为此使用附加程序?如果是这样怎么办?

采纳答案by Manu

From the "clean" standalone.xmlI do the following:

从“干净”standalone.xml我执行以下操作:

  1. Add a handler to the console:
  1. 向控制台添加处理程序:
<profile>
    <subsystem xmlns="urn:jboss:domain:logging:2.0">
        ...
        <console-handler name="CONSOLE_HANDLER">
            <level name="DEBUG"/>
            <formatter>
                <named-formatter name="ECLIPSE_PATTERN"/>
            </formatter>
        </console-handler>
        ...
  1. If you wish a log file:
  1. 如果你想要一个日志文件:
<profile>
    <subsystem xmlns="urn:jboss:domain:logging:2.0">
       ...
       <periodic-rotating-file-handler name="MI_PROJECT_FILE_HANDLER" autoflush="true">
            <formatter>
                <named-formatter name="ECLIPSE_PATTERN"/>
            </formatter>
            <file relative-to="jboss.server.log.dir" path="myProject.log"/>
            <suffix value=".yyyy-MM-dd"/>
            <append value="true"/>
        </periodic-rotating-file-handler>
       ...
  1. The logger (same level as 1 and 2) notice the use-parent-handlers
  1. 记录器(与 1 和 2 级别相同)注意到use-parent-handlers
<logger category="com.company.project" use-parent-handlers="false">
    <level name="DEBUG"/>
    <handlers>
        <handler name="MI_PROJECT_FILE_HANDLER"/>
        <handler name="CONSOLE_HANDLER"/>
    </handlers>
</logger>
  1. I've used a custom pattern (same level):
  1. 我使用了自定义模式(相同级别):
<formatter name="ECLIPSE_PATTERN">
    <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
</formatter> 
  1. Keep sure this:
  1. 确保这一点:
<root-logger>
    <level name="INFO"/>
    <handlers>
        <handler name="CONSOLE"/>
        <handler name="FILE"/>
    </handlers>
</root-logger>