Issue #5872 - DynamicMBean for JettyLoggerFactory

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2021-01-22 10:08:46 -06:00
parent 87ab8bda8f
commit 5f9ea7804a
No known key found for this signature in database
GPG Key ID: 2D0E1FB8FE4B68B4
3 changed files with 24 additions and 39 deletions

View File

@ -35,7 +35,7 @@ import javax.management.ReflectionException;
import org.slf4j.ILoggerFactory; import org.slf4j.ILoggerFactory;
import org.slf4j.Logger; import org.slf4j.Logger;
public class JettyLoggerFactory implements ILoggerFactory, DynamicMBean, JettyLoggerFactoryMBean public class JettyLoggerFactory implements ILoggerFactory, DynamicMBean
{ {
private final JettyLoggerConfiguration configuration; private final JettyLoggerConfiguration configuration;
private final JettyLogger rootLogger; private final JettyLogger rootLogger;
@ -254,11 +254,6 @@ public class JettyLoggerFactory implements ILoggerFactory, DynamicMBean, JettyLo
return ret; return ret;
} }
/*
setLoggerLevel(String loggerName, String levelName);
String getLoggerLevel(String loggerName);
*/
@Override @Override
public Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException, ReflectionException public Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException, ReflectionException
{ {

View File

@ -1,25 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.logging;
public interface JettyLoggerFactoryMBean
{
int getLoggerCount();
String[] getLoggerNames();
boolean setLoggerLevel(String loggerName, String levelName);
String getLoggerLevel(String loggerName);
}

View File

@ -19,7 +19,6 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.stream.Stream; import java.util.stream.Stream;
import javax.management.JMX;
import javax.management.MBeanAttributeInfo; import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo; import javax.management.MBeanInfo;
import javax.management.MBeanServer; import javax.management.MBeanServer;
@ -52,28 +51,44 @@ public class JMXTest
MBeanAttributeInfo attr = Stream.of(attributeInfos).filter((a) -> a.getName().equals("LoggerNames")).findFirst().orElseThrow(); MBeanAttributeInfo attr = Stream.of(attributeInfos).filter((a) -> a.getName().equals("LoggerNames")).findFirst().orElseThrow();
assertThat("attr", attr.getDescription(), is("List of Registered Loggers by Name.")); assertThat("attr", attr.getDescription(), is("List of Registered Loggers by Name."));
JettyLoggerFactoryMBean mbean = JMX.newMBeanProxy(mbeanServer, objectName, JettyLoggerFactoryMBean.class); // Do some MBean attribute testing
int loggerCount;
// Only the root logger. // Only the root logger.
assertEquals(1, mbean.getLoggerCount()); loggerCount = (int)mbeanServer.getAttribute(objectName, "LoggerCount");
assertEquals(1, loggerCount);
JettyLoggerFactory loggerFactory = (JettyLoggerFactory)LoggerFactory.getILoggerFactory(); JettyLoggerFactory loggerFactory = (JettyLoggerFactory)LoggerFactory.getILoggerFactory();
JettyLogger child = loggerFactory.getJettyLogger("org.eclipse.jetty.logging"); JettyLogger child = loggerFactory.getJettyLogger("org.eclipse.jetty.logging");
JettyLogger parent = loggerFactory.getJettyLogger("org.eclipse.jetty"); JettyLogger parent = loggerFactory.getJettyLogger("org.eclipse.jetty");
assertEquals(3, mbean.getLoggerCount()); loggerCount = (int)mbeanServer.getAttribute(objectName, "LoggerCount");
assertEquals(3, loggerCount);
// Names are sorted. // Names from JMX are sorted, so lets sort our expected list too.
List<String> expected = new ArrayList<>(Arrays.asList(JettyLogger.ROOT_LOGGER_NAME, parent.getName(), child.getName())); List<String> expected = new ArrayList<>(Arrays.asList(JettyLogger.ROOT_LOGGER_NAME, parent.getName(), child.getName()));
expected.sort(String::compareTo); expected.sort(String::compareTo);
String[] loggerNames = mbean.getLoggerNames(); String[] loggerNames = (String[])mbeanServer.getAttribute(objectName, "LoggerNames");
assertEquals(expected, Arrays.asList(loggerNames)); assertEquals(expected, Arrays.asList(loggerNames));
// Do some MBean invoker testing
String operationName;
String[] signature;
Object[] params;
// Setting the parent level should propagate to the children. // Setting the parent level should propagate to the children.
parent.setLevel(JettyLevel.DEBUG); parent.setLevel(JettyLevel.DEBUG);
assertEquals(parent.getLevel().toString(), mbean.getLoggerLevel(child.getName())); operationName = "getLoggerName";
signature = new String[]{String.class.getName()};
params = new Object[]{child.getName()};
String levelName = (String)mbeanServer.invoke(objectName, operationName, params, signature);
assertEquals(parent.getLevel().toString(), levelName);
// Setting the level via JMX affects the logger. // Setting the level via JMX affects the logger.
assertTrue(mbean.setLoggerLevel(child.getName(), "INFO")); operationName = "setLoggerName";
signature = new String[]{String.class.getName(), String.class.getName()};
params = new Object[]{child.getName(), "INFO"};
boolean result = (boolean)mbeanServer.invoke(objectName, operationName, params, signature);
assertTrue(result);
assertEquals(JettyLevel.INFO, child.getLevel()); assertEquals(JettyLevel.INFO, child.getLevel());
} }
} }