File Handler doesn't start working until i have a file created

1.2k views Asked by At

I have a loggers.config file that specifies a custom file handler to format log messages to a file. The handler wont work unless a create a default file with the specified file pattern. It then creates a new file with a 0 appended to it as specified by the pattern and starts logging to that. How can I get it to log to a file without specifying a default file first.

Here is the logging file:

handlers=com.daniel.logging.MyFileHandler

# Default global logging level.
# This specifies which kinds of events are logged across
# all loggers.  For any given facility this global level
# can be overriden by a facility specific level
# Note that the ConsoleHandler also has a separate level
# setting to limit messages printed to the console.
.level= INFO

com.daniel.logging.MyFileHandler.level=INFO

# Naming style for the output file: 
com.daniel.logging.MyFileHandler.pattern=daniel%g.log

# Limiting size of output file in bytes -defaulted to 1MB: 
com.daniel.logging.MyFileHandler.limit=1MB

# Number of output files to cycle through, by appending an 
# integer to the base file name: 
com.daniel.logging.MyFileHandler.count=10

# Style of output (Simple or XML): 
com.daniel.logging.MyFileHandler.formatter=com.daniel.logging.MyLogFormatter

# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

In the installer (this is an application logging properties file there was a file created there is a line specifying the logger.config file as logger.properties. A couple of lines later a file was created and the command CMD [@] >> default_file 2>&1. I am not sure what that line does (I'm quite new to bash)

1

There are 1 answers

5
jmehrens On

Check that the user account the program is executing under has write permissions to the file path.

com.daniel.logging.MyFileHandler.limit=1MB

That should be changed to:

com.daniel.logging.MyFileHandler.limit=1048576

Your file pattern is using a relative path. You should use a absolute path or use the patterns for the home or temp directory. The FileHandler will fail to create a new log file if the directory path doesn't exist. The directories have to exist before the FileHandler will create the log file.

If you want to SimpleDateFormat patterns in the file pattern you can do something like:

    public final class MyFileHandler extends FileHandler {

        public MyFileHandler() throws IOException {
            super(toPattern());
        }

        private static String toPattern() throws IOException {
            String defaultPattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
            String p = MyFileHandler.class.getName();
            String v = LogManager.getLogManager().getProperty(p + ".pattern");
            if (v == null) {
                v = defaultPattern;
            }

            try {
                return new SimpleDateFormat(v).format(new Date());   
            } catch (RuntimeException re) {
                throw new IOException(v, re);
            }
        }
    }

If you need rotation support you can reference PackageNameFileHandler as a starting point.