This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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") :
com.myweb.dao 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:
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.
Joined: Aug 31, 2004
That helps explain things a bit. The part that confuses me still is seeing, for example, the following in log4j.properties: log4j.category.org.apache=WARN 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.
Joined: Oct 10, 2000
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.
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.