This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Customer Requirements for Developers and have Marcho Behler on-line!
See this thread for details.
The moose likes Other Open Source Projects and the fly likes log4j category Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "log4j category" Watch "log4j category" New topic

log4j category

Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
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 ?

Carol Enderlin
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
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

Joined: Aug 31, 2004
Posts: 225
That helps explain things a bit. The part that confuses me still is seeing, for example, the following in, 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
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
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("")

// Preferred form for retrieving loggers:
Logger logger = Logger.getLogger("")
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 agree. Here's the link:
subject: log4j category
It's not a secret anymore!