aboutsummaryrefslogtreecommitdiffstats
path: root/vespalog/src/main/java/com/yahoo/log/LogSetup.java
blob: e000684db7fae12516af6c1a5604bd971bc68912 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Timer;
import java.util.logging.FileHandler;
import java.util.logging.Filter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

/**
 * Sets up Vespa logging. Call a setup method to set up this.
 *
 * @author  Bjorn Borud
 * @author arnej27959
 */
public class LogSetup {

    private static final Timer taskRunner = new Timer(true);

    static Timer getTaskRunner() { return taskRunner; }

    /** The log handler used by this */
    private static VespaLogHandler logHandler;

    private static ZooKeeperFilter zooKeeperFilter = null;

    /** Clear all handlers registered in java.util.logging framework */
    public static void clearHandlers () {
        Enumeration<String> names = LogManager.getLogManager().getLoggerNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            Logger logger = Logger.getLogger(name);

            Handler[] handlers = logger.getHandlers();
            for (Handler handler : handlers) {
                logger.removeHandler(handler);
            }
        }
    }

    private static boolean isInitialized = false;

    /**
     * Every Vespa application should call initVespaLogging exactly
     * one time.  This should be done from the main() method or from a
     * static initializer in the main class.  The library will pick up
     * the environment variables usually set by the Vespa
     * config-sentinel (VESPA_LOG_LEVEL, VESPA_LOG_TARGET,
     * VESPA_SERVICE_NAME, VESPA_LOG_CONTROL_DIR) but it's possible to
     * override these by setting system properties before calling
     * initVespaLogging.  This may be useful for unit testing etc:
     * <br>
     * System.setProperty("vespa.log.level", "all")
     * <br>
     * System.setProperty("vespa.log.target", "file:foo.log")
     * <br>
     * System.setProperty("vespa.service.name", "my.name")
     * <br>
     * System.setProperty("vespa.log.control.dir", ".")
     * <br>
     * System.setProperty("vespa.log.control.file", "my.logcontrol")
     * <br>
     * vespa.log.control.file is used if it's set, otherwise it's
     * vespa.log.control.dir + "/" + vespa.service.name + ".logcontrol"
     * if both of those variables are set, otherwise there will be no
     * runtime log control.
     *
     * @param programName the name of the program that is running;
     * this is added as a prefix to the logger name to form the
     * "component" part of the log message.  (Usually the logger name
     * is the name of the class that logs something, so the
     * programName should be kept short and simple.)
     **/
    public static void initVespaLogging(String programName) {
        if (isInitialized) {
            System.err.println("WARNING: initVespaLogging called twice");
        }
        isInitialized = true;

        // prefer Java system properties
        String logLevel   = System.getProperty("vespa.log.level");
        String logTarget  = System.getProperty("vespa.log.target");
        String logService = System.getProperty("vespa.service.name");
        String logControlDir  = System.getProperty("vespa.log.control.dir");
        String logControlFile = System.getProperty("vespa.log.control.file");
        if (programName == null || programName.equals("")) {
            throw new RuntimeException("invalid programName: " + programName);
        }

        // then try environment values
        if (logTarget == null)      logTarget = System.getenv("VESPA_LOG_TARGET");
        if (logService == null)     logService = System.getenv("VESPA_SERVICE_NAME");
        if (logControlDir == null)  logControlDir = System.getenv("VESPA_LOG_CONTROL_DIR");
        if (logControlFile == null) logControlFile = System.getenv("VESPA_LOG_CONTROL_FILE");
        if (logLevel == null)       logLevel = System.getenv("VESPA_LOG_LEVEL");

        // then hardcoded defaults
        if (logTarget == null) logTarget = "fd:2";
        if (logLevel == null) logLevel = "all -debug -spam";

        if (logControlFile == null &&
            logControlDir != null &&
            logService != null &&
            !logService.equals("") &&
            !logService.equals("-"))
        {
            logControlFile = logControlDir + "/" + logService + ".logcontrol";
        }

        // for backwards compatibility - XXX should be removed
        if (logService == null) logService = System.getProperty("config.id");
        if (logService == null) logService = "-";

        System.setProperty("vespa.service.name", logService);
        System.setProperty("vespa.program.name", programName);

        try {
            initInternal(logTarget, logService, logControlFile, programName, logLevel);
        } catch (FileNotFoundException e) {
            throw new RuntimeException("Unable to initialize logging", e);
        }
    }

    private static LogTarget getLogTargetFromString(String target) throws FileNotFoundException {
        if ("fd:2".equals(target)) {
            return new StderrLogTarget();
        } else if ("fd:1".equals(target)) {
            return new StdoutLogTarget();
        } else if (target.startsWith("file:")) {
            return new FileLogTarget(new File(target.substring(5)));
        }
        throw new IllegalArgumentException("Target '" + target + "' is not a valid target");
    }

    private static void initInternal(String target,
                                     String service,
                                     String logCtlFn,
                                     String app,
                                     String lev) throws FileNotFoundException {
        clearHandlers();

        if (app != null && app.length() > 64) app = app.substring(0, 63);

        if (logHandler != null) {
            logHandler.cleanup();
            Logger.getLogger("").removeHandler(logHandler);
        }
        Logger.getLogger("").setLevel(Level.ALL);
        logHandler = new VespaLogHandler(getLogTargetFromString(target), new VespaLevelControllerRepo(logCtlFn, lev, app), service, app);
        String zookeeperLogFile = System.getProperty("zookeeper_log_file_prefix");
        if (zookeeperLogFile != null) {
            zooKeeperFilter = new ZooKeeperFilter(zookeeperLogFile);
            logHandler.setFilter(zooKeeperFilter);
        }
        Logger.getLogger("").addHandler(logHandler);
    }

    static VespaLogHandler getLogHandler() {
        return logHandler;
    }

    /** perform cleanup */
    public static void cleanup() {
         if (zooKeeperFilter != null)
            zooKeeperFilter.close();
    }

    /**
     * Class that has an isLoggable methods that handles log records that
     * start with "org.apache.zookeeper." or "org.apache.curator"
     * (writing them to a log file with the prefix specified in the system property
     * zookeeper_log_file_prefix) and returning false.
     * For other log records, isLoggable returns true
     */
    static class ZooKeeperFilter implements Filter {

        private static final int FILE_SIZE = 10*1024*1024; // Max 10 Mb per log file
        private static final int maxFilesCount = 10; // Keep at most 10 log files

        private FileHandler fileHandler;

        ZooKeeperFilter(String logFilePrefix) {
            String logFilePattern = logFilePrefix + ".%g.log";
            try {
                fileHandler = new FileHandler(logFilePattern, FILE_SIZE, maxFilesCount, true);
                fileHandler.setFormatter(new VespaFormatter());
            } catch (IOException e) {
                System.out.println("Not able to create " + logFilePattern);
                fileHandler = null;
            }
        }

        /**
         * Return true if loggable (ordinary log record), returns false if this filter
         * logs the log record itself
         *
         * @param record a #{@link LogRecord}
         * @return true if loggable, false otherwise
         */
        @Override
        public boolean isLoggable(LogRecord record) {
            if (record.getLoggerName() == null) return true;
            if (!record.getLoggerName().startsWith("org.apache.zookeeper.") &&
                    !record.getLoggerName().startsWith("org.apache.curator")) {
                return true;
            }
            fileHandler.publish(record);
            return false;
        }

        public void close() {
            fileHandler.close();
        }
    }

}