简体   繁体   中英

Display only selected Log4j debug statements

Is it possible to display only those statements in console, which are having certain words.

For 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.

You want to use the log4j StringMatchFilter which is part of the "extras" package from apache logging.

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). The Log4J configuration can be set with different levels for each logger.

You can read more here

One could use a tool to filter out messages. For Windows you could do this with BareTail . Filtering of messages is only possible with the pro (paid) version. Perhaps there are others tools that do the same job.

SLF4J markers are perhaps suitable for your purposes. All printing methods such as debug and info in org.slf4j.Logger admit a Marker as a first parameter . Moreover, logback-classic, a native SLF4J implementation, ships with a filter called MarkerFilter which I think does what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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