001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2008 Sun Microsystems, Inc.
015 * Portions Copyright 2013-2016 ForgeRock AS.
016 */
017package org.opends.server.loggers;
018
019import java.util.HashMap;
020
021import org.forgerock.i18n.slf4j.LocalizedLogger;
022
023import java.util.HashSet;
024import java.util.List;
025import java.util.Map;
026import java.util.Set;
027
028import org.forgerock.i18n.LocalizableMessage;
029import org.opends.messages.Severity;
030import org.forgerock.opendj.server.config.server.ErrorLogPublisherCfg;
031
032/**
033 * This class defines the set of methods and structures that must be implemented
034 * for a Directory Server error log publisher.
035 *
036 * @param <T>
037 *          The type of error log publisher configuration handled by this log
038 *          publisher implementation.
039 */
040@org.opends.server.types.PublicAPI(
041    stability = org.opends.server.types.StabilityLevel.VOLATILE,
042    mayInstantiate = false, mayExtend = true, mayInvoke = false)
043public abstract class ErrorLogPublisher<T extends ErrorLogPublisherCfg>
044    implements LogPublisher<T>
045{
046
047  private static final LocalizedLogger logger = LocalizedLogger
048      .getLoggerForThisClass();
049
050  /** The hash map that will be used to define specific log severities for the various categories. */
051  protected Map<String, Set<Severity>> definedSeverities = new HashMap<>();
052
053  /**
054   * The set of default log severities that will be used if no custom severities
055   * have been defined for the associated category.
056   */
057  protected Set<Severity> defaultSeverities = new HashSet<>();
058
059  @Override
060  public boolean isConfigurationAcceptable(T configuration,
061      List<LocalizableMessage> unacceptableReasons)
062  {
063    // This default implementation does not perform any special
064    // validation. It should be overridden by error log publisher
065    // implementations that wish to perform more detailed validation.
066    return true;
067  }
068
069  /**
070   * Writes a message to the error log using the provided information.
071   * <p>
072   * The category and severity information are used to determine whether to
073   * actually log this message.
074   * <p>
075   * Category is defined using either short name (used for classes in well
076   * defined packages) or fully qualified classname. Conversion to short name is
077   * done automatically when loggers are created, see
078   * {@code LoggingCategoryNames} for list of existing short names.
079   *
080   * @param category
081   *          The category of the message, which is either a classname or a
082   *          simple category name defined in {@code LoggingCategoryNames}
083   *          class.
084   * @param severity
085   *          The severity of the message.
086   * @param message
087   *          The message to be logged.
088   * @param exception
089   *          The exception to be logged. May be {@code null}.
090   */
091  public abstract void log(String category, Severity severity,
092      LocalizableMessage message, Throwable exception);
093
094  /**
095   * Check if a message should be logged for the provided category and severity.
096   *
097   * @param category
098   *          The category of the message, which is either a classname or a
099   *          simple category name defined in {@code LoggingCategoryNames}
100   *          class.
101   * @param severity
102   *          The severity of the message.
103   * @return {@code true} if the message should be logged, {@code false}
104   *         otherwise
105   */
106  public abstract boolean isEnabledFor(String category, Severity severity);
107
108}