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
010 * docs/licenses/cddl.txt
011 * or http://www.opensource.org/licenses/cddl1.php.
012 * See the License for the specific language governing permissions
013 * and limitations under the License.
014 *
015 * When distributing Covered Code, include this CDDL HEADER in each
016 * file and include the License file at
017 * docs/licenses/cddl.txt.  If applicable,
018 * add the following below this CDDL HEADER, with the fields enclosed
019 * by brackets "[]" replaced with your own identifying information:
020 *      Portions Copyright [yyyy] [name of copyright owner]
021 *
022 * CDDL HEADER END
023 *
024 *
025 *      Portions Copyright 2011-2023 Ping Identity Corporation
026 */
027package com.unboundid.directory.sdk.common.api;
028
029
030
031import java.util.Collections;
032import java.util.List;
033import java.util.Map;
034
035import com.unboundid.directory.sdk.broker.internal.BrokerExtension;
036import com.unboundid.directory.sdk.common.config.FileBasedErrorLoggerConfig;
037import com.unboundid.directory.sdk.common.internal.ExampleUsageProvider;
038import com.unboundid.directory.sdk.common.internal.Reconfigurable;
039import com.unboundid.directory.sdk.common.internal.UnboundIDExtension;
040import com.unboundid.directory.sdk.common.types.LogCategory;
041import com.unboundid.directory.sdk.common.types.LogSeverity;
042import com.unboundid.directory.sdk.common.types.ServerContext;
043import com.unboundid.directory.sdk.ds.internal.DirectoryServerExtension;
044import com.unboundid.directory.sdk.metrics.internal.MetricsEngineExtension;
045import com.unboundid.directory.sdk.proxy.internal.DirectoryProxyServerExtension;
046import com.unboundid.directory.sdk.sync.internal.SynchronizationServerExtension;
047import com.unboundid.ldap.sdk.LDAPException;
048import com.unboundid.ldap.sdk.ResultCode;
049import com.unboundid.util.Extensible;
050import com.unboundid.util.ThreadSafety;
051import com.unboundid.util.ThreadSafetyLevel;
052import com.unboundid.util.args.ArgumentException;
053import com.unboundid.util.args.ArgumentParser;
054
055
056
057/**
058 * This class defines an API that may be used to create a specific type of
059 * error logger which is intended to write log messages to text files.  This is
060 * a convenience for developers which wish to create custom error loggers that
061 * write to text files and provides support for a wide range of functionality
062 * including high-performance and highly-concurrent logging.  All of the options
063 * available to {@link ErrorLogger} implementations are available for
064 * file-based error loggers, as well as options for indicating the log file
065 * path, the rotation and retention policies, whether to buffer the output, etc.
066 * <BR><BR>
067 * Note that file-based error loggers will automatically be registered within
068 * the server as disk space consumers, so there is no need to implement the
069 * {@link DiskSpaceConsumer} interface.  Also note that configuration change
070 * related to the log file (e.g., the log file path, buffer size, queue size,
071 * etc.) will also automatically be handled by the server, so subclasses only
072 * need to be concerned about changes to the custom arguments they define.
073 * <BR>
074 * <H2>Configuring File-Based Error Loggers</H2>
075 * In order to configure a file-based error logger created using this API, use a
076 * command like:
077 * <PRE>
078 *      dsconfig create-log-publisher \
079 *           --publisher-name "<I>{logger-name}</I>" \
080 *           --type third-party-file-based-error \
081 *           --set enabled:true \
082 *           --set "log-file:<I>{path}</I>" \
083 *           --set "rotation-policy:<I>{rotation-policy-name}</I>" \
084 *           --set "retention-policy:<I>{retention-policy-name}</I>" \
085 *           --set "extension-class:<I>{class-name}</I>" \
086 *           --set "extension-argument:<I>{name=value}</I>"
087 * </PRE>
088 * where "<I>{logger-name}</I>" is the name to use for the error logger
089 * instance, "<I>{path}</I>" is the path to the log file to be written,
090 * "<I>{rotation-policy-name}</I>" is the name of the log rotation policy to use
091 * for the log file, "<I>{retention-policy-name}</I>" is the name of the log
092 * retention policy to use for the log file, "<I>{class-name}</I>" is the
093 * fully-qualified name of the Java class that extends
094 * {@code com.unboundid.directory.sdk.common.api.FileBasedErrorLogger}, and
095 * "<I>{name=value}</I>" represents name-value pairs for any arguments to
096 * provide to the logger.  If multiple arguments should be provided to the
097 * logger, then the "<CODE>--set extension-argument:<I>{name=value}</I></CODE>"
098 * option should be provided multiple times.  It is also possible to specify
099 * multiple log rotation and/or retention policies if desired.
100 *
101 * @see  ErrorLogger
102 * @see  com.unboundid.directory.sdk.common.scripting.ScriptedErrorLogger
103 * @see
104 *    com.unboundid.directory.sdk.common.scripting.ScriptedFileBasedErrorLogger
105 */
106@Extensible()
107@DirectoryServerExtension()
108@DirectoryProxyServerExtension(appliesToLocalContent=true,
109     appliesToRemoteContent=true)
110@SynchronizationServerExtension(appliesToLocalContent=true,
111     appliesToSynchronizedContent=true)
112@MetricsEngineExtension()
113@BrokerExtension()
114@ThreadSafety(level=ThreadSafetyLevel.INTERFACE_THREADSAFE)
115public abstract class FileBasedErrorLogger
116       implements UnboundIDExtension,
117                  Reconfigurable<FileBasedErrorLoggerConfig>,
118                  ExampleUsageProvider
119{
120  /**
121   * Creates a new instance of this file-based error logger.  All file-based
122   * error logger implementations must include a default constructor, but any
123   * initialization should generally be done in the
124   * {@code initializeErrorLogger} method.
125   */
126  public FileBasedErrorLogger()
127  {
128    // No implementation is required.
129  }
130
131
132
133  /**
134   * {@inheritDoc}
135   */
136  public abstract String getExtensionName();
137
138
139
140  /**
141   * {@inheritDoc}
142   */
143  public abstract String[] getExtensionDescription();
144
145
146
147  /**
148   * {@inheritDoc}
149   */
150  public void defineConfigArguments(final ArgumentParser parser)
151         throws ArgumentException
152  {
153    // No arguments will be allowed by default.
154  }
155
156
157
158  /**
159   * Initializes this file-based error logger.
160   *
161   * @param  serverContext  A handle to the server context for the server in
162   *                        which this extension is running.
163   * @param  config         The general configuration for this file-based error
164   *                        logger.
165   * @param  parser         The argument parser which has been initialized from
166   *                        the configuration for this file-based error logger.
167   *
168   * @throws  LDAPException  If a problem occurs while initializing this
169   *                         file-based error logger.
170   */
171  public void initializeErrorLogger(final ServerContext serverContext,
172                                    final FileBasedErrorLoggerConfig config,
173                                    final ArgumentParser parser)
174         throws LDAPException
175  {
176    // No initialization will be performed by default.
177  }
178
179
180
181  /**
182   * {@inheritDoc}
183   */
184  public boolean isConfigurationAcceptable(
185                      final FileBasedErrorLoggerConfig config,
186                      final ArgumentParser parser,
187                      final List<String> unacceptableReasons)
188  {
189    // No extended validation will be performed by default.
190    return true;
191  }
192
193
194
195  /**
196   * {@inheritDoc}
197   */
198  public ResultCode applyConfiguration(final FileBasedErrorLoggerConfig config,
199                                       final ArgumentParser parser,
200                                       final List<String> adminActionsRequired,
201                                       final List<String> messages)
202  {
203    // If there are any custom arguments, then add an admin action message
204    // indicating that the extension needs to be restarted for any changes to
205    // take effect.
206    if (! parser.getNamedArguments().isEmpty())
207    {
208      adminActionsRequired.add(
209           "If any extension-argument values have been altered, then " +
210                "those new values have not actually been applied.  The new " +
211                "configuration for those arguments will not take effect " +
212                "until this file-based error logger is disabled and " +
213                "re-enabled, or until the server is restarted.");
214    }
215
216    return ResultCode.SUCCESS;
217  }
218
219
220
221  /**
222   * Performs any cleanup which may be necessary when this file-based error
223   * logger is to be taken out of service.
224   */
225  public void finalizeErrorLogger()
226  {
227    // No implementation is required.
228  }
229
230
231
232  /**
233   * Records information about the provided message, if appropriate.
234   *
235   * @param  category   The category for the message to be logged.
236   * @param  severity   The severity for the message to be logged.
237   * @param  messageID  The unique identifier with which the message text is
238   *                    associated.
239   * @param  message    The message to be logged.
240   *
241   * @return  The content of the log message that should be written.  It may be
242   *          {@code null} or empty if no message should be written.  It may
243   *          optionally include line breaks if the log message should span
244   *          multiple lines.
245   */
246  public abstract CharSequence logError(final LogCategory category,
247                                        final LogSeverity severity,
248                                        final long messageID,
249                                        final String message);
250
251
252
253  /**
254   * {@inheritDoc}
255   */
256  public Map<List<String>,String> getExamplesArgumentSets()
257  {
258    return Collections.emptyMap();
259  }
260}