Java JUL create file rotation with my own handler

By | July 12, 2018
Questions:

please help.
I try to create logging file rotation by my own 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

Init 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.");
        }
    }

in logging properties:

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

The Log file is created in the desired directory, but not rotated…
Help.

Answers:

Leave a Reply

Your email address will not be published. Required fields are marked *