Log4j initialization for standalone application

I am new to log4j. This is what I have. I have about 20 files in different packages in STAND ALONE JAVA APPLICATION. I am trying to use and write log files.

Below is my log4j.properties file, which is in my class:

log4j.appender.R = org.apache.log4j.DailyRollingFileAppender log4j.appender.R.File = /ParentFolder/ChildFolder/application.log log4j.appender.R.Append = true log4j.appender.R.DatePattern = '.'yyy-MM-dd log4j.appender.R.layout = org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n 

Below is the log initialization code in my main method

 final String LOG_FILE = "C:/eclipse_workspace/lib/log4j.properties"; Properties logProp = new Properties(); try { logProp.load(new FileInputStream (LOG_FILE)); PropertyConfigurator.configure(logProperties); logger.info("Logging enabled"); } catch(IOException e) { System.out.println("Logging not enabled"); } 

In each java application class I have the following code

 import org.apache.log4j.*; private static final Logger logger = Logger.getLogger(TheActualClassName.class); 

But when the application starts , the following warning messages appear.

log4j: WARN No add-ons were found for the logger (com.xxx.myApp.MainProgram.MyFileName). log4j: WARN Please initialize the log4j system correctly. log4j: WARN For more information, see http://logging.apache.org/log4j/1.2/faq.html#noconfig .

What am I doing wrong? The log file "application.log" is not created

+6
source share
3 answers

The following line may be required:

 # Set root logger level to INFO and appender to R. log4j.rootLogger=INFO, R 

The root log is always available and does not have a name.

Since version 1.2.7, log4j (with the LogManager class), first looks for log4j.xml in the classpath. If log4j.xml does not exist, then log4j (with the LogManager class) looks for log4j.properties in the classpath.

+6
source

If you intend to use a file called log4j.properties , and this is on the way to the application class, there is no need to even call PropertyConfiguration or DOMConfigurator - DOMConfigurator will do this automatically when it is initialized first (when the registrar is first loaded).

The error message seems to indicate that your configuration is not loading.

Add the VM argument -Dlog4j.debug to your application so that -Dlog4j.debug whole bunch of startup information that includes the files it tries to load and what values ​​it finds in the configuration.

+4
source

Raghu, if you use a standalone configuration to configure log4j properties, then use the BasicConfigurator.configure () method to solve the problem with your applications.

0
source

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


All Articles