Read .properties file with slf4j

I want to read data from a .properties file using slf4j, T I can output data to the console, but what I want is to output data to some file, so I need an Appender file for this, which is declared in the .properties file, and I cannot read the .properties file using slf4j.Can any help.

PS: I need an example that explains how to use the .properties file in slf4j and how to initialize the logger factory for this.

+4
source share
3 answers

See http://slf4j.org/faq.html .

SLF4J is only a facade, which means that it does not provide a complete logging solution. Operations such as setting up applications or setting up Registration Levels cannot be performed using SLF4J.

slf4j-simple does not provide additional configuration functions at all.

For other implementations, you should use a way to configure them.

For example, log4j.properties for slf4j-log4j. See http://logging.apache.org/log4j/1.2/manual.html#Configuration .

+4
source

When using log4j, as an alternative, it can be used with the "Log4jLoggerAdapter", defining the configuration in the .properties file. The code is below.

Necessary banks:

slf4j-api-1.7.5.jar slf4j-log4j12-1.7.5.jar If desired the source code (useful when debugging): slf4j-api-1.7.5-sources.jar slf4j-log4j12-1.7.5-sources.jar 

Testing java class:

 import org.apache.log4j.PropertyConfigurator; import org.slf4j.LoggerFactory; import org.slf4j.impl.Log4jLoggerAdapter; public class Slf4j_log4j_main { private static Log4jLoggerAdapter log = (Log4jLoggerAdapter) LoggerFactory.getLogger(Slf4j_log4j_main.class); public static void main(String[] args) { PropertyConfigurator.configure(Slf4j_log4j_main.class.getClassLoader().getResource("basic/log4j.properties")); log.debug( "a debug" ); log.info( "an info" ); log.warn("a warn"); log.error("an error"); //log.fatal("a fatal"); // slf4j misses fatal log. log.trace("a fatal"); System.out.println(""); System.out.println("[INFO]: done"); } } 

Essentials / log4j.properties

 #@FROM: log4j_slf4j.basic #@BASED: [BIN319P17]/[BIN319P42] #using your own named logger. # defining appender file log=/home/alsdias/work/dev/java/lab/slf4j/log4j/log4j_slf4j/src/basic # root logger setup log4j.rootLogger = DEBUG, A1, FILE #setting your own named logger. If more loggers, set additivity false (below) log4j.logger.log4j.level=INFO,A1 log4j.additivity.log4j.level=false # console appender config log4j.appender.A1=org.apache.log4j.ConsoleAppender log4j.appender.A1.layout=org.apache.log4j.PatternLayout # Print the date in ISO 8601 format log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n # file appender config log4j.appender.FILE=org.apache.log4j.FileAppender log4j.appender.FILE.File=${log}/log.out #setting the immediate flush to true (default) log4j.appender.FILE.ImmediateFlush=true #setting the threshold log4j.appender.FILE.Threshold=debug #setting the append to false, overwrite log4j.appender.FILE.Append=false #set a layout for the appender log4j.appender.FILE.layout=org.apache.log4j.PatternLayout log4j.appender.FILE.layout.conversionPattern=%d [%t] %-5p %c - %m%n 

Generated Output:

 2013-06-14 11:47:00,473 [main] DEBUG basic.Slf4j_log4j_main - a debug 2013-06-14 11:47:00,474 [main] INFO basic.Slf4j_log4j_main - an info 2013-06-14 11:47:00,474 [main] WARN basic.Slf4j_log4j_main - a warn 2013-06-14 11:47:00,475 [main] ERROR basic.Slf4j_log4j_main - an error [INFO]: done 
+3
source

slf4j is an API - if you think that it consists only of interfaces and classes, you are not far off.

The behavior you need is an implementation of the interfaces, which for slf4j can be logback, AVSL, JDK14 (java.util.logging), log4j or Simple. Some can read properties, some cannot.

For the magazine you can use

 <property file="src/main/java/chapters/configuration/variables1.properties" /> 

See http://logback.qos.ch/manual/configuration.html#definingProps for more details.

+1
source

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


All Articles