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