Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
730 views
in Technique[技术] by (71.8m points)

multithreading - Log File not being updated after few seconds while using Logger and FileHandler Class of Java.util

I am using the following Class 'LoggerUtil' for logging to console and to a file 'logfile.log'. The logging to console is working fine. However, logging to 'logfile.log' file stops after few logs. Any suggestions in locating the bug would be welcome. I am attaching the code below:

public class LoggerUtil {   
  public static final String LOGGERNAME = "project.logging";

  static {
      try {
          Logger.getLogger(LOGGERNAME).setUseParentHandlers(false);
          Handler ch = new ConsoleHandler();
          Handler fh = new FileHandler("logfile.log");
          SimpleFormatter sf = new SimpleFormatter();
          fh.setFormatter(sf);
          Logger.getLogger(LOGGERNAME).addHandler(ch);
          Logger.getLogger(LOGGERNAME).addHandler(fh);
          setHandlersLevel(Level.ALL);
      } catch (IOException | SecurityException ex) {
          Logger.getLogger(LoggerUtil.class.getName()).log(Level.SEVERE, null, ex);
      }
  }

  public static void setHandlersLevel(Level level) {
      Handler[] handlers = Logger.getLogger(LOGGERNAME).getHandlers();
      for (Handler h : handlers) {
          h.setLevel(level);
      }
      Logger.getLogger(LOGGERNAME).setLevel(level);
  }

  public static Logger getLogger() {
      return Logger.getLogger(LOGGERNAME);
  }
}

I call the following code from various places in my project to log:

LoggerUtil.getLogger().log(Level.INFO, "Message to be logged");

Kindly note, my project is multithreaded. Various threads use the same file for logging. Could this be a concurrency issue or is that just a red herring!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Make a hard reference to your logger. Rule of thumb is to use a static final field in your class.

public static final String LOGGERNAME = "project.logging";
//Pin logger in memory.
private static final Logger logger = Logger.getLogger(LOGGERNAME);

From the Logger.getLogger(String) documentation:

Note: The LogManager may only retain a weak reference to the newly created Logger. It is important to understand that a previously created Logger with the given name may be garbage collected at any time if there is no strong reference to the Logger.

When the logger is garbage collected your ConsoleHandler and FileHandler are not reattached to the new logger.

Use FindBugs because it detects this bug pattern:

LG: Potential lost logger changes due to weak reference in OpenJDK (LG_LOST_LOGGER_DUE_TO_WEAK_REFERENCE)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...