Java JUL使用我自己的处理程序创建文件旋转

时间:2018-07-11 19:41:41

标签: java logging properties

请帮助。 我尝试通过自己的FileHandler创建日志文件旋转:

public class LogHandlerTry extends FileHandler {
    private static LogHandlerTry handler;
    private LogHandlerTry(String pattern) throws IOException, SecurityException {
        super(pattern, true);
        readConfiguration();
    }
    private void readConfiguration() {
        LogManager manager = LogManager.getLogManager();

        String cname = getClass().getName() + ".";
    }
    public static Handler getInstance(String pattern) throws IOException {
        if (handler == null) {
            synchronized (LogHandlerTry.class) {
                if (handler == null) {
                    handler = new LogHandlerTry(pattern);
                }
            }
        }
        return handler;
    } }
enter code here

在这里初始化:

private void setupLogger() {
        LogManager manager = LogManager.getLogManager();

        String level = manager.getProperty(LogHandlerTry.class.getName() + ".level");
        if (level == null || level.isEmpty()) {
            logger.setLevel(Level.FINE);
        }

        logger.setUseParentHandlers(false);
        Handler[] old = logger.getHandlers();
        for (Handler h : old) {
            logger.removeHandler(h);
        }

        String pattern = manager.getProperty(LogHandlerTry.class.getName() + ".pattern");
        if (pattern == null || pattern.isEmpty()) {
            pattern = "../logs/myPath/logName%g.log";
        }

        try {
            logger.addHandler(LogHandlerTry.getInstance(pattern));
        } catch (SecurityException | IOException e) {
            LOGGER.severe("Unable to create log handler for logging.");
        }
    }

在日志记录属性中:

com.myproject.xxx.LogHandlerTry.limit = 1024000 com.myproject.xxx.LogHandlerTry.pattern = ... / logName%g.log

在所需目录中创建了日志文件,但未旋转... 帮助。

0 个答案:

没有答案