You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WebLogic14x中修改日志格式,仅保留应用日志内容?

WebLogic 14.x 日志精简方案(适配cargo-maven3-plugin 1.9.4)

以下是几种可行的日志精简方案,按需选择:

方案一:修改WebLogic日志配置文件(推荐)

WebLogic 14.x支持通过配置文件自定义日志格式,无需改动应用代码:

  • 找到WebLogic域的config/fmwconfig/servers/[你的服务器名]/logging.xml文件
  • 定位到对应Stdout的console-handler配置,替换其格式化规则,只保留应用日志内容。示例配置片段:
    <log_handler name='console-handler' class='weblogic.logging.ConsoleHandler'>
      <property name='formatter' value='pure-message-formatter'/>
    </log_handler>
    <formatter name='pure-message-formatter' class='weblogic.logging.SimpleFormatter'>
      <property name='format' value='%m%n'/>
    </formatter>
    
  • 若通过cargo-maven3-plugin启动容器,可在插件配置中指定自定义logging.xml,避免修改默认域配置:
    <plugin>
      <groupId>org.codehaus.cargo</groupId>
      <artifactId>cargo-maven3-plugin</artifactId>
      <version>1.9.4</version>
      <configuration>
        <container>
          <containerId>weblogic14x</containerId>
          <home>${weblogic.home}</home>
        </container>
        <configuration>
          <configfiles>
            <configfile>
              <file>${project.basedir}/src/main/resources/custom-logging.xml</file>
              <todir>config/fmwconfig/servers/${weblogic.server.name}</todir>
            </configfile>
          </configfiles>
        </configuration>
      </configuration>
    </plugin>
    

方案二:应用层独立日志框架输出

如果无法修改容器配置,可在应用中改用独立日志框架控制输出:

  • 引入SLF4J+Logback依赖,排除WebLogic自带日志组件避免冲突:
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.36</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.2.11</version>
      <scope>runtime</scope>
    </dependency>
    
  • 配置Logback的ConsoleAppender仅输出日志内容,示例logback.xml:
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
      <encoder>
        <pattern>%msg%n</pattern>
      </encoder>
    </appender>
    <root level="info">
      <appender-ref ref="CONSOLE"/>
    </root>
    

方案三:Maven插件过滤日志输出

以上方案都无法实施时,可通过Maven插件事后处理日志文件:

  • 使用maven-antrun-plugin匹配日志格式,提取纯应用日志内容,示例配置:
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-antrun-plugin</artifactId>
      <version>3.1.0</version>
      <executions>
        <execution>
          <phase>post-integration-test</phase>
          <goals>
            <goal>run</goal>
          </goals>
          <configuration>
            <tasks>
              <replaceregexp file="${project.build.directory}/weblogic-output.log" 
                            match=".*&lt;BEA-000000&gt; &lt;(.*)&gt;" 
                            replace="&lt;\1&gt;" 
                            flags="g"/>
            </tasks>
          </configuration>
        </execution>
      </executions>
    </plugin>
    
  • 注意:该方式为事后处理日志文件,无法实时过滤控制台输出。

内容的提问来源于stack exchange,提问作者jprzystupa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 13:50:46