How to write logs in text file when using java.util.logging.Logger

JavaLogging

Java Problem Overview


I have a situation in which I want to write all logs created by me into a text file.

We are using java.util.logging.Logger API to generate the logs.

I tried:

private static Logger logger = Logger.getLogger(className.class.getName());
FileHandler fh;   
fh = new FileHandler("C:/className.log");   
logger.addHandler(fh); 

But still getting my logs on console only....

Java Solutions


Solution 1 - Java

Try this sample. It works for me.

public static void main(String[] args) {  
      
    Logger logger = Logger.getLogger("MyLog");  
    FileHandler fh;  
      
    try {  
          
        // This block configure the logger with handler and formatter  
        fh = new FileHandler("C:/temp/test/MyLogFile.log");  
        logger.addHandler(fh);
        SimpleFormatter formatter = new SimpleFormatter();  
        fh.setFormatter(formatter);  
          
        // the following statement is used to log any messages  
        logger.info("My first log");  
          
    } catch (SecurityException e) {  
        e.printStackTrace();  
    } catch (IOException e) {  
        e.printStackTrace();  
    }  
      
    logger.info("Hi How r u?");  
      
}

Produces the output at MyLogFile.log

Apr 2, 2013 9:57:08 AM testing.MyLogger main  
INFO: My first log  
Apr 2, 2013 9:57:08 AM testing.MyLogger main  
INFO: Hi How r u?

Edit:

To remove the console handler, use

logger.setUseParentHandlers(false);

since the ConsoleHandler is registered with the parent logger from which all the loggers derive.

Solution 2 - Java

Firstly, where did you define your logger and from what class\method trying to call it? There is a working example, fresh baked:

public class LoggingTester {
	private final Logger logger = Logger.getLogger(LoggingTester.class
			.getName());
	private FileHandler fh = null;

	public LoggingTester() {
        //just to make our log file nicer :)
		SimpleDateFormat format = new SimpleDateFormat("M-d_HHmmss");
		try {
			fh = new FileHandler("C:/temp/test/MyLogFile_"
                + format.format(Calendar.getInstance().getTime()) + ".log");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		fh.setFormatter(new SimpleFormatter());
		logger.addHandler(fh);
	}

	public void doLogging() {
		logger.info("info msg");
		logger.severe("error message");
		logger.fine("fine message"); //won't show because to high level of logging
	}
}	

In your code you forgot to define the formatter, if you need simple one you can do it as I mentioned above, but there is another option, you can format it by yourself, there is an example (just insert it instead of this line fh.setFormatter(new SimpleFormatter()) following code):

fh.setFormatter(new Formatter() {
            @Override
			public String format(LogRecord record) {
				SimpleDateFormat logTime = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
				Calendar cal = new GregorianCalendar();
				cal.setTimeInMillis(record.getMillis());
				return record.getLevel()
						+ logTime.format(cal.getTime())
						+ " || "
						+ record.getSourceClassName().substring(
								record.getSourceClassName().lastIndexOf(".")+1,
								record.getSourceClassName().length())
						+ "."
						+ record.getSourceMethodName()
						+ "() : "
						+ record.getMessage() + "\n";
			}
		});

Or any other modification whatever you like. Hope it helps.

Solution 3 - Java

Location of log file can be control through logging.properties file. And it can be passed as JVM parameter ex : java -Djava.util.logging.config.file=/scratch/user/config/logging.properties

Details: https://docs.oracle.com/cd/E23549_01/doc.1111/e14568/handler.htm

Configuring the File handler

To send logs to a file, add FileHandler to the handlers property in the logging.properties file. This will enable file logging globally.

handlers= java.util.logging.FileHandler Configure the handler by setting the following properties:

java.util.logging.FileHandler.pattern=<home directory>/logs/oaam.log
java.util.logging.FileHandler.limit=50000
java.util.logging.FileHandler.count=1
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter

java.util.logging.FileHandler.pattern specifies the location and pattern of the output file. The default setting is your home directory.

java.util.logging.FileHandler.limit specifies, in bytes, the maximum amount that the logger writes to any one file.

java.util.logging.FileHandler.count specifies how many output files to cycle through.

java.util.logging.FileHandler.formatter specifies the java.util.logging formatter class that the file handler class uses to format the log messages. SimpleFormatter writes brief "human-readable" summaries of log records.


To instruct java to use this configuration file instead of $JDK_HOME/jre/lib/logging.properties:

java -Djava.util.logging.config.file=/scratch/user/config/logging.properties

Solution 4 - Java

A good library available named log4j for Java.
This will provide numerous feature. Go through link and you will find your solution.

Solution 5 - Java

Maybe this is what you need...

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * LogToFile class
 * This class is intended to be use with the default logging class of java
 * It save the log in an XML file  and display a friendly message to the user
 * @author Ibrabel <[email protected]>
 */
public class LogToFile {

    protected static final Logger logger=Logger.getLogger("MYLOG");
    /**
     * log Method 
     * enable to log all exceptions to a file and display user message on demand
     * @param ex
     * @param level
     * @param msg 
     */
    public static void log(Exception ex, String level, String msg){

        FileHandler fh = null;
        try {
            fh = new FileHandler("log.xml",true);
            logger.addHandler(fh);
            switch (level) {
                case "severe":
                    logger.log(Level.SEVERE, msg, ex);
                    if(!msg.equals(""))
                        JOptionPane.showMessageDialog(null,msg,
                            "Error", JOptionPane.ERROR_MESSAGE);
                    break;
                case "warning":
                    logger.log(Level.WARNING, msg, ex);
                    if(!msg.equals(""))
                        JOptionPane.showMessageDialog(null,msg,
                            "Warning", JOptionPane.WARNING_MESSAGE);
                    break;
                case "info":
                    logger.log(Level.INFO, msg, ex);
                    if(!msg.equals(""))
                        JOptionPane.showMessageDialog(null,msg,
                            "Info", JOptionPane.INFORMATION_MESSAGE);
                    break;
                case "config":
                    logger.log(Level.CONFIG, msg, ex);
                    break;
                case "fine":
                    logger.log(Level.FINE, msg, ex);
                    break;
                case "finer":
                    logger.log(Level.FINER, msg, ex);
                    break;
                case "finest":
                    logger.log(Level.FINEST, msg, ex);
                    break;
                default:
                    logger.log(Level.CONFIG, msg, ex);
                    break;
            }
        } catch (IOException | SecurityException ex1) {
            logger.log(Level.SEVERE, null, ex1);
        } finally{
            if(fh!=null)fh.close();
        }
    }

    public static void main(String[] args) {

        /*
            Create simple frame for the example
        */
        JFrame myFrame = new JFrame();
        myFrame.setTitle("LogToFileExample");
        myFrame.setSize(300, 100);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setLocationRelativeTo(null);
        JPanel pan = new JPanel();
        JButton severe = new JButton("severe");
        pan.add(severe);
        JButton warning = new JButton("warning");
        pan.add(warning);
        JButton info = new JButton("info");
        pan.add(info);

        /*
            Create an exception on click to use the LogToFile class
        */
        severe.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent ae) {
                int j = 20, i = 0;
                try {
                    System.out.println(j/i);
                } catch (ArithmeticException ex) {
                    log(ex,"severe","You can't divide anything by zero");
                }

            }

        });

        warning.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent ae) {
                int j = 20, i = 0;
                try {
                    System.out.println(j/i);
                } catch (ArithmeticException ex) {
                    log(ex,"warning","You can't divide anything by zero");
                }

            }

        });

        info.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent ae) {
                int j = 20, i = 0;
                try {
                    System.out.println(j/i);
                } catch (ArithmeticException ex) {
                    log(ex,"info","You can't divide anything by zero");
                }

            }

        });

        /*
            Add the JPanel to the JFrame and set the JFrame visible
        */
        myFrame.setContentPane(pan);
        myFrame.setVisible(true);
    }
}

Solution 6 - Java

import java.io.IOException;
import org.apache.log4j.Appender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;

/**
 * @author Kiran
 * 
 */
public class MyLogger {

	public MyLogger() {
    }

    public static void main(String[] args) {
    	Logger logger = Logger.getLogger("MyLog");
    	Appender fh = null;
    	try {
    		fh = new FileAppender(new SimpleLayout(), "MyLogFile.log");
    		logger.addAppender(fh);
    		fh.setLayout(new SimpleLayout());
    		logger.info("My first log");
    	} catch (SecurityException e) {
    		e.printStackTrace();
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    	logger.info("Hi How r u?");
    }
}

Solution 7 - Java

int SIZE = "<intialize-here>"
int ROTATIONCOUNT = "<intialize-here>"

Handler handler = new FileHandler("test.log", SIZE, LOG_ROTATIONCOUNT);
logger.addHandler(handler);     // for your code.. 

// you can also set logging levels
Logger.getLogger(this.getClass().getName()).log(Level.[...]).addHandler(handler);

Solution 8 - Java

Hope people find this helpful

public static void writeLog(String info) {
	String filename = "activity.log";
	String FILENAME = "C:\\testing\\" + filename;
	BufferedWriter bw = null;
	FileWriter fw = null;
	try {
		fw = new FileWriter(FILENAME, true);
		bw = new BufferedWriter(fw);
		bw.write(info);
		bw.write("\n");
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		try {
			if (bw != null)
				bw.close();
			if (fw != null)
				fw.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}

Solution 9 - Java

Here is my logging class based on the accepted answer:

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.*;

public class ErrorLogger
{
	private Logger logger;

	public ErrorLogger()
	{
		logger = Logger.getAnonymousLogger();

		configure();
	}

	private void configure()
	{
		try
		{
			String logsDirectoryFolder = "logs";
			Files.createDirectories(Paths.get(logsDirectoryFolder));
			FileHandler fileHandler = new FileHandler(logsDirectoryFolder + File.separator + getCurrentTimeString() + ".log");
			logger.addHandler(fileHandler);
			SimpleFormatter formatter = new SimpleFormatter();
			fileHandler.setFormatter(formatter);
		} catch (IOException exception)
		{
			exception.printStackTrace();
		}

		addCloseHandlersShutdownHook();
	}

	private void addCloseHandlersShutdownHook()
	{
		Runtime.getRuntime().addShutdownHook(new Thread(() ->
		{
			// Close all handlers to get rid of empty .LCK files
			for (Handler handler : logger.getHandlers())
			{
				handler.close();
			}
		}));
	}

	private String getCurrentTimeString()
	{
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
		return dateFormat.format(new Date());
	}

	public void log(Exception exception)
	{
		logger.log(Level.SEVERE, "", exception);
	}
}

Solution 10 - Java

Here is an example of how to overwrite Logger configuration from the code. Does not require external configuration file ..

FileLoggerTest.java:

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

public class FileLoggerTest {

    public static void main(String[] args) {

        try {
            String h = MyLogHandler.class.getCanonicalName();
            StringBuilder sb = new StringBuilder();
            sb.append(".level=ALL\n");
            sb.append("handlers=").append(h).append('\n');
            LogManager.getLogManager().readConfiguration(new ByteArrayInputStream(sb.toString().getBytes("UTF-8")));
        } catch (IOException | SecurityException ex) {
            // Do something about it
        }

        Logger.getGlobal().severe("Global SEVERE log entry");
        Logger.getLogger(FileLoggerTest.class.getName()).log(Level.SEVERE, "This is a SEVERE log entry");
        Logger.getLogger("SomeName").log(Level.WARNING, "This is a WARNING log entry");
        Logger.getLogger("AnotherName").log(Level.INFO, "This is an INFO log entry");
        Logger.getLogger("SameName").log(Level.CONFIG, "This is an CONFIG log entry");
        Logger.getLogger("SameName").log(Level.FINE, "This is an FINE log entry");
        Logger.getLogger("SameName").log(Level.FINEST, "This is an FINEST log entry");
        Logger.getLogger("SameName").log(Level.FINER, "This is an FINER log entry");
        Logger.getLogger("SameName").log(Level.ALL, "This is an ALL log entry");

    }
}

MyLogHandler.java

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.SimpleFormatter;

public final class MyLogHandler extends FileHandler {

    public MyLogHandler() throws IOException, SecurityException {
        super("/tmp/path-to-log.log");
        setFormatter(new SimpleFormatter());
        setLevel(Level.ALL);
    }

    @Override
    public void publish(LogRecord record) {
        System.out.println("Some additional logic");
        super.publish(record);
    }

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPankajView Question on Stackoverflow
Solution 1 - JavaSri Harsha ChilakapatiView Answer on Stackoverflow
Solution 2 - JavaDenisDView Answer on Stackoverflow
Solution 3 - JavaAwanish KumarView Answer on Stackoverflow
Solution 4 - JavaChintan RathodView Answer on Stackoverflow
Solution 5 - JavaIbrabelView Answer on Stackoverflow
Solution 6 - JavakanaparthikiranView Answer on Stackoverflow
Solution 7 - JavaAurAView Answer on Stackoverflow
Solution 8 - JavaSnowbasesView Answer on Stackoverflow
Solution 9 - JavaBullyWiiPlazaView Answer on Stackoverflow
Solution 10 - JavaAndrew KrasnyView Answer on Stackoverflow