I\'m currently writing a big project in java, with numerous classes, some classes are quiet small, that simply represent objects with only few methods. I have a logger set i
The best way to do it is to have each class have it's own logger (named after the class), then set up your configuration so that they all append to the same appender.
For example:
class A {
private static final Logger log = Logger.getLogger(A.class);
}
class B {
private static final Logger log = Logger.getLogger(B.class);
}
Then your log4j.properties can look like the example in the log4j documentation:
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
Both A and B will log to the root logger and hence to the same appender (in this case the console).
This will give you what you want: each class is independent but they all write to the same log. You also get the bonus feature that you can change the logging level for each class in the log4j configuration.
As an aside, you might want to consider moving to slf4j if the project is still in early development. slf4j has some enhancements over log4j that make it a little easier to work with.