[log4j] annotation

来源:互联网 发布:apache tomcat 编辑:程序博客网 时间:2024/05/17 01:13
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Annotation that identifies a Class as a Plugin.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Plugin {
    /**
     * Value of the elementType when none is specified.
     */
    String EMPTY = "";

    String name();
    String category();
    String elementType() default EMPTY;
    boolean printObject() default false;
    boolean deferChildren() default false;
}

@Plugin(name = "RFC5424Layout", category = "Core", elementType = "layout", printObject = true)
public class RFC5424Layout extends AbstractStringLayout {


2.
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Identifies a Plugin Attribute.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface PluginAttribute {

    String value();
}



3.

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Identifies a parameter as a Configuration.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface PluginConfiguration {
}

4.
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Identifies a parameter as an element.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface PluginElement {

    String value();
}


5.
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Identifies a Method as the factory to create the plugin.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface PluginFactory {

}



----------------
/**
     * Create the RFC 5424 Layout.
     *
     * @param facility         The Facility is used to try to classify the message.
     * @param id               The default structured data id to use when formatting according to RFC 5424.
     * @param ein              The IANA enterprise number.
     * @param includeMDC       Indicates whether data from the ThreadContextMap will be included in the RFC 5424 Syslog
     *                         record. Defaults to "true:.
     * @param mdcId            The id to use for the MDC Structured Data Element.
     * @param mdcPrefix        The prefix to add to MDC key names.
     * @param eventPrefix      The prefix to add to event key names.
     * @param includeNL        If true, a newline will be appended to the end of the syslog record. The default is false.
     * @param escapeNL         String that should be used to replace newlines within the message text.
     * @param appName          The value to use as the APP-NAME in the RFC 5424 syslog record.
     * @param msgId            The default value to be used in the MSGID field of RFC 5424 syslog records.
     * @param excludes         A comma separated list of MDC keys that should be excluded from the LogEvent.
     * @param includes         A comma separated list of MDC keys that should be included in the FlumeEvent.
     * @param required         A comma separated list of MDC keys that must be present in the MDC.
     * @param exceptionPattern The pattern for formatting exceptions.
     * @param useTLSMessageFormat If true the message will be formatted according to RFC 5425.
     * @param loggerFields     Container for the KeyValuePairs containing the patterns
     * @param config           The Configuration. Some Converters require access to the Interpolator.
     * @return An RFC5424Layout.
     */
    @PluginFactory
    public static RFC5424Layout createLayout(
            @PluginAttribute("facility") final String facility,
            @PluginAttribute("id") final String id,
            @PluginAttribute("enterpriseNumber") final String ein,
            @PluginAttribute("includeMDC") final String includeMDC,
            @PluginAttribute("mdcId") String mdcId,
            @PluginAttribute("mdcPrefix") final String mdcPrefix,
            @PluginAttribute("eventPrefix") final String eventPrefix,
            @PluginAttribute("newLine") final String includeNL,
            @PluginAttribute("newLineEscape") final String escapeNL,
            @PluginAttribute("appName") final String appName,
            @PluginAttribute("messageId") final String msgId,
            @PluginAttribute("mdcExcludes") final String excludes,
            @PluginAttribute("mdcIncludes") String includes,
            @PluginAttribute("mdcRequired") final String required,
            @PluginAttribute("exceptionPattern") final String exceptionPattern,
            @PluginAttribute("useTLSMessageFormat") final String useTLSMessageFormat, // RFC 5425
            @PluginElement("LoggerFields") final LoggerFields[] loggerFields,
            @PluginConfiguration final Configuration config) {
        final Charset charset = Charsets.UTF_8;
        if (includes != null && excludes != null) {
            LOGGER.error("mdcIncludes and mdcExcludes are mutually exclusive. Includes wil be ignored");
            includes = null;
        }
        final Facility f = Facility.toFacility(facility, Facility.LOCAL0);
        final int enterpriseNumber = Integers.parseInt(ein, DEFAULT_ENTERPRISE_NUMBER);
        final boolean isMdc = Booleans.parseBoolean(includeMDC, true);
        final boolean includeNewLine = Boolean.parseBoolean(includeNL);
        final boolean useTlsMessageFormat = Booleans.parseBoolean(useTLSMessageFormat, false);
        if (mdcId == null) {
            mdcId = DEFAULT_MDCID;
        }

        return new RFC5424Layout(config, f, id, enterpriseNumber, isMdc, includeNewLine, escapeNL, mdcId, mdcPrefix,
                eventPrefix, appName, msgId, excludes, includes, required, charset, exceptionPattern,
                useTlsMessageFormat, loggerFields);
    }