2014-01-16 13 views
6

私はSLF4J FAQからの簡単な例を試みた:ここ例外スタックトレースがログインしていないのはなぜですか?

package com.aed.tests.logging; 
import org.slf4j.LoggerFactory; 

public class TestLogging 
{ 

    public TestLogging() 
    { 
     // TODO Auto-generated constructor stub 
    } 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) 
    { 
     String s = "Hello world"; 
     try 
     { 
      Integer i = Integer.valueOf(s); 
     } 
     catch(NumberFormatException e) 
     { 
      LoggerFactory.getLogger("simplelogger").error("Failed to format {}", s, e); 
      LoggerFactory.getLogger("simplelogger").error("Without parametrized string", e); 

     } 
    } 
} 

が出力されます。

15:33:51,248000000 [SEVERE]simplelogger: Failed to format Hello world 
15:33:51,275000000 [SEVERE]simplelogger: Without parametrized string 

私は、次のlogging.properties

で、ロギング実装としてのjava.util.loggingを使用します
# Properties file which configures the operation of the JDK 
# logging facility. 

# The system will look for this config file, first using 
# a System property specified at startup: 
# 
# >java -Djava.util.logging.config.file=myLoggingConfigFilePath 
# 
# If this property is not specified, then the config file is 
# retrieved from its default location at: 
# 
# JDK_HOME/jre/lib/logging.properties 

# Global logging properties. 
# ------------------------------------------ 
# The set of handlers to be loaded upon startup. 
# Comma-separated list of class names. 
# (? LogManager docs say no comma here, but JDK example has comma.) 
handlers=java.util.logging.ConsoleHandler 

# Default global logging level. 
# Loggers and Handlers may override this level 
.level=ALL 

# Loggers 
# ------------------------------------------ 
# Loggers are usually attached to packages. 
# Here, the level for each package is specified. 
# The global level is used by default, so levels 
# specified here simply act as an override. 
# myapp.ui.level=ALL 
# myapp.business.level=CONFIG 
# myapp.data.level=SEVERE 

# Handlers 
# ----------------------------------------- 

# --- ConsoleHandler --- 
# Override of global logging level 
java.util.logging.ConsoleHandler.level=ALL 
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter 
# HH:MM:ss,nanosec 
java.util.logging.SimpleFormatter.format=%1$tT,%1$tN [%4$s]%3$s: %5$s %n 

私は例外のスタックトレースを見ることが予想されます。私は設定で何かを見逃していますか? SLF4Jまたはjdkロギングを使用して、例外的にstrack traceを有効にする必要がありますか?

EDIT「重複としてマークされています。 私の質問は「スタックトレースを印刷する方法」ではありませんでしたが、スタックトレースをプリントアウトするためのジョブを実行するSLF4Jの例を使用しても、スタックトレースは印刷されませんでした。私が言ったように、スタックトレースはlog4jを実装として使用していて、java.util.loggingではプリントアウトしていませんでした。だから、本当にjava.util.loggingの設定が間違っていたことがわかりました。私が見つけたときに答えを出しました。

+0

ちょうど実装としてのlog4jを試みたが、私は、スタックを手に入れましたトレース。 JDKロギングではスタックトレースを記録できないのですか? – remi

答えて

7

ログレコード用のフォーマッタを適切に構成することが実際には問題でした。

manual saysとして、throwableはメソッド形式の6番目のパラメータです。だから私は、logging.propertiesで、私のフォーマットに%6$sを追加しました:

java.util.logging.SimpleFormatter.format=%1$tT,%1$tN [%4$s]%3$s: %5$s %6$s %n 

そして、ここでは出力され、スタックトレースを示す:

17:29:33,314000000 [SEVERE]simplelogger: Failed to format Hello world 
java.lang.NumberFormatException: For input string: "Hello world" 
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 
    at java.lang.Integer.parseInt(Integer.java:492) 
    at java.lang.Integer.valueOf(Integer.java:582) 
    at com.aed.tests.logging.TestLogging.main(TestLogging.java:15) 

17:29:33,344000000 [SEVERE]simplelogger: Without parametrized string 
java.lang.NumberFormatException: For input string: "Hello world" 
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 
    at java.lang.Integer.parseInt(Integer.java:492) 
    at java.lang.Integer.valueOf(Integer.java:582) 
    at com.aed.tests.logging.TestLogging.main(TestLogging.java:15) 
関連する問題