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
069     */
070    public 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     *
080     * JMX clients calling this method must be prepared to deal with the errors
081     * that occur if they do not have the class definition for such
082     * {@code Throwable}s in their classpath.
083     *
084     * @return the most recent messages logged by the {@code StatusLogger}.
085     */
086    List<StatusData> getStatusData();
087
088    /**
089     * Returns a string array with the most recent messages in the status
090     * history. The list has up to 200 entries by default but the length can be
091     * configured with system property {@code "log4j2.status.entries"}.
092     *
093     * @return the most recent messages logged by the {@code StatusLogger}.
094     */
095    String[] getStatusDataHistory();
096
097    /**
098     * Returns the {@code StatusLogger} level as a String.
099     *
100     * @return the {@code StatusLogger} level.
101     */
102    String getLevel();
103
104    /**
105     * Sets the {@code StatusLogger} level to the specified value.
106     *
107     * @param level the new {@code StatusLogger} level.
108     * @throws IllegalArgumentException if the specified level is not one of
109     *             "OFF", "FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE",
110     *             "ALL"
111     */
112    void setLevel(String level);
113
114    /**
115     * Returns the name of the LoggerContext that the {@code StatusLogger} is associated with.
116     * @return logger context name
117     */
118    String getContextName();
119}