Netconf-cli compilable and included in project
[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 java.io.File;
11 import java.net.URL;
12
13 import org.slf4j.ILoggerFactory;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 import ch.qos.logback.classic.LoggerContext;
18 import ch.qos.logback.classic.joran.JoranConfigurator;
19 import ch.qos.logback.core.joran.spi.JoranException;
20 import ch.qos.logback.core.util.StatusPrinter;
21
22 /**
23  * Logback configuration loader.
24  * Strategy:
25  * <ol>
26  * <li>reset actual configuration (probably default configuration)</li>
27  * <li>load all given logback config xml files in given order</li>
28  * </ol>
29  */
30 public final class LogbackConfigurationLoader {
31
32     private static final Logger LOG = LoggerFactory
33             .getLogger(LogbackConfigurationLoader.class);
34
35     /**
36      *  forbidden ctor
37      */
38     private LogbackConfigurationLoader() {
39         throw new UnsupportedOperationException();
40     }
41
42     /**
43      * load given logback configurations in given order, reset existing configuration before applying first one
44      * @param purgeBefore require reset before loading first config
45      * @param args
46      */
47     public static void load(boolean purgeBefore, Object...args) {
48         try {
49             if (purgeBefore) {
50                 resetExistingConfiguration();
51             }
52             for (Object logbackConfig : args) {
53                 load(logbackConfig);
54             }
55         } catch (IllegalStateException e) {
56             LOG.warn("loading of multiple logback configurations failed", e);
57         }
58     }
59
60     /**
61      * purge existing logback configuration
62      */
63     public static void resetExistingConfiguration() {
64         LOG.trace("resetting existing logback configuration");
65         LoggerContext context = getLoggerContext();
66         JoranConfigurator configurator = new JoranConfigurator();
67         configurator.setContext(context);
68         context.reset();
69     }
70
71     /**
72      * @return logback context
73      */
74     private static LoggerContext getLoggerContext() {
75         ILoggerFactory context = LoggerFactory.getILoggerFactory();
76         if (context != null && context instanceof LoggerContext) {
77             // now SLF4J is bound to logback in the current environment
78             return (LoggerContext) context;
79         }
80         throw new IllegalStateException("current logger factory is not supported: " + context);
81     }
82
83     /**
84      * @param logbackConfig
85      * @param reset true if previous configuration needs to get purged
86      */
87     public static void load(Object logbackConfig) {
88         LOG.trace("BEFORE logback reconfig");
89         try {
90             LoggerContext context = getLoggerContext();
91             JoranConfigurator configurator = new JoranConfigurator();
92             configurator.setContext(context);
93             if (logbackConfig instanceof String) {
94                 configurator.doConfigure((String) logbackConfig);
95             } else if (logbackConfig instanceof URL) {
96                 configurator.doConfigure((URL) logbackConfig);
97             } else if (logbackConfig instanceof File) {
98                 configurator.doConfigure((File) logbackConfig);
99             }
100
101             LOG.trace("applied {}", logbackConfig);
102             StatusPrinter.printInCaseOfErrorsOrWarnings(context);
103         } catch (IllegalStateException | JoranException je) {
104             LOG.warn("Logback configuration loading failed: {}", logbackConfig);
105         }
106         LOG.trace("AFTER logback reconfig");
107     }
108
109 }