Log4j properties file: how to configure?

My procedure for using log4j is this:

  • put the .properties file somewhere in the project folder
  • in the initialization method, which runs only once, PropertyConfigurator.configure is called ("file path")
  • In each method, we must use logger, we define a static logger variable and just call getLogger (class)

It is right? What happens if the initialization module is not defined? Where can I put the "log4j.properties" file in such a way that I don’t need to call propertyconfigurator.configure at all? If this is not possible, is it possible to call PropertyConfigurator.configure ("path_file_file") in each method using the logger?

+5
source share
2 answers

If you put it somewhere in the classpath, Log4J will load it automatically. A typical place for it is the root directory of the source (or resource). Thus, it can be copied to the root directory of your jar (if you create it from your class files).

, . . Maven src/main/resources.

+10

log4j log4j. , log4j .

log4j - -, "/log4j.properties". , , , Loggers, Appenders .. API- log4j. ( , ...)

( log4j "src/main/resources" Maven .)

+3

All Articles