2017-04-07 166 views
1

更新 我犯了一個錯誤,我忽略了整個。我的logging.properties文件在文件名中有一個尾部空白,我不知道。我不知道它是如何進入的,但是一旦我刪除了這個空間,一切都奏效了。我的問題是我提供了錯誤的名稱,即沒有尾部空格的文件名。java.util.logging日誌文件或輸出在哪裏去?


我不明白java.util.logging是如何工作的。我試圖複製提供的示例代碼: Java Practices -> Logging messages

我首先在Eclipse中創建了一個空的Java項目。我在包myapp.business中創建了一個類SimpleLogger.java。根據resources,我把logging.properties。我沒有任何編譯問題,我可以通過代碼,但我不知道輸出到哪裏去了?

SimpleLogger.java樣子:

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這是在resources目錄的樣子:

# 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 

在我的運行配置在Eclipse中,我有我的主類爲myapp.business.SimpleLogger和我的VM參數爲-Djava.util.logging.config.file=resources/logging.properties

我在控制檯上看不到任何內容,也找不到任何* .log文件。我正在運行這個Ubuntu 16.10如果有幫助。

編輯:針對PVG 我已經嘗試的Eclipse改變VM參數在到:-Djava.util.logging.config.file=/home/myusername/EclipseWorkspace/Temp/resources/logging.properties

我也試着通過命令行中bin叫它目錄:

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

這也不起作用,即我沒有看到任何輸出,也沒有看到任何地方的* .log文件。

+0

爲什麼在日誌記錄屬性路徑中有一個反斜槓作爲分隔符? – pvg

+0

謝謝** pvg **這是一個錯誤。我試着用正斜槓,我仍然無法得到這個工作 –

+0

也許你應該給它一個絕對路徑。更好的是,從終端運行它,而不是日食。 – pvg

回答

1

對於我來說,它只能如果我把在Eclipse VM參數整個路徑:

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

然後,輸出文件將根據什麼在logging.properties配置文件來創建。在這種情況下:

# 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意味着「解決衝突的唯一編號」(也稱爲自動生成的編號,以避免具有相同名稱的文件;在我的情況下,它是0)。

+1

謝謝** Hugo **爲您解釋。我在文件名中輸入了一個錯字。一旦我擺脫了它,-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

非常感謝。我的問題更多的是我沒有看到任何* .log文件,而不知道哪個是** **或** **目錄。我仍然沒有在這些地方看到* .log文件。 –