programing

slf4j-simple 설정 방법

bestcode 2022. 8. 25. 23:55
반응형

slf4j-simple 설정 방법

api 1.7 및 slf4j-module을 구현합니다.이 조합으로 로깅레벨을 설정하는 방법을 찾을 수 없습니다.

누구 도와줄 사람 있어?

시스템 속성 중 하나입니다.

-Dorg.slf4j.simpleLogger.defaultLogLevel=debug

또는simplelogger.properties클래스 패스에 파일하다

자세한 것은, http://www.slf4j.org/api/org/slf4j/impl/SimpleLogger.html 를 참조해 주세요.

이것은 샘플입니다.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

메이븐 또는 그래들 프로젝트에서 "클래스 패스 위"에 있는 편리한 장소는src/main/resources/simplelogger.properties.

시스템 속성을 설정하여 프로그래밍 방식으로 변경할 수 있습니다.

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를 사용할 수 있습니다.

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

물론, 이것은 매우 안정적이고 신뢰할 수 있는 솔루션이 아닙니다.다음번엔 약쟁이들이 벌목기를 갈아끼우면 깨질테니까요.

언급URL : https://stackoverflow.com/questions/14544991/how-to-configure-slf4j-simple

반응형