Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java logging to file

Posted on 2009-07-11
7
Medium Priority
?
445 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 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

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