0

我有一个 loggers.config 文件,它指定了一个自定义文件处理程序以将日志消息格式化为文件。除非使用指定的文件模式创建默认文件,否则处理程序将无法工作。然后它会创建一个新文件,并按照模式指定的附加 0 并开始记录到该文件。如何在不先指定默认文件的情况下将其记录到文件中。

这是日志文件:

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

在安装程序中(这是一个应用程序日志记录属性文件,创建了一个文件,其中有一行将 logger.config 文件指定为 logger.properties。几行之后,创建了一个文件并使用命令 CMD [@] >> default_file 2>&1. 我不确定那行是做什么的(我对 bash 很陌生)

4

1 回答 1

0

检查程序正在执行的用户帐户是否具有文件路径的写入权限。

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

那应该改为:

com.daniel.logging.MyFileHandler.limit=1048576

您的文件模式正在使用相对路径。您应该使用绝对路径或使用主目录或临时目录的模式。如果目录路径不存在,FileHandler 将无法创建新的日志文件。在 FileHandler 创建日志文件之前,这些目录必须存在。

如果您想在文件模式中使用 SimpleDateFormat 模式,您可以执行以下操作:

    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);
            }
        }
    }

如果您需要旋转支持,您可以参考PackageNameFileHandler作为起点。

于 2014-11-20T22:37:13.443 回答