Java util Logger not writing to file

 ̄綄美尐妖づ 提交于 2021-02-05 08:15:50

问题


I have written a silly Logger for my application. It writes into a log file info, warning and severe messages. It worked perfectly some time ago, but suddenly, it stopped working for no aparent reason. It keeps creating the log file if it does not exist, but no content is written. I can't find why it is not working now. I would thank some help.

Debugging with my IDE, I realise that messages are passed correctly to logger, and logger.info() is being executed.

public class FsLogger {

    private static FsLogger instance;

    private final String filename = "Logs/LogFsForensics.log";
    private final String loggerName = "FsLogger";
    private static Logger logger;   
        private FileHandler fh;
        private final int limit = 1024*10000; //10 MB
        private SimpleFormatter sf;

    private FsLogger() {
        logger =  Logger.getLogger(loggerName);

        sf = new SimpleFormatter();
        try {
            fh = new FileHandler(filename, limit, 1, true);
            sf = new SimpleFormatter();
            fh.setFormatter(sf);

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

    public static FsLogger getInstance() {
        if(instance == null) {
            instance = new FsLogger();
        }
        return instance;
    }

    public void info(String message) {
        logger.info(message);
    }

    public void warning(String message) {
        logger.warning(message);        
    }

    public void severe(String message) {
        logger.severe(message);
    }

Example of a logger call:

private FsLogger logger = FsLogger.getInstance();
logger.severe(e1.getMessage());

回答1:


I've tried to execute your code and get the following output in both the console and file:

May 30, 2019 12:35:11 PM io.yooksi.Main main
INFO: This is a test.
May 30, 2019 12:35:11 PM io.yooksi.FsLogger info
INFO: This is an info message
May 30, 2019 12:35:11 PM io.yooksi.FsLogger warning
WARNING: This is a warning
May 30, 2019 12:35:11 PM io.yooksi.FsLogger severe
SEVERE: This is a sever error message

This means that your code is not at fault, so you can try the following things:

  • Make sure that the following code properly outputs to console:

    Logger logger = Logger.getLogger("test");
    logger.info("This is a test.");
    
  • Make sure that the file is not in read-only state.
  • Try manually writing to file using either FileWriter or PrintWriter.

If the first test fails to print anything to console then read the following document where you can find a list of common reasons why Java logger fails to works and concrete steps how to resolve them (at least the one related to configuration):

Why is my Java Logging not working




回答2:


From the code you are using a relative path of Logs/LogFsForensics.log This means that if the current working directory if the process changes the path will be invalid. When that happens the FileHandler will create a file in the default location. Use an absolute path or specify one of the FileHandler special components. For example:

/Logs/LogFsForensics.log as absolute path or %h/Logs/LogFsForensics.log as relative from home directory.

You can modify the test program from Logging not showing to print the logger tree and and handlers. It is possible the levels are not set correctly or the handler was not installed.

Looking at the code, I would think that you want getInstance to be synchronized so you never attempt to create more than one handler.



来源:https://stackoverflow.com/questions/56376223/java-util-logger-not-writing-to-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!