简体   繁体   English

仅显示选定的Log4j调试语句

[英]Display only selected Log4j debug statements

Is it possible to display only those statements in console, which are having certain words. 是否可以仅在控制台中显示具有特定单词的那些语句。

For eq: 对于eq:

   logger.debug ( "java: hello " );
   logger.debug ( "groovy: hello " );
   logger.debug ( "ruby: hello " );

Now, by doing some configuration or whatever, all statements which are starting with groovy: should display. 现在,通过执行某些配置或其他任何操作,应显示以groovy开头的所有语句。

You want to use the log4j StringMatchFilter which is part of the "extras" package from apache logging. 您想使用log4j StringMatchFilter ,它是来自apache日志记录的“extras”包的一部分。

Here is a quick example found online : 这是在线发现的一个简单示例:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  <appender name="CustomAppender" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="custom.log"/>
    <param name="Append" value="true"/>
    <param name="MaxFileSize" value="5000KB"/>
    <param name="maxBackupIndex" value="5"/> 
          <layout class="org.apache.log4j.PatternLayout">
                  <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p - %m%n" />
          </layout>

          <filter class="org.apache.log4j.varia.StringMatchFilter">
                  <param name="StringToMatch" value="Here is DEBUG" />
                  <param name="AcceptOnMatch" value="true" />
          </filter>

          <filter class="org.apache.log4j.varia.DenyAllFilter"/>
  </appender>

  <root>
    <appender-ref ref="CustomAppender"/>
  </root>
</log4j:configuration>

What about create your customs Levels 如何创建您的习俗级别

public class Groovy extends Level

And then in the log properties file set those levels as your configuration 然后在日志属性文件中将这些级别设置为您的配置

Hope this helps, David. 大卫,希望这会有所帮助。

One can use different loggers (say a logger for "java" messages and one for "groovy" messages). 可以使用不同的记录器(比如说“java”消息的记录器和“groovy”消息的记录器)。 The Log4J configuration can be set with different levels for each logger. 可以为每个记录器设置不同级别的Log4J配置。

You can read more here 你可以在这里阅读更多

One could use a tool to filter out messages. 可以使用工具来过滤掉消息。 For Windows you could do this with BareTail . 对于Windows,您可以使用BareTail执行此操作 Filtering of messages is only possible with the pro (paid) version. 只有pro(付费)版本才能过滤消息。 Perhaps there are others tools that do the same job. 也许还有其他工具可以完成同样的工作。

SLF4J markers are perhaps suitable for your purposes. SLF4J标记可能适合您的用途。 All printing methods such as debug and info in org.slf4j.Logger admit a Marker as a first parameter . org.slf4j.Logger所有打印方法(如debug和info) org.slf4j.Logger 允许将Marker作为第一个参数 Moreover, logback-classic, a native SLF4J implementation, ships with a filter called MarkerFilter which I think does what you want. 此外,logback-classic是一个原生SLF4J实现,附带一个名为MarkerFilter的过滤器,我认为它MarkerFilter您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM