O.O.
O.O.

Reputation: 2013

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. 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 i.e. the filename without the trailing space.


I don’t understand how java.util.logging works. I’m trying to replicate the sample code provided at: Java Practices -> Logging messages

I first created an empty Java project in Eclipse. I created a class SimpleLogger.java in the package myapp.business. Under resources, I put 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:

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:

# 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

I don't see anything on the console nor can I locate any *.log file. I'm running this on Ubuntu 16.10 if that helps.

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

I've also tried to call it via the command line in the bin directory:

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

This too does not work i.e. I don't see any output, nor do I see a *.log file anywhere.

Upvotes: 4

Views: 7930

Answers (2)

jmehrens
jmehrens

Reputation: 11045

...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.

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();
}

Upvotes: 1

user7605325
user7605325

Reputation:

For me, it works only if I put the whole path in Eclipse VM arguments:

-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. 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" (a.k.a. an auto-generated number to avoid having files with the same name; in my case, it was 0).

Upvotes: 2

Related Questions