2017-04-07 9 views
1

更新 私は間違いを犯しました。私のlogging.propertiesファイルには、わからなかったファイル名の末尾にスペースがあります。どうやってそこに入ったのかわからないけど、一度そのスペースを削除すればすべてがうまくいった。私の問題は、間違った名前、つまり末尾のスペースがないファイル名を提供していたことでした。java.util.loggingログファイルまたは出力はどこに行きますか?


java.util.loggingの仕組みがわかりません。私は提供されたサンプルコードを複製しようとしています: Java Practices -> Logging messages

初めてで空のJavaプロジェクトを作成しました。私はクラスSimpleLogger.javamyapp.businessパッケージに作成しました。 resourcesの下にはlogging.propertiesと書かれています。コンパイルに問題はなく、コードをステップバイステップで実行できますが、出力がどこにあるのかわかりません。 resourcesディレクトリにある

package myapp.business; 

import java.util.logging.Level; 
import java.util.logging.Logger; 

public final class SimpleLogger { 

    public static void main(String... args) { 
    SimpleLogger thing = new SimpleLogger(); 
    thing.doSomething(); 
    } 

    public void doSomething() { 
    // Log messages, one for each level 
    // The actual logging output depends on the configured 
    // level for this package. Calls to "inapplicable" 
    // messages are inexpensive. 
    fLogger.finest("this is finest"); 
    fLogger.finer("this is finer"); 
    fLogger.fine("this is fine"); 
    fLogger.config("this is config"); 
    fLogger.info("this is info"); 
    fLogger.warning("this is a warning"); 
    fLogger.severe("this is severe"); 

    // In the above style, the name of the class and 
    // method which has generated a message is placed 
    // in the output on a best-efforts basis only. 
    // To ensure that this information is always 
    // included, use the following "precise log" 
    // style instead : 
    fLogger.logp(Level.INFO, this.getClass().toString(), "doSomething", "blah"); 

    // For the very common task of logging exceptions, there is a 
    // method which takes a Throwable : 
    Throwable ex = new IllegalArgumentException("Some exception text"); 
    fLogger.log(Level.SEVERE, "Some message", ex); 

    // There are convenience methods for exiting and 
    // entering a method, which are at Level.FINER : 
    fLogger.exiting(this.getClass().toString(), "doSomething"); 

    // Display user.home directory, if desired. 
    // (This is the directory where the log files are generated.) 
    // System.out.println("user.home dir: " + 
    // System.getProperty("user.home")); 
    } 

    // PRIVATE 

    // This style has no hard-coded literals, and requires the logger 
    // to be non-static. 
    private final Logger fLogger = Logger.getLogger(this.getClass().getPackage().getName()); 

    // This style lets the logger be static, but hard-codes a class literal. 
    // private static final Logger fLogger = 
    // Logger.getLogger(SimpleLogger.class.getPackage().getName()) 
    // ; 

    // This style uses a hard-coded literal and should likely be avoided: 
    // private static final Logger fLogger = Logger.getLogger("myapp.business"); 
} 

マイlogging.propertiesは、次のようになります:

SimpleLogger.javaは次のようになりますEclipseのの私の実行構成で

# 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.FileHandler, java.util.logging.ConsoleHandler 

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

# 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=SEVERE 
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter 

# --- FileHandler --- 
# Override of global logging level 
java.util.logging.FileHandler.level=ALL 

# Naming style for the output file: 
# (The output file is placed in the directory 
# defined by the "user.home" System property.) 
java.util.logging.FileHandler.pattern=%h/java%u.log 

# Limiting size of output file in bytes: 
java.util.logging.FileHandler.limit=50000 

# Number of output files to cycle through, by appending an 
# integer to the base file name: 
java.util.logging.FileHandler.count=1 

# Style of output (Simple or XML): 
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter 

、私はmyapp.business.SimpleLoggerとして私のメインクラスを持っており、 VMの引数は、-Djava.util.logging.config.file=resources/logging.properties

コンソールに何も表示されず、* .logファイルが見つかりません。私はUbuntu 16.10でこれを実行しています。

編集:私もbinで、コマンドラインを介して、それを呼び出すようにしようとしました-Djava.util.logging.config.file=/home/myusername/EclipseWorkspace/Temp/resources/logging.properties

PVG への応答では、私はにEclipseのでVM引数を変更しようとしてきましたディレクトリ:

java -Djava.util.logging.config.file=/home/myusername/EclipseWorkspace/Temp/resources/logging.properties -cp . myapp.business.SimpleLogger 

これも機能しません。出力が表示されず、* .logファイルが表示されません。

-Djava.util.logging.config.file=/whole/path/of/logging.properties 

次に、出力ファイルがlogging.propertiesファイルで構成されているものに基づいて作成されます。私にとって

+0

ロギングプロパティのパスにセパレータとしてバックスラッシュを使用しているのはなぜですか? – pvg

+0

ありがとうございました** pvg **間違いでした。私はフォワードスラッシュでそれを試しても、私はまだこれを動作させることができません –

+0

おそらくあなたは絶対パスを与える必要があります。さらに良い方法は、端末から実行することです。 – pvg

答えて

1

は、それは私がEclipseのVM引数でパス全体を置く場合にのみ機能します。この場合:

# Naming style for the output file: 
# (The output file is placed in the directory 
# defined by the "user.home" System property.) 
java.util.logging.FileHandler.pattern=%h/java%u.log 

出力ファイルは、ユーザーのホームディレクトリに作成されます。私の場合、作成されたファイル名はjava0.log - %uで、意味はで、 "競合を解消するための一意の番号"(私の場合は同じ名前のファイルを避けるために自動生成された番号です。

+1

ありがとうございます**あなたの説明のためのヒューゴ**。私はファイル名にタイプミスをしました。私が一旦それを取り除くと '-Djava.util.logging.config.file = resources/logging.properties'はフルパスなしで動作します。 –

1

...しかし、私は出力がどこに行くのか分からないのですか?

次のコードを使用して作業ディレクトリを取得し、ホームディレクトリを示す環境を表示します。この例では、LogManager設定を使用してファイルハンドラを作成しようとしています。

public static void main(String[] args) throws IOException { 
    System.out.println("Working directory=" + new File(".").getCanonicalPath()); 
    for (Map.Entry<String, String> e : System.getenv().entrySet()) { 
     System.out.println(e); 
    } 
    new FileHandler().close(); 
} 
+0

ありがとうございました。私の質問は、** working **や** home **ディレクトリがどれか分かっていないのではなく、* .logファイルが表示されないということでした。これらの場所に* .logファイルがまだ表示されません。 –

関連する問題