development

속성 파일을 사용하여 Java 로깅을 설정하는 방법은 무엇입니까?

big-blog 2020. 10. 12. 07:47
반응형

속성 파일을 사용하여 Java 로깅을 설정하는 방법은 무엇입니까? (java.util.logging)


멍청한 자바 로깅 문제가 있습니다. 내 앱 구성 파일에서 로깅 구성을로드하고 있습니다.하지만 파일을 읽은 후에는 아무것도 기록하지 않습니다 (인터넷에서 찾을 수있는 예제와 거의 비슷합니다. 추가 응용 프로그램 구성-제거해도 도움이되지 않습니다.) "초기화 중 ..."로그 줄은 정상적으로 표시되지만 "시작 앱"및 추가 메시지는 콘솔에 기록되지 않으며 로그 파일도 생성되지 않습니다. 내가 여기서 무엇을 놓치고 있습니까?

Logger 코드는 다음과 같습니다.

...
Logger log = Logger.getLogger("myApp");
log.setLevel(Level.ALL);
log.info("initializing - trying to load configuration file ...");

Properties preferences = new Properties();
try {
    FileInputStream configFile = new FileInputStream("/path/to/app.properties");
    preferences.load(configFile);
    LogManager.getLogManager().readConfiguration(configFile);
} catch (IOException ex)
{
    System.out.println("WARNING: Could not open configuration file");
    System.out.println("WARNING: Logging not configured (console output only)");
}
log.info("starting myApp");
...

그리고 이것은 구성 파일입니다.

appconfig1 = foo
appconfig2 = bar

# Logging
handlers = java.util.logging.FileHandler, java.util.logging.ConsoleHandler
.level = ALL

# File Logging
java.util.logging.FileHandler.pattern = %h/myApp.log
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.level = INFO

# Console Logging
java.util.logging.ConsoleHandler.level = ALL

좋아요, 첫 번째 직감이 여기 있습니다 :

handlers = java.util.logging.FileHandler, java.util.logging.ConsoleHandler
.level = ALL

Java prop 파일 파서는 그다지 똑똑하지 않습니다.이 문제를 처리 할 수 ​​있을지 모르겠습니다. 하지만 다시 문서를 보러 갈 게요 ....

그동안 다음을 시도하십시오.

handlers = java.util.logging.FileHandler
java.util.logging.ConsoleHandler.level = ALL

최신 정보

아니, 커피가 더 필요해. 신경 쓰지 마.

더 많이 생각하지만, 속성 의 메소드를 사용하여 소품 파일을로드하고 인쇄 할 수 있습니다. Java가 해당 파일에서 읽는 것으로 생각하는 것을보기 위해 최소한의 프로그램을 작성하는 것이 좋습니다.


또 다른 업데이트

이 줄 :

    FileInputStream configFile = new FileInputStream("/path/to/app.properties"));

여분의 괄호가 있습니다. 컴파일되지 않습니다. 자신이 생각하는 클래스 파일로 작업하고 있는지 확인하십시오.


명령 줄을 통해 로깅 구성 파일을 설정할 수 있습니다.

$ java -Djava.util.logging.config.file=/path/to/app.properties MainClass

이 방법은 더 깨끗하고 유지 관리하기 쉽습니다.


위의 코드에서 코드를 시도했지만 [preferences.load (configFile);] 문을 사용하지 마십시오. 그러면 작동합니다. 여기에서 샘플 코드를 실행하고 있습니다.

public static void main(String[]s)
{

    Logger log = Logger.getLogger("MyClass");
    try {
    FileInputStream fis =  new FileInputStream("p.properties");
    LogManager.getLogManager().readConfiguration(fis);
    log.setLevel(Level.FINE);
    log.addHandler(new java.util.logging.ConsoleHandler());
    log.setUseParentHandlers(false);

    log.info("starting myApp");
    fis.close();

    } 
    catch(IOException e) {
    e.printStackTrace();
    }
}

Logger log = Logger.getLogger("myApp");
log.setLevel(Level.ALL);
log.info("initializing - trying to load configuration file ...");

//Properties preferences = new Properties();
try {
    //FileInputStream configFile = new //FileInputStream("/path/to/app.properties");
    //preferences.load(configFile);
    InputStream configFile = myApp.class.getResourceAsStream("app.properties");
    LogManager.getLogManager().readConfiguration(configFile);
} catch (IOException ex)
{
    System.out.println("WARNING: Could not open configuration file");
    System.out.println("WARNING: Logging not configured (console output only)");
}
log.info("starting myApp");

이것은 작동합니다 .. :) readConfiguration ()에서 InputStream을 전달해야합니다.


올바른 경로에서 로그 파일을 찾고 있습니까 : % h / one % u.log

Here %h resolves to your home : In windows this defaults to : C:\Documents and Settings(user_name).

I have tried the sample code you have posted and it works fine after you specify the configuration file path (logging.properties either through code or java args) .

참고URL : https://stackoverflow.com/questions/960099/how-to-set-up-java-logging-using-a-properties-file-java-util-logging

반응형