Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up java logging using a properties file? (java.util.logging)

Tags:

java

logging

I have a stupid java logging problem: I'm loading the logging configuration from my app configuration file - but it just doesn't log anything after reading the file (which looks pretty much like the examples you will find on the net except for the additional application configuration - removing this also doesn't help). The "initializing..." log line appears just fine, but the "starting app" and any further messages are neither logged to the console, nor is the logfile ever created. What am I missing here?

The Logger code looks like this:

... Logger log = Logger.getLogger("myApp"); log.setLevel(Level.ALL); log.info("initializing - trying to load configuration file ...");  Properties preferences = new Properties(); try {     FileInputStream configFile = new FileInputStream("/path/to/app.properties");     preferences.load(configFile);     LogManager.getLogManager().readConfiguration(configFile); } catch (IOException ex) {     System.out.println("WARNING: Could not open configuration file");     System.out.println("WARNING: Logging not configured (console output only)"); } log.info("starting myApp"); ... 

And this is the configuration file:

appconfig1 = foo appconfig2 = bar  # Logging handlers = java.util.logging.FileHandler, java.util.logging.ConsoleHandler .level = ALL  # File Logging java.util.logging.FileHandler.pattern = %h/myApp.log java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter java.util.logging.FileHandler.level = INFO  # Console Logging java.util.logging.ConsoleHandler.level = ALL 
like image 479
VolkA Avatar asked Jun 06 '09 16:06

VolkA


People also ask

Where do I put logging properties file?

properties from the classpath. Normally, we put the logging. properties at the src/main/resources , and project compile or build will copy it to the root of the classpath. And we can use LogManager or System.

How do I create a Java logger?

The process of creating a new Logger in Java is quite simple. You have to use Logger. getLogger() method. The getLogger() method identifies the name of the Logger and takes string as a parameter.

What is Java Util logging?

In Java, the LogManager is a class that belongs to the java. util. logging package. It keeps track the global logging configuration, creates, and maintains the logger instances. We can also use it to set its own application-specific configuration.


2 Answers

you can set your logging configuration file through command line:

$ java -Djava.util.logging.config.file=/path/to/app.properties MainClass

this way seems cleaner and easier to maintain.

like image 149
cd1 Avatar answered Sep 22 '22 03:09

cd1


Okay, first intuition is here:

handlers = java.util.logging.FileHandler, java.util.logging.ConsoleHandler .level = ALL 

The Java prop file parser isn't all that smart, I'm not sure it'll handle this. But I'll go look at the docs again....

In the mean time, try:

handlers = java.util.logging.FileHandler java.util.logging.ConsoleHandler.level = ALL 

Update

No, duh, needed more coffee. Nevermind.

While I think more, note that you can use the methods in Properties to load and print a prop-file: it might be worth writing a minimal program to see what java thinks it reads in that file.


Another update

This line:

    FileInputStream configFile = new FileInputStream("/path/to/app.properties")); 

has an extra end-paren. It won't compile. Make sure you're working with the class file you think you are.

like image 25
Charlie Martin Avatar answered Sep 25 '22 03:09

Charlie Martin