Using log4j with legacy classes

I am instantiating a log4j object inside a class that inherits most of the methods and attributes of the parent class. Right now I am only receiving log messages from a subclass. How can I get log messages for output in superclass and subclass?

EDIT: The way I'm doing it is that I have an instance variable in both the subclass and the superclass, so it looks something like this:

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 goes 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
source share
2 answers

You will need to make the logger visible in the superclass.

Typically, the setup is to create a static logger variable in each class so that each class has a different level of logging. eg.

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

If you enter a class name instead of ClassName

+1
source

Umm ... actually registering them?

You need to call log4j logging methods ( debug() , info() , error() , etc.) from any method of any class from which you want to log a message. Then you can configure (in your log4j.properties ) which messages will be displayed / logged / go to that log file / etc ... at the class level (if you choose so).

+1
source

Source: https://habr.com/ru/post/1381138/


All Articles