1

I am using the default java.util.Logging API to print information to my log.

Logger.getLogger(Datastore.class.getName());
logger.info("Registering ");

where Datastore is the name of the class.But I am unable to find where the logs are stored. I am running on a Windows 7 machine and the above program is a part of a servlet. I apologize for the seemingly stupid question, but sometimes such insignificant things greatly hinder progress.

Chan
  • 2,601
  • 6
  • 28
  • 45
user1179510
  • 1,003
  • 1
  • 14
  • 24
  • Assuming you use java.util.logging.Logger, and a FileHandler has been added to the root logger (by default only a ConsoleHandler is available), and the log file name has not been provided explicitly, then the log file is C:\Users\YourUserName\javaN.log where N is a number. See http://docs.oracle.com/javase/7/docs/api/java/util/logging/FileHandler.html – mins May 17 '14 at 22:49

4 Answers4

1

You could add a handler to the Logger, to specify where the log should go.

Handler handler = new FileHandler("DataStore.log");
Logger logger = Logger.getLogger(Datastore.class.getName());
logger.addHandler(handler);
logger.info("Registering ");

The logger only creates the log-object, but it is the handler that acutally displays it. The handler can be any of the subclasses defined from Handler. If you use FileHandler, you should read the Java API for FileHandler.

Heskja
  • 787
  • 6
  • 19
1

By default the java.util.logging.Logger sends output to the console and aren't printed to a file. You will have to add a java.util.logging.Handler to the Logger. Below are snippets to create Handlers of HTML, TEXTFILE. Hope this helps.

package logging;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class MyLogger {
    static private FileHandler fileTxt;
    static private SimpleFormatter formatterTxt;

    static private FileHandler fileHTML;
    static private Formatter formatterHTML;

    static public void setup() throws IOException {
        // Create Logger
        Logger logger = Logger.getLogger("");
        logger.setLevel(Level.INFO);
        fileTxt = new FileHandler("Logging.txt");
        fileHTML = new FileHandler("Logging.html");

        // Create txt Formatter
        formatterTxt = new SimpleFormatter();
        fileTxt.setFormatter(formatterTxt);
        logger.addHandler(fileTxt);

        // Create HTML Formatter
        formatterHTML = new MyHtmlFormatter();
        fileHTML.setFormatter(formatterHTML);
        logger.addHandler(fileHTML);
    }
} 

For further reference read this article.

Chan
  • 2,601
  • 6
  • 28
  • 45
0

Check the log4j configuration file which will have the path to the place where the logs are updated. Check this for reference.

Community
  • 1
  • 1
Anuj Balan
  • 7,629
  • 23
  • 58
  • 92
0

If you don't have a log4j configuration file (assuming that Logger is from log4j), things will get very quiet.

For starters, try to add the properties file at the root of your classes directory, something like:

# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

Which comes from here

Miquel
  • 15,405
  • 8
  • 54
  • 87