tags:

views:

460

answers:

5

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.

A: 

Hi, 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.

David Santamaria
Thanks David, but really, i don' want anything like that.
A: 

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

abyx
+4  A: 

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/"&gt;
  <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>
Alex B
It sounds interesting, will you please elaborate?
A: 

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.

kgiannakakis
A: 

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. Morever, logback-classic, a native SLF4J implementation, ships with a filter called MarkerFilter which I think does what you want.

Ceki