• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

log4j category

Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please explain what a log4j category is? I have seen these in the log4j config file. It looks like somehow you can assign an appender to a category.

Does a category define an appender for a particluar package? For example, com.myweb.dao ?

Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a bunch of documentation on the Log4j site including short log4j manual. Your questions seem simple, but some background is needed.

The Category has been replaced by Logger (Category is deprecated), but lots of teams are still using Category. In code, a Logger/Category is used to log debug, error (etc.) messages, see code snippet in which I assume log4j is configured by a properties or xml file.

When Loggers are obtained using the fully qualified class name (e.g. using Logger.getLogger(MyClass.class)) then there is an implied hierarchy of Loggers based on the package name. There are other ways to name Loggers, but this is a common one.

You have probably seen that in your configuration file a category (aka Logger) can be configured/controlled as an implied logger, meaning you create Loggers for these:


and can configure these implied Loggers (implied because you didn't create a Logger with Logger.getLogger("com.myweb.dao") or Logger.getLogger("com.myweb") :

or com.myweb

From log4j manual, the Logger hierarchy uses dot notation:

Loggers are named entities. Logger names are case-sensitive and they follow the hierarchical naming rule:

Named Hierarchy

A logger is said to be an ancestor of another logger if its name followed by a dot is a prefix of the descendant logger name. A logger is said to be a parent of a child logger if there are no ancestors between itself and the descendant logger.

As you indicated appenders can be associated with Categories/Loggers (also check out the root logger/category), usually using the hierarchy. The level (error, warn, debug, etc) of messages can be controlled by the Logger and/or Appender configuration.

Still have questions? Read some of the documentation and then come back here and ask for clarification.
Andy Hahn
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That helps explain things a bit. The part that confuses me still is seeing, for example, the following in log4j.properties:

log4j.logger.org.apache=WARN, stdout

If a category == logger, and category is deprecated then why is "category" being used all over log4j property files? I have read both of the log4j manuals and there is no talk about categories.

Still confused...
Carol Enderlin
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right now use of Category is discouraged. It still works though. Yes it's confusing but they thought it was worth the confusion to make log4j terminology more like the new java.util.logging terminology.

From log4j short manual,

Logger hierarchy
The first and foremost advantage of any logging API over plain System.out.println resides in its ability to disable certain log statements while allowing others to print unhindered. This capability assumes that the logging space, that is, the space of all possible logging statements, is categorized according to some developer-chosen criteria. This observation had previously led us to choose category as the central concept of the package. However, since log4j version 1.2, Logger class has replaced the Category class. For those familiar with earlier versions of log4j, the Logger class can be considered as a mere alias to the Category class.

From the
Java documentation on Category class:

public class Category
extends Object
implements AppenderAttachable
This class has been deprecated and replaced by the Logger subclass. It will be kept around to preserve backward compatibility until mid 2003. In the 1.3 alpha 6 javadoc this line has been change to end, "until such time as the Log4j team sees fit to remove it".

Logger is a subclass of Category, i.e. it extends Category. In other words, a logger is a category. Thus, all operations that can be performed on a category can be performed on a logger. Internally, whenever log4j is asked to produce a Category object, it will instead produce a Logger object. Log4j 1.2 will never produce Category objects but only Logger instances. In order to preserve backward compatibility, methods that previously accepted category objects still continue to accept category objects.

For example, the following are all legal and will work as expected.

// Deprecated form:
Category cat = Category.getInstance("foo.bar")

// Preferred form for retrieving loggers:
Logger logger = Logger.getLogger("foo.bar")
The first form is deprecated and should be avoided.

There is absolutely no need for new client code to use or refer to the Category class. Whenever possible, please avoid referring to it or using it.

See the short manual for an introduction on this class.

See the document entitled preparing for log4j 1.3 for a more detailed discussion.

[ April 14, 2005: Message edited by: Carol Enderlin ]
I knew I would regret that burrito. But this tiny ad has never caused regrets:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic