read .properties file using slf4j

前端 未结 3 1626
南笙
南笙 2021-01-05 23:39

I want to read data from .properties file using slf4j,T i am able to output the data on console but what i want is to output the data on some file so i need file Appender fo

相关标签:
3条回答
  • 2021-01-05 23:43

    If using log4j, alternatively it can be used "Log4jLoggerAdapter", defining the configuration on a .properties file. Code below.

    The required jars:

    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
    

    The 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");
        }
    }
    

    The basic/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
    

    The 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
    
    0 讨论(0)
  • 2021-01-05 23:48

    slf4j is an API - if you consider it to consist only of interfaces and no classes, you are not far off.

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

    For logback you can use

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

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

    0 讨论(0)
  • 2021-01-05 23:58

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

    SLF4J is only a facade, meaning that it does not provide a complete logging solution. Operations such as configuring appenders or setting logging levels cannot be performed with SLF4J.

    slf4j-simple doesn't provide extra configuration features at all.

    For other implementations you should use the way to configure they provide.

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

    0 讨论(0)
提交回复
热议问题