Solved

Java logging to file

Posted on 2009-07-11
7
432 Views
Last Modified: 2012-05-07
How do you use java.util.logging API to log to a specific file?

My app runs on TomCat and I created a logged but it always goes to the console, when I wanted it to go to file. Here is how I set it up:

      private static Logger log = Logger.getLogger("com.trm");

      public Log(){
            FileHandler fileHandler;
            try {
                  fileHandler = new FileHandler("/temp/log/app.log", true);
                  log.addHandler(fileHandler);
                  log.setLevel(Level.FINEST);
            } catch (SecurityException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }  
            
      }

I expected all my logging to go to th file /temp/log/app.log but it wlays goes to the console. But why?

0
Comment
Question by:ChrisOz2008
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24832113
You're not actually using the logger in your code above. You need to use the logger in the catch blocks
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24832120
What you have worked fine for me to log to app.log file (although I tested on Windows so I had to change my path to "c:\\temp\\app.log".  Double check the path to the file exists, maybe that is the issue if you are not getting log to app.log on your end.  I do get log to console, which you can turn off by adding this in constructor.

log.setUseParentHandlers(false); // turns off the parent handlers
// since only setting file handler for the logger, you should only get that with parent handlers off.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 24832121
btw, you would be better to use a logging configuration file. See

http://www.javapractices.com/topic/TopicAction.do?Id=143
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24832124
CEHJ is correct, but figured you had more to code than that.  I used your code like this in my system as indicated.
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
 
public class Log {
	private static Logger log = Logger.getLogger("com.trm");
 
    public Log(){
    	
          FileHandler fileHandler;
          try {
                fileHandler = new FileHandler("c:\\temp\\app.log", true);
                log.addHandler(fileHandler);
                log.setLevel(Level.FINEST);
                log.setUseParentHandlers(false);
          } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
          } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
          }  
          
    }
    
    public static void main(String[] args) {
    	Log l = new Log();
    	l.log.log(Level.INFO, "Testing");
    }
}

Open in new window

0
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 24832787
>       private static Logger log = Logger.getLogger("com.trm");

your logger is private, nothoing will be able to access it.
Make it public and call it to do any logging you need to do

Log.log.log(Level.INFO, "blah blah");

Remember only calls to Log.log will go to your file
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 300 total points
ID: 24832814
And if you call it statically, remember that the constructor is not called.  You will need to use a static initializer with code you have or use properties file as CEHJ suggested.
0
 

Author Closing Comment

by:ChrisOz2008
ID: 31602467
Thanks guys. mwvisa1 hit the nail on the head. The class provides static calls for the logging class so the constructor was never called and hence everything was going to the console.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
javap bin 2 41
reverse digits of a number using for loop 5 52
how to see all occupied ports on windows 10 laptop 15 77
Java Restore security prompts not working 10 67
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question