提问者:小点点

JBoss/Wildfly - 安全高效的日志过滤 - 多个处理程序记录到同一个文件


将过滤器表达式应用于始终以相同类别出现的日志消息的最安全和最有效的方法是什么?

我在一个容器中有超过100个应用程序记录到同一个文件。我想要处理的消息非常具体。对每条消息应用一个复杂的过滤器规范看起来开销很大,所以我决定为我要过滤的每一类消息创建单独的记录器。每个记录器都有自己的处理程序和自己的过滤器规范。这样,我将只对尽可能少的日志消息应用过滤逻辑。

以下是独立.xml中的记录器:

<root-logger>
    <level name="INFO"/>
    <handlers>
        <handler name="CONSOLE"/>
        <handler name="FILE"/>
    </handlers>
</root-logger>
<logger category="org.hibernate.engine.jdbc.spi.SqlExceptionHelper" use-parent-handlers="false">
    <level name="INFO"/>
    <handlers>
        <handler name="CONSOLE"/>
        <handler name="SQL_EXECUTION_HELPER_FILE"/>
    </handlers>
</logger>
<logger category="org.jboss.as.ejb3.invocation" use-parent-handlers="false">
    <level name="INFO"/>
    <handlers>
        <handler name="CONSOLE"/>
        <handler name="EJB3_INVOCATION_FILE"/>
    </handlers>
</logger>

以下是文件处理程序:

<periodic-rotating-file-handler name="FILE" autoflush="true">
    <formatter>
        <named-formatter name="PATTERN"/>
    </formatter>
    <file relative-to="jboss.server.log.dir" path="server.log"/>
    <suffix value=".yyyy-MM-dd"/>
    <append value="true"/>
</periodic-rotating-file-handler>
<periodic-rotating-file-handler name="SQL_EXECUTION_HELPER_FILE">
    <filter-spec value="any(  not(match(&quot;ERROR: duplicate key value violates unique constraint \&quot;option_option_expiry_id_option_type_id_strike_price_key\&quot;&quot;)),  all(  match(&quot;ERROR: duplicate key value violates unique constraint \&quot;option_option_expiry_id_option_type_id_strike_price_key\&quot;&quot;),  levelChange(WARN)  )  )"/>
    <formatter>
        <named-formatter name="PATTERN"/>
    </formatter>
    <file relative-to="jboss.server.log.dir" path="server.log"/>
    <suffix value=".yyyy-MM-dd"/>
    <append value="true"/>
</periodic-rotating-file-handler>
<periodic-rotating-file-handler name="EJB3_INVOCATION_FILE">
    <filter-spec value="any(  not(match(&quot;EJB Invocation failed on component OptionProductManagementDataService for method public void com.nodalexchange.optionproductmanagement.OptionProductManagementDataService.insert&quot;)),  all(  match(&quot;EJB Invocation failed on component OptionProductManagementDataService for method public void com.nodalexchange.optionproductmanagement.OptionProductManagementDataService.insert&quot;),  levelChange(WARN)  )  )"/>
    <formatter>
        <named-formatter name="PATTERN"/>
    </formatter>
    <file relative-to="jboss.server.log.dir" path="server.log"/>
    <suffix value=".yyyy-MM-dd"/>
    <append value="true"/>
</periodic-rotating-file-handler>

我不明白这些文件处理程序如何在后台工作,例如它们是共享文件描述符还是各自打开自己的文件描述符。这对我来说一直很好,除了有一个事件,我只从单个处理程序获取消息,而没有其他消息,这导致我相信使用多个处理程序是不安全的,但我无法重现这一点。

  1. 多个处理程序写入同一文件时,文件是否有损坏的风险
  2. 有更好的方法吗

共1个答案

匿名用户

我强烈建议不要使用多个处理程序写入同一个文件。这肯定会有问题。

因为您正在定义您想要查看过滤器的特定记录器,所以您应该将过滤器放在这些记录器上。