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;
020
021import javax.management.ObjectName;
022
023import org.apache.logging.log4j.status.StatusData;
024
025/**
026 * The MBean interface for monitoring and managing the {@code StatusLogger}.
027 */
028public interface StatusLoggerAdminMBean {
029    /**
030     * ObjectName pattern ({@value}) for StatusLoggerAdmin MBeans.
031     * This pattern contains a variable, which is the name of the logger context.
032     * <p>
033     * You can find all registered StatusLoggerAdmin MBeans like this:
034     * </p>
035     * <pre>
036     * MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
037     * String pattern = String.format(StatusLoggerAdminMBean.PATTERN, &quot;*&quot;);
038     * Set&lt;ObjectName&gt; statusLoggerNames = mbs.queryNames(new ObjectName(pattern), null);
039     * </pre>
040     * <p>
041     * Some characters are not allowed in ObjectNames. The logger context name
042     * may be quoted. When StatusLoggerAdmin MBeans are
043     * registered, their ObjectNames are created using this pattern as follows:
044     * </p>
045     * <pre>
046     * String ctxName = Server.escape(loggerContext.getName());
047     * String name = String.format(PATTERN, ctxName);
048     * ObjectName objectName = new ObjectName(name);
049     * </pre>
050     * @see Server#escape(String)
051     */
052    String PATTERN = Server.DOMAIN + ":type=%s,component=StatusLogger";
053
054    /**
055     * Notifications with this type have a {@code StatusData} userData object
056     * and a {@code null} message.
057     */
058    String NOTIF_TYPE_DATA = "com.apache.logging.log4j.core.jmx.statuslogger.data";
059
060    /**
061     * Notifications with this type have a formatted status data message string
062     * but no {@code StatusData} in their userData field.
063     */
064    String NOTIF_TYPE_MESSAGE = "com.apache.logging.log4j.core.jmx.statuslogger.message";
065
066    /**
067     * Returns the {@code ObjectName} that this status logger mbean is registered with.
068     * @return the ObjectName of this StatusLogger MBean
069     */
070    ObjectName getObjectName();
071
072    /**
073     * Returns a list with the most recent {@code StatusData} objects in the
074     * status history. The list has up to 200 entries by default but the length
075     * can be configured with system property {@code "log4j2.status.entries"}.
076     * <p>
077     * Note that the returned objects may contain {@code Throwable}s from
078     * external libraries.
079     * </p>
080     * <p>
081     * JMX clients calling this method must be prepared to deal with the errors
082     * that occur if they do not have the class definition for such
083     * {@code Throwable}s in their classpath.
084     * </p>
085     *
086     * @return the most recent messages logged by the {@code StatusLogger}.
087     */
088    List<StatusData> getStatusData();
089
090    /**
091     * Returns a string array with the most recent messages in the status
092     * history. The list has up to 200 entries by default but the length can be
093     * configured with system property {@code "log4j2.status.entries"}.
094     *
095     * @return the most recent messages logged by the {@code StatusLogger}.
096     */
097    String[] getStatusDataHistory();
098
099    /**
100     * Returns the {@code StatusLogger} level as a String.
101     *
102     * @return the {@code StatusLogger} level.
103     */
104    String getLevel();
105
106    /**
107     * Sets the {@code StatusLogger} level to the specified value.
108     *
109     * @param level the new {@code StatusLogger} level.
110     * @throws IllegalArgumentException if the specified level is not one of
111     *             "OFF", "FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE",
112     *             "ALL"
113     */
114    void setLevel(String level);
115
116    /**
117     * Returns the name of the LoggerContext that the {@code StatusLogger} is associated with.
118     * @return logger context name
119     */
120    String getContextName();
121}