2 * Copyright (c) 201 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.logback.config.loader.impl;
13 import org.slf4j.ILoggerFactory;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
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;
23 * Logback configuration loader.
26 * <li>reset actual configuration (probably default configuration)</li>
27 * <li>load all given logback config xml files in given order</li>
30 public final class LogbackConfigurationLoader {
32 private static final Logger LOG = LoggerFactory
33 .getLogger(LogbackConfigurationLoader.class);
38 private LogbackConfigurationLoader() {
39 throw new UnsupportedOperationException();
43 * load given logback configurations in given order, reset existing configuration before applying first one
44 * @param purgeBefore require reset before loading first config
47 public static void load(boolean purgeBefore, Object...args) {
50 resetExistingConfiguration();
52 for (Object logbackConfig : args) {
55 } catch (IllegalStateException e) {
56 LOG.warn("loading of multiple logback configurations failed", e);
61 * purge existing logback configuration
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);
72 * @return logback context
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;
80 throw new IllegalStateException("current logger factory is not supported: " + context);
84 * @param logbackConfig
85 * @param reset true if previous configuration needs to get purged
87 public static void load(Object logbackConfig) {
88 LOG.trace("BEFORE logback reconfig");
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);
101 LOG.trace("applied {}", logbackConfig);
102 StatusPrinter.printInCaseOfErrorsOrWarnings(context);
103 } catch (IllegalStateException | JoranException je) {
104 LOG.warn("Logback configuration loading failed: {}", logbackConfig);
106 LOG.trace("AFTER logback reconfig");