Using log4j with inherited classes

房东的猫 提交于 2019-12-13 18:13:51

问题


I am instantiating a log4j object inside of a class which inherits most of the methods and attributes from a parent class. Right now I'm getting logging messages from the subclass only. How can I get logging messages to output in both the super class and subclass?

EDIT: The way I am do the logging is that I have an instance variable in both the subclass and superclass, so it would look something like the following:

public class SuperClass {
/**
 * Logger for this class
 */
private static final Logger logger = Logger.getLogger(SuperClass.class); 

/* Rest of code goes here */
} // end SuperClass

And the same for the subclass.

My log4j configuration:

## gps log4j.rootLogger==ERROR, clientLog, catissuecoreclientLog

log4j.rootLogger==DEBUG, clientLog, catissuecoreclientLog
### direct messages to file hibernate.log ###

log4j.appender.clientLog=org.apache.log4j.FileAppender
log4j.appender.clientLog.File=./log/client.log
log4j.appender.clientLog.layout=org.apache.log4j.PatternLayout
log4j.appender.clientLog.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

log4j.appender.catissuecoreclientLog=org.apache.log4j.FileAppender
log4j.appender.catissuecoreclientLog.File=./log/catissuecoreclient.log
log4j.appender.catissuecoreclientLog.layout=org.apache.log4j.PatternLayout
log4j.appender.catissuecoreclientLog.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1} - %m%n

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

#log4j.rootLogger=warn, stdout
#log4j.rootLogger=warn, file
#log4j.logger.org.hibernate=info

### log just the SQL
#log4j.logger.org.hibernate.SQL=debug

### log JDBC bind parameters ###
log4j.logger.org.hibernate.type=info

### log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl=debug

### log cache activity ###
#log4j.logger.org.hibernate.cache=debug

### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=debug

### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace

回答1:


You would need to make the logger visible in super class as well.

Normally the setup is to create a logger static variable in each class to allow each class to have different logging level. e.g.

private static final Logger LOG = Logger.getLogger( ClassName.class.getName() );

Where you enter your class's name instead of ClassName




回答2:


Umm... by actually logging them?

You need to invoke log4j's logging methods (debug(), info(), error(), etc...) from whatever method of whatever class you want to log message from. You can then configure (in your log4j.properties) what messages are visible / logged / go to what log file / etc... per class level (if you so choose).



来源:https://stackoverflow.com/questions/1406931/using-log4j-with-inherited-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!