hudi
hudi

Reputation: 16555

How to extend syslogAppender in log4j2

In log4j I use syslog to log just message

log4j.appender.SYSLOG.layout.ConversionPattern=%m%n

now I updated to log4j2. Problem is that syslog now just contains 2 layout and I can't use some custom to log just message so I need to overrided it. How should I start ?

Upvotes: 1

Views: 743

Answers (2)

hudi
hudi

Reputation: 16555

ok I extended syslogappender and created static method where I add layout as argument

@PluginFactory
public static PatternSyslogAppender createAppender(
        // @formatter:off
        @PluginAttribute("host") final String host,
        @PluginAttribute(value = "port", defaultInt = 0) final int port,
        @PluginAttribute("protocol") final String protocolStr,
        @PluginElement("SSL") final SslConfiguration sslConfig,
        @PluginAttribute(value = "connectTimeoutMillis", defaultInt = 0) final int connectTimeoutMillis,
        @PluginAliases("reconnectionDelay")// deprecated
        @PluginAttribute(value = "reconnectionDelayMillis", defaultInt = 0) final int reconnectionDelayMillis,
        @PluginAttribute(value = "immediateFail", defaultBoolean = true) final boolean immediateFail,
        @PluginAttribute("name") final String name,
        @PluginAttribute(value = "immediateFlush", defaultBoolean = true) final boolean immediateFlush,
        @PluginAttribute(value = "ignoreExceptions", defaultBoolean = true) final boolean ignoreExceptions,
        @PluginAttribute(value = "facility", defaultString = "LOCAL0") final Facility facility,
        @PluginAttribute("id") final String id,
        @PluginAttribute(value = "enterpriseNumber", defaultInt = Rfc5424Layout.DEFAULT_ENTERPRISE_NUMBER) final int enterpriseNumber,
        @PluginAttribute(value = "includeMdc", defaultBoolean = true) final boolean includeMdc,
        @PluginAttribute("mdcId") final String mdcId, @PluginAttribute("mdcPrefix") final String mdcPrefix,
        @PluginAttribute("eventPrefix") final String eventPrefix,
        @PluginAttribute(value = "newLine", defaultBoolean = false) final boolean newLine,
        @PluginAttribute("newLineEscape") final String escapeNL, @PluginAttribute("appName") final String appName,
        @PluginAttribute("messageId") final String msgId, @PluginAttribute("mdcExcludes") final String excludes,
        @PluginAttribute("mdcIncludes") final String includes,
        @PluginAttribute("mdcRequired") final String required, @PluginAttribute("format") final String format,
        @PluginElement("Filter") final Filter filter, @PluginConfiguration final Configuration config,
        @PluginAttribute(value = "charset", defaultString = "UTF-8") final Charset charsetName,
        @PluginAttribute("exceptionPattern") final String exceptionPattern,
        @PluginElement("LoggerFields") final LoggerFields[] loggerFields,
        @PluginAttribute(value = "advertise", defaultBoolean = false) final boolean advertise,
        @PluginElement("Layout") Layout<? extends Serializable> layout)

{
    // @formatter:on

    // TODO: add Protocol to TypeConverters
    final Protocol protocol = EnglishEnums.valueOf(Protocol.class, protocolStr);
    final boolean useTlsMessageFormat = sslConfig != null || protocol == Protocol.SSL;

    if (layout == null) {
        layout = PatternLayout.createDefaultLayout();
    }

    if (RFC5424.equalsIgnoreCase(format)) {
        Rfc5424Layout.createLayout(facility, id, enterpriseNumber, includeMdc, mdcId, mdcPrefix, eventPrefix,
                newLine, escapeNL, appName, msgId, excludes, includes, required, exceptionPattern,
                useTlsMessageFormat, loggerFields, config);
    } else if (layout == null) {
        SyslogLayout.createLayout(facility, newLine, escapeNL, charsetName);
    }

    if (name == null) {
        LOGGER.error("No name provided for SyslogAppender");
        return null;
    }
    final AbstractSocketManager manager = createSocketManager(name, protocol, host, port, connectTimeoutMillis,
            sslConfig, reconnectionDelayMillis, immediateFail, layout);

    return new PatternSyslogAppender(name, layout, filter, ignoreExceptions, immediateFlush, manager,
            advertise ? config.getAdvertiser() : null);
}

Upvotes: 1

rgoers
rgoers

Reputation: 9151

If you want a custom layout use the SocketAppender.

Upvotes: 0

Related Questions