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