1

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

3
  • I just learned a new word: irrespective. Thanks. Commented Nov 6, 2009 at 10:28
  • Given the discussion below, the question could be phrased as "Can the pattern for an appender be overridden for a particular category?" Commented Nov 6, 2009 at 10:33
  • Do you want to keep the log statements in the same log file as the rest of the log output? Consider putting the log output with a different format in a different file using an appender and filter out all categories except the one above... Commented Nov 6, 2009 at 10:38

2 Answers 2

6

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>
Sign up to request clarification or add additional context in comments.

Comments

2

You need to change your PatternLayout appropriately.

I think %m%n will do the trick.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.