2

I am very new to java and coding so please understand that I am very naive when it comes to this stuff.

I am trying to get Java to write logs to a .txt file. I have been researching this for hours and my brain hurts too much to keep looking. I am hoping you guys can look at this and tell me what is wrong. Below is the code. This is being written on my Mac for the time being but ultimately I will have it run on Windows.

import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.Level;

public class WriteLogEntriesToLogFile extends Login {

    public WriteLogEntriesToLogFile(String[] args) throws Exception {

        boolean append = true;
        FileHandler handler = new FileHandler("Test.logon.log.txt");

        Logger logger = Logger.getLogger("/Downloads/log.txt");
        logger.addHandler(handler);

        logger.severe("severe message");

  logger.warning("warning message");

  logger.info("info message");

  logger.config("config message");

  logger.fine("fine message");

  logger.finer("finer message");

  logger.finest("finest message");

    }

}

I definitely feel like I am missing something (or a lot of somethings). Any assistance is appreciated!

  • any error messages? – Shivansh Narayan Mar 29 '20 at 18:40
  • _I am trying to get Java to write logs to a .txt file_ In other words, this is not wotking for you. But what is the actual problem? The log file is not created? It is created but is empty? One thing I noticed in the code you posted: The argument passed to method [getLogger(String)](https://docs.oracle.com/javase/8/docs/api/java/util/logging/Logger.html#getLogger-java.lang.String-) is an arbitrary name for the `Logger` and not the path to the log file - assuming that "/Downloads/log.txt" is the path to your log file. – Abra Mar 29 '20 at 18:49
  • The log file is not creating. Under the output in NetBeans I see the information but I need it to write to a text file. – Smith Nicky Mar 29 '20 at 18:56

1 Answers1

0

From what I could tell your logging was outputting to a file and seemed to be working.

However, I noticed the logs were difficult to parse due to the format being xml-record based, so I used a SimpleFormatter in my solution to read the logs at a line level.

Solution:

 public static void main(String[] args) {

        Logger logger = Logger.getLogger("");
        FileHandler fileHandler;

        try {
            fileHandler = new FileHandler("Test.logon.log.txt");
            logger.addHandler(fileHandler);

            SimpleFormatter formatter = new SimpleFormatter();
            fileHandler.setFormatter(formatter);
            logger.info("0.0 - My first log");
            logger.info("1.0 - Test log");

        } catch (SecurityException | IOException e) {
            e.printStackTrace();
        }
    }

I hope this helps.

Shane Creedon
  • 1,573
  • 1
  • 14
  • 18
  • I can not find where the log.text file is located and I am also struggling to add in what it should be loggging. I need it to log the user name, date, time and if the login was successful. – Smith Nicky Mar 29 '20 at 19:29
  • For me the log file was located at the root of the project. But it is being generated with the logs as expected. – Shane Creedon Mar 29 '20 at 19:30
  • You are right, it is there. Thank you! Would you be able to guide me in the right direction for logging the username and successful login or not. Since date and time show up in the log? – Smith Nicky Mar 29 '20 at 19:36
  • Yes of course, simply add the username and boolean success/failure to the logger.info() statement and it should do the trick. – Shane Creedon Mar 29 '20 at 19:43
  • Last question. How can I make this less messy, username didn't do this. Mar 29, 2020 1:58:02 PM java.util.logging.LogManager$RootLogger log INFO: Message javax.swing.JLabel[,1,26,87x26,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text= Invalid user.. ,verticalAlignment=CENTER,verticalTextPosition=CENTER] – Smith Nicky Mar 29 '20 at 20:14
  • I think it looks as though you're logging a JLabel object's default toString() method rather than the text of it. I assume this is what you mean by messy. Try log jlabel.getText() for the actual value. – Shane Creedon Mar 29 '20 at 20:25
  • No problem at all – Shane Creedon Mar 29 '20 at 20:38