2017-04-07 165 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文件。 –