Fix checkstyle warnings in logback-config-loader.
[controller.git] / opendaylight / config / logback-config-loader / src / main / java / org / opendaylight / controller / logback / config / loader / impl / LogbackConfigurationLoader.java
1 /**
2  * Copyright (c) 201 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.logback.config.loader.impl;
9
10 import ch.qos.logback.classic.LoggerContext;
11 import ch.qos.logback.classic.joran.JoranConfigurator;
12 import ch.qos.logback.core.joran.spi.JoranException;
13 import ch.qos.logback.core.util.StatusPrinter;
14 import java.io.File;
15 import java.net.URL;
16 import org.slf4j.ILoggerFactory;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * Logback configuration loader.
22  * Strategy:
23  * <ol>
24  * <li>reset actual configuration (probably default configuration)</li>
25  * <li>load all given logback config xml files in given order</li>
26  * </ol>
27  */
28 public final class LogbackConfigurationLoader {
29
30     private static final Logger LOG = LoggerFactory
31             .getLogger(LogbackConfigurationLoader.class);
32
33     /**
34      *  forbidden ctor
35      */
36     private LogbackConfigurationLoader() {
37         throw new UnsupportedOperationException();
38     }
39
40     /**
41      * load given logback configurations in given order, reset existing configuration before applying first one
42      * @param purgeBefore require reset before loading first config
43      * @param args
44      */
45     public static void load(boolean purgeBefore, Object...args) {
46         try {
47             if (purgeBefore) {
48                 resetExistingConfiguration();
49             }
50             for (Object logbackConfig : args) {
51                 load(logbackConfig);
52             }
53         } catch (IllegalStateException e) {
54             LOG.warn("loading of multiple logback configurations failed", e);
55         }
56     }
57
58     /**
59      * purge existing logback configuration
60      */
61     public static void resetExistingConfiguration() {
62         LOG.trace("resetting existing logback configuration");
63         LoggerContext context = getLoggerContext();
64         JoranConfigurator configurator = new JoranConfigurator();
65         configurator.setContext(context);
66         context.reset();
67     }
68
69     /**
70      * @return logback context
71      */
72     private static LoggerContext getLoggerContext() {
73         ILoggerFactory context = LoggerFactory.getILoggerFactory();
74         if (context != null && context instanceof LoggerContext) {
75             // now SLF4J is bound to logback in the current environment
76             return (LoggerContext) context;
77         }
78         throw new IllegalStateException("current logger factory is not supported: " + context);
79     }
80
81     /**
82      * @param logbackConfig
83      * @param reset true if previous configuration needs to get purged
84      */
85     public static void load(Object logbackConfig) {
86         LOG.trace("BEFORE logback reconfig");
87         try {
88             LoggerContext context = getLoggerContext();
89             JoranConfigurator configurator = new JoranConfigurator();
90             configurator.setContext(context);
91             if (logbackConfig instanceof String) {
92                 configurator.doConfigure((String) logbackConfig);
93             } else if (logbackConfig instanceof URL) {
94                 configurator.doConfigure((URL) logbackConfig);
95             } else if (logbackConfig instanceof File) {
96                 configurator.doConfigure((File) logbackConfig);
97             }
98
99             LOG.trace("applied {}", logbackConfig);
100             StatusPrinter.printInCaseOfErrorsOrWarnings(context);
101         } catch (IllegalStateException | JoranException je) {
102             LOG.warn("Logback configuration loading failed: {}", logbackConfig);
103         }
104         LOG.trace("AFTER logback reconfig");
105     }
106
107 }