2017-09-05 4 views
0

Ich habe eine Java-Standalone-App. mit einem Hauptverfahren, mit diesen 2 Importe:JAVA: Logback in Standalone-Apps konfigurieren

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

und

private static final Logger log = LoggerFactory.getLogger(PecadorDeLaPradera.class); 

Im selben Ordner habe ich auch eine logback.xml aber ich weiß nicht, wie das Programm zu sagen, dass die logback.xml verwendet, um Config das Protokoll

ich keine Klasse ähnlich wie org.apache.log4j.PropertyConfigurator

diesen logback.xml im sa ich finden konnte, Ich Ordner der Java-Klasse, die ich gerade betreibe:

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 

    <!-- trace, debug, info, warn, error, fatal --> 
    <timestamp key="myTimestamp" datePattern="yyyy-MM-dd'_'HH-mm-ss.SSS"/> 


    <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator"> 
     <resetJUL>true</resetJUL> 
    </contextListener> 

    <!-- To enable JMX Management --> 
    <jmxConfigurator/> 

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> 
     <encoder> 
      <pattern>%d{"yyyy-MM-dd HH:mm"} [%thread] %-5level %logger{35} - %msg%n</pattern> 
     </encoder> 
    </appender> 

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> 
     <file>pecador.log</file> 

     <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> 
      <fileNamePattern>handler.%i.log.zip</fileNamePattern>    
      <minIndex>1</minIndex> 
      <maxIndex>3</maxIndex> 
     </rollingPolicy> 

     <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> 
      <maxFileSize>1MB</maxFileSize> 
     </triggeringPolicy> 
     <encoder> 
      <pattern>%d{"yyyy-MM-dd HH:mm"} [%thread] %-5level %logger{35} - %msg%n</pattern> 
     </encoder> 
    </appender> 



    <!-- <logger name="org.springframework.orm.jpa"  level="debug" /> -->  
    <logger name="com.calzada.pecador" level="debug" /> 


    <root level="info"> 
     <appender-ref ref="CONSOLE" /> 
     <appender-ref ref="FILE" /> 
    </root> 
</configuration> 

ich auch die Anwendung ausführen. mit Programm Argumente:

-Dlogback.configurationFile=/Users/calzada/Dev/J2EE/eclipseWSJ2EE/myApp/src/com/calzada/pecador/logback.xml 

und bekam ich diesen Fehler:

java.util.MissingResourceException: Can't find bundle for base name -Dlogback.configurationFile=/Users/nullpointer/Development/J2EE/eclipseWSJ2EE/myApp/src/com/calzada/pecador/logback.xml, locale en_ES 

Die verwendete Bibliothek ist logback-classic-1.2.3.jar

Antwort

1

Hier ist, wie logback selbst konfiguriert:

Logback tries to find a file called logback-test.xml in the classpath.

If no such file is found, logback tries to find a file called logback.groovy in the classpath.

If no such file is found, it checks for the file logback.xml in the classpath.

If no such file is found, service-provider loading facility (introduced in JDK 1.6) is used to resolve the implementation of com.qos.logback.classic.spi.Configurator interface by looking up the file META-INF\services\ch.qos.logback.classic.spi.Configurator in the class path. Its contents should specify the fully qualified class name of the desired Configurator implementation.

If none of the above succeeds, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.

Sie können auch zeigen Loggen Sie sich mit dem Systemparameterbei einer bestimmten Konfigurationsdatei ein. Aus der Dokumentation:

You may specify the location of the default configuration file with a system property named "logback.configurationFile". The value of this property can be a URL, a resource on the class path or a path to a file external to the application.

java -Dlogback.configurationFile=/path/to/config.xml chapters.configuration.MyApp1

Alle oben vom docs genommen.

Entsprechend Ihrer Frage haben Sie eine logback.xml aber diese Datei ist im "selben Ordner" wie Ihre Klasse. Wenn sich Ihre Klasse nicht im Root-Paket befindet, bedeutet dies, dass nicht im Stammverzeichnis des Klassenpfads enthalten ist, sodass Logback sie nicht erkennt. Damit Logback konfigurieren sich aus dieser Datei, die Sie eine der folgenden tun können:

  • Legen Sie Ihre logback.xml in der Wurzel Ihrer Classpath (für ein Maven Projekt dieser ist so einfach wie das Kopieren von logback.xml-src/main/resources)
  • Führen Sie Ihr Java-Programm mit -Dlogback.configurationFile=/path/to/logback.xml