aboutsummaryrefslogtreecommitdiffstats
path: root/logserver/src/main/java/com/yahoo/logserver/handlers/logmetrics/LogMetricsPlugin.java
blob: d2c217fb4e0a6fcd66a81b54342cef8b3069f57a (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 Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/*
 * $Id$
 */
package com.yahoo.logserver.handlers.logmetrics;

import java.util.logging.Logger;

import com.yahoo.logserver.Server;
import com.yahoo.plugin.Config;
import com.yahoo.plugin.Plugin;


public class LogMetricsPlugin implements Plugin {
    private static final Logger log = Logger.getLogger(LogMetricsPlugin.class.getName());
    private LogMetricsHandler logMetricsHandler;
    private final Server server = Server.getInstance();

    public String getPluginName() {
        return "logmetrics";
    }

    /**
     * Initialize the logmetrics plugin
     *
     * @param config Plugin config object, keys used:
     *               <code>thread</code> - name of handler thread this plugin runs in
     */
    public void initPlugin(Config config) {
        if (logMetricsHandler != null) {
            log.finer("LogMetricsPlugin doubly initialized");
            throw new IllegalStateException(
                    "plugin already initialized: " + getPluginName());
        }
        String threadName = config.get("thread", getPluginName());
        logMetricsHandler = new LogMetricsHandler();
        server.registerLogHandler(logMetricsHandler, threadName);
    }

    /**
     * Shut down the logmetrics plugin.
     */
    public void shutdownPlugin() {
        if (logMetricsHandler == null) {
            log.finer("LogMetricsPlugin shutdown before initialize");
            throw new IllegalStateException("plugin not initialized: " +
                                                    getPluginName());
        }
        server.unregisterLogHandler(logMetricsHandler);
        logMetricsHandler = null;
    }
}