(Java) writing events to log text file

23,199

Solution 1

fileTxt = new FileHandler("c:/SimleTaskEvents.txt");

This line only creates a handler.

It does not create the file. What you need to do is, create the file(SimleTaskEvents.txt) in the directory "C:/". after that when u execute your program, the same program that u have put here, you will see logs being written to it.

Solution 2

You need to write to the log first

logger.info("this is a line of logging");

and maybe check this tutorial

Share:
23,199

Related videos on Youtube

Han
Author by

Han

Updated on April 10, 2020

Comments

  • Han
    Han about 4 years

    I am trying to write the events in a log file but no file is being created. I am getting no error at all. Here is the log class:

    public class Logs {
    static FileHandler fileTxt;
    static SimpleFormatter formatterTxt;
    
    
    static public void logging() throws IOException {
    
        Logger logger = Logger.getLogger("");
        logger.setLevel(Level.INFO);//Loget Info, Warning dhe Severe do ruhen
        fileTxt = new FileHandler("c:/SimleTaskEvents.txt");
        formatterTxt = new SimpleFormatter();
        fileTxt.setFormatter(formatterTxt);
        logger.addHandler(fileTxt);
    
    }
    }
    
  • Han
    Han over 11 years
    Yes I am doing that through another class. I declare:
  • Han
    Han over 11 years
    private final static Logger lgs = Logger.getLogger(Logs.class.getName());
  • Han
    Han over 11 years
    and I use the lgs object than. The problem is that no file is being created!
  • Han
    Han over 11 years
    this is using log4j, I just need to write a simple text file. However I followed the tutorial of the previous comment and I am in a good way I think :)