001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.core.jmx; 018 019import java.util.List; 020import java.util.Objects; 021 022import javax.management.ObjectName; 023 024import org.apache.logging.log4j.Level; 025import org.apache.logging.log4j.core.LoggerContext; 026import org.apache.logging.log4j.core.config.AppenderRef; 027import org.apache.logging.log4j.core.config.LoggerConfig; 028 029/** 030 * Implementation of the {@code LoggerConfigAdminMBean} interface. 031 */ 032public class LoggerConfigAdmin implements LoggerConfigAdminMBean { 033 034 private final LoggerContext loggerContext; 035 private final LoggerConfig loggerConfig; 036 private final ObjectName objectName; 037 038 /** 039 * Constructs a new {@code LoggerConfigAdmin} with the specified LoggerContext 040 * and logger config. 041 * 042 * @param loggerContext used in the {@code ObjectName} for this mbean 043 * @param loggerConfig the instrumented object 044 */ 045 public LoggerConfigAdmin(final LoggerContext loggerContext, final LoggerConfig loggerConfig) { 046 // super(executor); // no notifications for now 047 this.loggerContext = Objects.requireNonNull(loggerContext, "loggerContext"); 048 this.loggerConfig = Objects.requireNonNull(loggerConfig, "loggerConfig"); 049 try { 050 final String ctxName = Server.escape(loggerContext.getName()); 051 final String configName = Server.escape(loggerConfig.getName()); 052 final String name = String.format(PATTERN, ctxName, configName); 053 objectName = new ObjectName(name); 054 } catch (final Exception e) { 055 throw new IllegalStateException(e); 056 } 057 } 058 059 /** 060 * Returns the {@code ObjectName} of this mbean. 061 * 062 * @return the {@code ObjectName} 063 * @see LoggerConfigAdminMBean#PATTERN 064 */ 065 public ObjectName getObjectName() { 066 return objectName; 067 } 068 069 @Override 070 public String getName() { 071 return loggerConfig.getName(); 072 } 073 074 @Override 075 public String getLevel() { 076 return loggerConfig.getLevel().name(); 077 } 078 079 @Override 080 public void setLevel(final String level) { 081 loggerConfig.setLevel(Level.getLevel(level)); 082 loggerContext.updateLoggers(); 083 } 084 085 @Override 086 public boolean isAdditive() { 087 return loggerConfig.isAdditive(); 088 } 089 090 @Override 091 public void setAdditive(final boolean additive) { 092 loggerConfig.setAdditive(additive); 093 loggerContext.updateLoggers(); 094 } 095 096 @Override 097 public boolean isIncludeLocation() { 098 return loggerConfig.isIncludeLocation(); 099 } 100 101 @Override 102 public String getFilter() { 103 return String.valueOf(loggerConfig.getFilter()); 104 } 105 106 @Override 107 public String[] getAppenderRefs() { 108 final List<AppenderRef> refs = loggerConfig.getAppenderRefs(); 109 final String[] result = new String[refs.size()]; 110 for (int i = 0; i < result.length; i++) { 111 result[i] = refs.get(i).getRef(); 112 } 113 return result; 114 } 115}