Solved

Java logging to file

Posted on 2009-07-11
7
412 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

705 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now