Cara mengkonfigurasi slf4j-sederhana

api 1.7 dan slf4j-sederhana sebagai implementasi. Aku hanya bisa't menemukan cara untuk mengkonfigurasi logging tingkat dengan kombinasi ini.

Adakah yang bisa membantu?

Mengomentari pertanyaan (1)
Larutan

It's baik melalui sistem properti

-Dorg.slf4j.simpleLogger.defaultLogLevel=debug

atau simplelogger.properties file pada classpath

lihat http://www.slf4j.org/api/org/slf4j/impl/SimpleLogger.html untuk rincian

Komentar (4)

Ini adalah contoh simplelogger.sifat yang dapat anda tempatkan di classpath (tanda sifat yang ingin anda gunakan):

# SLF4J's SimpleLogger configuration file
# Simple implementation of Logger that sends all enabled log messages, for all defined loggers, to System.err.

# Default logging detail level for all instances of SimpleLogger.
# Must be one of ("trace", "debug", "info", "warn", or "error").
# If not specified, defaults to "info".
#org.slf4j.simpleLogger.defaultLogLevel=info

# Logging detail level for a SimpleLogger instance named "xxxxx".
# Must be one of ("trace", "debug", "info", "warn", or "error").
# If not specified, the default logging detail level is used.
#org.slf4j.simpleLogger.log.xxxxx=

# Set to true if you want the current date and time to be included in output messages.
# Default is false, and will output the number of milliseconds elapsed since startup.
#org.slf4j.simpleLogger.showDateTime=false

# The date and time format to be used in the output messages.
# The pattern describing the date and time format is the same that is used in java.text.SimpleDateFormat.
# If the format is not specified or is invalid, the default format is used.
# The default format is yyyy-MM-dd HH:mm:ss:SSS Z.
#org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss:SSS Z

# Set to true if you want to output the current thread name.
# Defaults to true.
#org.slf4j.simpleLogger.showThreadName=true

# Set to true if you want the Logger instance name to be included in output messages.
# Defaults to true.
#org.slf4j.simpleLogger.showLogName=true

# Set to true if you want the last component of the name to be included in output messages.
# Defaults to false.
#org.slf4j.simpleLogger.showShortLogName=false
Komentar (2)

Anda dapat secara programatik perubahan itu dengan menetapkan sistem properti:

public class App {

    public static void main(String[] args) {

        System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "TRACE");

        final org.slf4j.Logger log = LoggerFactory.getLogger(App.class);

        log.trace("trace");
        log.debug("debug");
        log.info("info");
        log.warn("warning");
        log.error("error");

    }
}

Log tingkat KESALAHAN > MEMPERINGATKAN > INFO > DEBUG > JEJAK.

Harap dicatat bahwa setelah logger adalah menciptakan tingkat log dapat't dapat diubah. Jika anda perlu untuk secara dinamis mengubah penebangan tingkat anda mungkin ingin menggunakan log4j dengan SLF4J.

Komentar (6)

Saya melihat bahwa Eemuli mengatakan bahwa anda dapat't mengubah tingkat log setelah mereka diciptakan - dan sementara itu mungkin desain, isn't sepenuhnya benar.

Aku berlari ke sebuah situasi di mana saya menggunakan perpustakaan yang masuk ke slf4j - dan saya menggunakan perpustakaan saat menulis maven mojo plugin.

Maven menggunakan (hacked) versi slf4j SimpleLogger, dan aku tidak mampu untuk mendapatkan kode plugin untuk mengalihkan para logging untuk sesuatu seperti log4j, yang bisa saya kontrol.

Dan saya dapat't mengubah maven penebangan config.

Jadi, untuk tenang beberapa berisik pesan info, saya menemukan saya bisa menggunakan refleksi seperti ini, untuk futz dengan SimpleLogger pada saat runtime.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.spi.LocationAwareLogger;
    try
    {
        Logger l = LoggerFactory.getLogger("full.classname.of.noisy.logger");  //This is actually a MavenSimpleLogger, but due to various classloader issues, can't work with the directly.
        Field f = l.getClass().getSuperclass().getDeclaredField("currentLogLevel");
        f.setAccessible(true);
        f.set(l, LocationAwareLogger.WARN_INT);
    }
    catch (Exception e)
    {
        getLog().warn("Failed to reset the log level of " + loggerName + ", it will continue being noisy.", e);
    }

Tentu saja, catatan, ini isn't sangat stabil / solusi yang dapat diandalkan... karena akan masuk waktu berikutnya maven orang-orang mengubah mereka logger.

Komentar (0)