简体   繁体   English

java.util.logging日志文件或输出在哪里?

[英]Where does the java.util.logging log file or output go?

Update I've made a mistake that I overlooked throughout. 更新我犯了一个错误,而这个错误一直都被我忽略了。 My logging.properties file had a trailing space in the filename that I was not aware of. 我的logging.properties文件在文件名中有一个我不知道的尾随空格。 I've no clue how it got in there, but once I deleted that space everything worked. 我不知道它是怎么进入那里的,但是一旦我删除了那个空间,一切就起作用了。 My problem was that I was providing the wrong name ie the filename without the trailing space. 我的问题是我提供的名称错误,即文件名没有尾随空格。


I don't understand how java.util.logging works. 我不明白java.util.logging是如何工作的。 I'm trying to replicate the sample code provided at: Java Practices -> Logging messages 我正在尝试复制以下位置提供的示例代码: Java Practices->记录消息

I first created an empty Java project in Eclipse. 我首先在Eclipse中创建了一个空的Java项目 I created a class SimpleLogger.java in the package myapp.business . 我在包myapp.business创建了一个类SimpleLogger.java Under resources , I put logging.properties . resources ,我放置了logging.properties I don't have any compilation problems and I can step through the code, but I cannot figure out where does the output go? 我没有任何编译问题,可以单步执行代码,但无法弄清楚输出在哪里?

SimpleLogger.java looks like: 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");
}

My logging.properties which is in the resources directory looks like: 我在resources目录中的logging.properties看起来像:

# 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

In my run configuration in Eclipse , I have my Main class as myapp.business.SimpleLogger and my VM arguments as -Djava.util.logging.config.file=resources/logging.properties Eclipse的运行配置中,我的Main类为myapp.business.SimpleLogger而我的VM参数为-Djava.util.logging.config.file=resources/logging.properties

I don't see anything on the console nor can I locate any *.log file. 我在控制台上看不到任何内容,也找不到任何* .log文件。 I'm running this on Ubuntu 16.10 if that helps. 如果有帮助,我可以在Ubuntu 16.10上运行它。

Edit: In response to pvg I've attempted to change the VM argument in Eclipse to: -Djava.util.logging.config.file=/home/myusername/EclipseWorkspace/Temp/resources/logging.properties 编辑:作为对pvg的响应,我试图将Eclipse中的VM参数更改为: -Djava.util.logging.config.file=/home/myusername/EclipseWorkspace/Temp/resources/logging.properties

I've also tried to call it via the command line in the bin directory: 我还尝试通过bin目录中的命令行调用它:

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

This too does not work ie I don't see any output, nor do I see a *.log file anywhere. 这也行不通,即我看不到任何输出,也看不到任何* .log文件。

For me, it works only if I put the whole path in Eclipse VM arguments: 对我来说,只有将整个路径放在Eclipse VM参数中,它才有效:

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

Then, the output file will be created according to what's configured in logging.properties file. 然后,将根据logging.properties文件中配置的内容创建输出文件。 In this case: 在这种情况下:

# 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

The output file will be created in your user's home directory. 输出文件将在用户的主目录中创建。 In my case, the filename created was java0.log - %u means "unique number to resolve conflicts" (aka an auto-generated number to avoid having files with the same name; in my case, it was 0 ). 在我的情况下,创建的文件名是java0.log %u表示“解决冲突的唯一编号” (又名自动生成的编号,以避免文件具有相同的名称;在我的情况下,它是0 )。

...but I cannot figure out where does the output go? ...但是我不知道输出在哪里?

Use the following code to get the working directory and list the environment which can tell you the home directory. 使用以下代码获取工作目录并列出可以告诉您主目录的环境。 This example also tries to create a file handler using the LogManager settings. 本示例还尝试使用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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM