Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Java logging to file

Posted on 2009-07-11
7
Medium Priority
?
447 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 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 400 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 400 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 1200 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

963 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