tags:

views:

105

answers:

2

We are using Log4j in our project for logging. I want to log some statements for some of the classes without showing any extra information except from the content, e.g.:

Currently it is like this, if log level is INFO:

05/11/2009 16:54:13 INFO TemplateManagerImpl - Templates in cache:1

I want only the information below, irrespective of any logging level set

Templates in cache:1

+2  A: 

You need to change your PatternLayout appropriately.

I think %m%n will do the trick.

Brian Agnew
I don't want to do it for all the logging , i just want to do it for some of the classes for e.g logging in the MyClass.class should only be changes , rest all classes should log as per normal.
Pratik
You shouldn't need to write your own appender, but you will need to set up a new appender in the log4j config file. You then give this new appender the %m%n layout, and set up a logger for MyClass using this new appender.
Caroline Orr
@Caroline - indeed, you're right. Amended appropriately.
Brian Agnew
+6  A: 

Log4j allows you to configure Layouts, Appenders and Loggers and plug them together in very flexible combinations. A Layout controls what the output will comprise and how its is formatted, an Appender controls how the output is output, and a Logger categorizes where your logging is coming from. By modifying the LogConfig.xml file, you can set up the relationships to do what you want. For example, something along the lines of the following snippet (See the Log4j docs for details):

<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
   <param name="Target" value="System.out"/>
   <param name="Threshold" value="error"/>

   <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%m%n"/>
   </layout>
</appender>

<logger name="org.myclasses.MyClass">
   <level value="debug"/>
   <appender-ref ref="CONSOLE" />
</logger>
alasdairg