java.util.logging 日志文件或输出到哪里去了?
Where does the java.util.logging log file or output go?
更新
我犯了一个我一直忽略的错误。我的 logging.properties
文件在文件名中有一个我不知道的尾随 space。我不知道它是如何进入那里的,但是一旦我删除了 space 一切正常。我的问题是我提供了错误的名称,即没有尾随 space.
的文件名
我不明白 java.util.logging
是如何工作的。我正在尝试复制以下位置提供的示例代码:
Java Practices -> Logging messages
我首先在 Eclipse 中创建了一个空的 Java 项目。 我在包 myapp.business
中创建了一个 class 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 中的 运行 配置中,我的 Main class 为 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 文件。
对我来说,只有当我将整个路径放在 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
表示 "unique number to resolve conflicts" (a.k.a。一个自动生成的数字,以避免具有相同名称的文件;在我的例子中,它是 0
).
...but I cannot figure out where does the output go?
使用以下代码获取工作目录并列出可以告诉您主目录的环境。此示例还尝试使用 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();
}
更新
我犯了一个我一直忽略的错误。我的 logging.properties
文件在文件名中有一个我不知道的尾随 space。我不知道它是如何进入那里的,但是一旦我删除了 space 一切正常。我的问题是我提供了错误的名称,即没有尾随 space.
我不明白 java.util.logging
是如何工作的。我正在尝试复制以下位置提供的示例代码:
Java Practices -> Logging messages
我首先在 Eclipse 中创建了一个空的 Java 项目。 我在包 myapp.business
中创建了一个 class 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 中的 运行 配置中,我的 Main class 为 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 文件。
对我来说,只有当我将整个路径放在 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
表示 "unique number to resolve conflicts" (a.k.a。一个自动生成的数字,以避免具有相同名称的文件;在我的例子中,它是 0
).
...but I cannot figure out where does the output go?
使用以下代码获取工作目录并列出可以告诉您主目录的环境。此示例还尝试使用 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();
}