2 * Copyright (c) 2013 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
12 * Generated from: yang module name: config-test yang module local name: testing
13 * Generated by: org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator
14 * Generated at: Fri Sep 27 14:06:33 CEST 2013
16 * Do not modify this file unless it is present under src/main directory
18 package org.opendaylight.controller.config.yang.logback.config;
20 import java.io.Closeable;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
26 import org.slf4j.LoggerFactory;
28 import ch.qos.logback.classic.LoggerContext;
29 import ch.qos.logback.core.status.StatusBase;
30 import ch.qos.logback.core.status.StatusListener;
31 import ch.qos.logback.core.status.StatusManager;
33 public class LogbackStatusListener implements StatusListener, LogbackRuntimeMXBean, Closeable {
35 private final List<StatusTO> receivedStatuses;
36 private final LogbackRuntimeRegistrator rootRuntimeBeanRegistratorWrapper;
37 private LogbackRuntimeRegistration reg;
39 public LogbackStatusListener(LogbackRuntimeRegistrator rootRuntimeBeanRegistratorWrapper) {
40 receivedStatuses = new ArrayList<>();
41 this.rootRuntimeBeanRegistratorWrapper = rootRuntimeBeanRegistratorWrapper;
45 public synchronized List<StatusTO> getStatusTO() {
46 return Collections.unmodifiableList(receivedStatuses);
50 public synchronized void reset() {
51 receivedStatuses.clear();
54 public LogbackRuntimeRegistration register() {
55 reg = registerToJMX(rootRuntimeBeanRegistratorWrapper);
60 private LogbackRuntimeRegistration registerToJMX(LogbackRuntimeRegistrator rootRuntimeBeanRegistratorWrapper) {
61 return rootRuntimeBeanRegistratorWrapper.register(this);
64 private synchronized void registerToLogback() {
65 LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
66 final StatusManager statusManager = context.getStatusManager();
68 statusManager.remove(this);
71 statusManager.add(this);
72 addInitialStatuses(statusManager);
75 private void addInitialStatuses(StatusManager statusManager) {
76 for (ch.qos.logback.core.status.Status status : statusManager.getCopyOfStatusList()) {
77 addStatusEvent(status);
82 public synchronized void addStatusEvent(ch.qos.logback.core.status.Status status) {
83 receivedStatuses.add(transformStatus(status));
86 private StatusTO transformStatus(ch.qos.logback.core.status.Status status) {
87 StatusTO transformed = new StatusTO();
89 transformed.setDate(status.getDate());
90 transformed.setLevel(transformStatusLevel(status.getLevel()));
91 transformed.setMessage(status.getMessage());
96 private String transformStatusLevel(int status) {
100 case StatusBase.WARN:
102 case StatusBase.ERROR:
105 throw new IllegalStateException("Unknown status level " + status);
110 public void close() throws IOException {
113 unregisterFromLogback();
116 private void unregisterFromLogback() {
117 LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
118 final StatusManager statusManager = context.getStatusManager();
119 statusManager.remove(this);