aboutsummaryrefslogtreecommitdiffstats
path: root/container-accesslogging/src/main/java/com/yahoo/container/logging/AccessLogHandler.java
blob: a31e7b3b8490f861b540e24d7dac47361540caab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.logging;

import com.yahoo.container.core.AccessLogConfig;

import static com.yahoo.container.core.AccessLogConfig.FileHandler.RotateScheme.DATE;

import java.util.logging.Logger;

/**
 * @author Bjorn Borud
 */
class AccessLogHandler {

    public Logger access = Logger.getAnonymousLogger();
    private LogFileHandler logFileHandler;

    public AccessLogHandler(AccessLogConfig.FileHandler config) {
        access.setUseParentHandlers(false);

        logFileHandler = new LogFileHandler(config.rotateScheme(), config.compressOnRotation());

        logFileHandler.setFilePattern(config.pattern());
        logFileHandler.setRotationTimes(config.rotation());

        if (config.rotateScheme() == DATE)
            createSymlink(config, logFileHandler);


        LogFormatter lf = new LogFormatter();
        lf.messageOnly(true);
        this.logFileHandler.setFormatter(lf);
        access.addHandler(this.logFileHandler);
    }

    private void createSymlink(AccessLogConfig.FileHandler config, LogFileHandler handler) {
        if (!config.symlink().isEmpty())
            handler.setSymlinkName(config.symlink());
    }

    public void shutdown() {
        logFileHandler.close();
        access.removeHandler(logFileHandler);

        if (logFileHandler!=null)
            logFileHandler.shutdown();
    }

    void rotateNow() {
        logFileHandler.rotateNow();
    }
}