如何配置slf4j-simple

API 1.7和slf4j-简单的实现。我只是找不到如何使用这个组合配置日志级别。

有人能帮忙吗?

245629 次浏览

要么是通过系统属性

-Dorg.slf4j.simpleLogger.defaultLogLevel=debug

simplelogger.properties文件在类路径上

详见https://www.slf4j.org/api/org/slf4j/simple/SimpleLogger.html

你可以通过设置system属性来改变它:

public class App {
public static void main(String[] args) {
// for the code below to work, it must be executed before the
​// logger is created. see note below
System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "TRACE");
       

​org.slf4j.Logger log = LoggerFactory.getLogger(App.class);
​
​log.trace("trace");
​log.debug("debug");
​log.info("info");
​log.warn("warning");
​log.error("error");
​}
​}

日志级别为ERROR >WARN祝辞INFO祝辞DEBUG祝辞TRACE

请注意,一旦创建了日志记录器,日志级别就不能更改。如果你需要动态更改日志级别,你可能想要使用SLF4J的log4j

这是一个示例simplelogger.properties,你可以把它放在类路径上(取消你想使用的属性的注释):

# 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

在Maven或Gradle项目中,“在类路径上”有一个方便的位置;src/main/resources/simplelogger.properties

我注意到Eemuli说在创建日志级别后不能更改它们——虽然这可能是设计,但并不完全正确。

我遇到了这样一种情况:我正在使用一个登录到slf4j的库——而且我在写maven mojo插件时正在使用这个库。

Maven使用slf4j SimpleLogger的(黑客)版本,我无法让我的插件代码将其日志重新路由到像log4j这样的东西,这是我可以控制的。

而且我不能更改maven日志记录配置。

因此,为了使一些嘈杂的信息消息安静下来,我发现我可以使用这样的反射,在运行时使用SimpleLogger。

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

当然,请注意,这不是一个非常稳定/可靠的解决方案……因为它将在下一次maven人员更改记录器时崩溃。

我不知道为什么。我使用simplelogger.propertiesorg.slf4j.simpleLogger.showDatetime,它不工作。

我查找了SimpleLogger类源代码,得到了这部分代码

static {
// Add props from the resource simplelogger.properties
InputStream in = (InputStream)AccessController.doPrivileged(
new PrivilegedAction() {
public Object run() {
ClassLoader threadCL = Thread.currentThread().getContextClassLoader();
if (threadCL != null) {
return threadCL.getResourceAsStream(CONFIGURATION_FILE);
} else {
return ClassLoader.getSystemResourceAsStream(CONFIGURATION_FILE);
}
}
});
if(null != in) {
try {
simpleLoggerProps.load(in);
in.close();
} catch(java.io.IOException e) {
// ignored
}
}


showLogName    = getBooleanProperty(systemPrefix + "showlogname",      showLogName);
showShortName  = getBooleanProperty(systemPrefix + "showShortLogname", showShortName);
showDateTime   = getBooleanProperty(systemPrefix + "showdatetime",     showDateTime);
showThreadName = getBooleanProperty(systemPrefix + "showthreadname",   showThreadName);
dateTimeFormat = getStringProperty(systemPrefix + "dateTimeFormat",    dateTimeFormat);


if(showDateTime) {
try {
dateFormatter = new SimpleDateFormat(dateTimeFormat);
} catch(IllegalArgumentException e) {
Util.report("Bad date format in " + CONFIGURATION_FILE + "; reverting to default", e);
// If the format pattern is invalid - use the default format
dateTimeFormat = DEFAULT_DATE_TIME_FORMAT;
dateFormatter = new SimpleDateFormat(dateTimeFormat);
}
}
}

systemPrefix + "showdatetime"org.slf4j.simplelogger.showdatetime 当我尝试将org.slf4j.simplelogger.showdatetime=true写入simplelogger.properties时,它正常工作。我希望它能帮助一些人。