aboutsummaryrefslogtreecommitdiffstats
path: root/vespalog/src/main/java/com/yahoo/log/VespaLogHandler.java
blob: 395aebcb800f3c106dd9af18b35cd907910cd656 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.log;

import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.StreamHandler;

import static java.util.Map.entry;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO;

/**
 * @author Bjorn Borud
 * @author arnej27959
 */
@SuppressWarnings("deprecation")
class VespaLogHandler extends StreamHandler {

    // Reduce log level for some loggers
    private static final Function<Level, Level> INFO_TO_FINE = level -> level == INFO ? FINE : level;
    private static final Map<String, Function<Level, Level>> loggersWithAlteredLogLevel = Map.ofEntries(
            entry("com.yahoo.vespa.spifly.repackaged.spifly.BaseActivator", INFO_TO_FINE),
            entry("org.eclipse.jetty.server.Server", INFO_TO_FINE),
            entry("org.eclipse.jetty.server.handler.ContextHandler", INFO_TO_FINE),
            entry("org.eclipse.jetty.server.AbstractConnector", INFO_TO_FINE),
            entry("org.eclipse.jetty.util.HostPort", __ -> FINE)
    );

    private final LogTarget logTarget;
    private final String serviceName;
    private final String appPrefix;
    private final LevelControllerRepo repo;
    private final RejectFilter logRejectFilter;

    /**
     * Construct handler which logs to specified logTarget.  The logTarget
     * may be of the following formats:
     *
     * <DL>
     *  <DT> <code>fd:&lt;number&gt;</code>
     *  <DD> Log to specified file descriptor number.  Only "fd:2"
     *       is supported.
     *
     *  <DT> <code>file:&lt;filename&gt;</code>
     *  <DD> Log to specified file in append mode
     * </DL>
     */
    VespaLogHandler(LogTarget logTarget,
                    LevelControllerRepo levelControllerRepo, String serviceName, String applicationPrefix) {
        this.logTarget = logTarget;
        this.serviceName = serviceName;
        this.appPrefix = applicationPrefix;
        this.repo = levelControllerRepo;
        this.logRejectFilter = RejectFilter.createDefaultRejectFilter();
        initialize();
    }

    /**
     * Publish a log record into the Vespa log target.
     */
    @Override
    public synchronized void publish(LogRecord record) {
        String loggerName = record.getLoggerName();
        Level level = possiblyReduceLogLevel(loggerName, record.getLevel());

        LevelController ctrl = getLevelControl(loggerName);
        if (!ctrl.shouldLog(level)) {
            return;
        }

        if (logRejectFilter.shouldReject(record.getMessage())) {
            return;
        }

        try {
            // provokes rotation of target
            setOutputStream(logTarget.open());
        } catch (RuntimeException e) {
            LogRecord r = new LogRecord(Level.SEVERE, "Unable to open file target");
            r.setThrown(e);
            emergencyLog(r);
            setOutputStream(System.err);
        }
        super.publish(record);
        flush();
        closeFileTarget();
    }

    private static Level possiblyReduceLogLevel(String loggerName, Level level) {
        if (loggerName == null) return level;

        var levelMapper = loggersWithAlteredLogLevel.get(loggerName);
        return levelMapper == null ? level : levelMapper.apply(level);
    }

    LevelController getLevelControl(String component) {
        return repo.getLevelController(component);
    }

    /**
     * Initialize the handler.  The main invariant is that
     * outputStream is always set to something valid when this method
     * returns.
     */
    private void initialize () {
        try {
            setFormatter(new VespaFormatter(serviceName, appPrefix));
            setLevel(LogLevel.ALL);
            setEncoding("UTF-8");
            // System.err.println("initialize vespa logging, default level: "+defaultLogLevel);
            setOutputStream(logTarget.open());
        }
        catch (UnsupportedEncodingException uee) {
            LogRecord r = new LogRecord(Level.SEVERE, "Unable to set log encoding to UTF-8");
            r.setThrown(uee);
            emergencyLog(r);
        }
        catch (RuntimeException e) {
            LogRecord r = new LogRecord(Level.SEVERE, "Unable to open file target");
            r.setThrown(e);
            emergencyLog(r);
            setOutputStream(System.err);
        }
    }


    /** Closes the target log file, if there is one */
    synchronized void closeFileTarget() {
        try {
            logTarget.close();
        }
        catch (RuntimeException e) {
            LogRecord r = new LogRecord(Level.WARNING, "Unable to close log");
            r.setThrown(e);
            emergencyLog(r);
        }
    }

    /**
     * If the logging system experiences problems we can't be expected
     * to log it through normal channels, so we have an emergency log
     * method which just uses STDERR for formatting the log messages.
     * (Which might be right, and might be wrong).
     *
     * @param record The log record to be logged
     */
    private void emergencyLog(LogRecord record) {
        record.setLoggerName(VespaLogHandler.class.getName());
        System.err.println(getFormatter().format(record));
    }

    public void cleanup() {
        repo.close();
    }
}