0

I am trying to create a java logger file that will write output to a file within the project folder (NetBeans, MacOS). I'm doing this for a school project so I can't use Log4j. I have reviewed previously asked questions regarding this subject and tried to implement those solutions but I'm still not seeing any success. Do I need to extend the file path, or is there a major component that I'm missing out on? Any steps in the right direction would be helpful, thank you.

I have tried extending the file path to go from the users folder all the way down to the individual project folder in netbeans. I have looked at quite a few resources and examples but whatever element I'm missing I haven't managed to find out.

public class log {
   private final static Logger LOGGER = 
Logger.getLogger(log.class.getName());
   private final static FileHandler handleLog = null;



   public static void start() throws IOException, 
SecurityException{
    FileHandler handleLog = new 
FileHandler("/Users/cassie/NetBeansProjects/GlobalConsulting- 
   userLog.%u.%g.txt", 1024 * 1024, 10, true);
    SimpleFormatter simple = new SimpleFormatter();
    handleLog.setFormatter(simple);
    LOGGER.addHandler(handleLog);
    LOGGER.setLevel(Level.INFO);

   }

   public static void main(String[] args) throws IOException, 
SecurityException{
       start();
       LOGGER.info("------------------START--------------------");

   }
}

When I call it in another file, this is how I did it.

LOGGER.log(Level.INFO, "{0} logged in", currentUser.getUsername());

What I was hoping was that when the program was run, the output (user logged in) would print to a log file within the project folder and would update 10 times before rewriting. I don't get any error messages but all output goes straight to console instead.

  • The Logger class you used in your class, is it from any Logging API? – Ramu Nov 10 '19 at 16:03
  • Ignore my previous comment. May I know how are calling it the another file? – Ramu Nov 10 '19 at 16:35
  • I called it as such : LOGGER.log(Level.INFO, "{0} logged in", currentUser.getUsername()); –  Nov 11 '19 at 19:27
  • I mean, how are you executing the other class? Does the other class has it's own main method? If possible, please post the code for the other class as well. – Ramu Nov 11 '19 at 22:36

1 Answers1

0

I don't get any error messages but all output goes straight to console instead.

Use the printConfig in your code to determine the state of the logger tree. In order to see output to a log file you need the following:

  1. The filehandler is created without exception. From your question it seems like that is working.
  2. You should be able to see the log file was at least created (and empty) when your program ran. By default, if the filehandler can't resolve the path it will either throw an error or create a log file in the user's home directory named java0.log. This usually happens if you specify a file path and the directories don't exist before you run the program.
  3. The filehandler has to be attached to the logger tree at loggable node or a parent logger. If the file handler is attached to a child logger from your program point of view it won't see log messages. Log records travel up the logger tree to the parent handlers. Usually the ConsoleHandler is installed on the root logger so it will see everything unlike your filehandler which is installed on a child of the root.
  4. The filehandler has to have the correct level. Use Level.ALL for the handler.
  5. The filehandler should not have a filter installed (null). The JDK doesn't come with any so this shouldn't be an issue.
  6. The logger level has to be set so that you are actually generating a log record to the handlers.
  7. Another function removed or reset the log manager which is deleting your configuration. The SecurityManager can determine this but it is most likely not the cause since you don't use any 3rd party libs.

Answer those points and you should be able to determine why you don't see a log file.

jmehrens
  • 10,580
  • 1
  • 38
  • 47