0

I'm learning this logger from java and trying to call the class MyLogger from the main class but it seems not to be working

MyLogger.java

public class MyLogger {

    public MyLogger() {

        Logger logger = Logger.getLogger("MyLog");
        FileHandler fh;
        try {

        fh = new FileHandler("c:\\opt\\MyLogFile.log", true);
        logger.addHandler(fh);
        logger.setLevel(Level.ALL);
        SimpleFormatter formatter = new SimpleFormatter();
        fh.setFormatter(formatter);
        logger.log(Level.WARNING,"My first log");

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

Client.java

public class Client {
    private final static Logger LOGGER = Logger.getLogger(Client.class.getName());

    public static void main(String[] args) {
        LOGGER.setLevel(Level.SEVERE);
        LOGGER.getName();
        LOGGER.getClass();
        LOGGER.info("ABC");


    }
}
  1. Why is it not calling the MyLogger.java to create the log file?
  2. Is there a beter way to log the eclipse console rather than putting LOGGER on each methods for each class?
  3. Why some uses flush() before closing the log file?
2
  • Why do you think it should use your MyLogger? Commented Jun 24, 2014 at 2:43
  • 1
    where in the Client.java does it even mention MyLogger? Commented Jun 24, 2014 at 2:45

1 Answer 1

1

Why is it not calling the MyLogger.java to create the log file?

If it refers to running Client, then that's because there is no relation between that class and your MyLogger class.

Is there a beter way to log the eclipse console rather than putting LOGGER on each methods for each class?

Better is very subjective. The majority of source code I've seen use the paradigm you're using in your Client class. That is, declaring a static final Logger with the name of enclosing class and using it where you need to log something within that class.

Some applications make use of AOP concepts for logging.

Why some uses flush() before closing the log file?

Some implementations may buffer log messages so as not to make many small IO system calls, but instead make few big ones. Closing the log file may not flush the buffer, so you'll want to flush it explicitly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.