Solved

Java logging to file

Posted on 2009-07-11
7
441 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 60

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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 60

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 60

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

626 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