/** * Ctor. The configuration is built using instance of * {@link Spec}: * * <ul> * <li>The name is set to {@link Spec#getName()}. * <li>Appenders from the parent object are cleared. * <li>{@link Appender}s are created * {@link #buildAppenders(Spec)}. * <li>{@link LoggerConfig}s are created using * {@link #buildLoggerConfigs(Spec)}. * <li>Every {@link LoggerConfig} is configured with its * {@link Appender} using * {@link #configureLoggerAppenders(Spec, Map, Map)}. * <li>Root loggers are configured. * </ul> * * @param spec */ EmbeddedConfiguration(Spec spec) { super(ConfigurationSource.NULL_SOURCE); setName(spec.getName()); // Clean up first getAppenders().clear(); getRootLogger().getAppenders().clear(); // Build Appenders final Map<String, Appender> appenders = buildAppenders(spec); final Map<String, SyslogAppender> syslogAppenders = buildSyslogAppenders(spec); // Build Logger Configs final Map<String, LoggerConfig> loggers = buildLoggerConfigs(spec); // Configure loggers with appenders configureLoggerAppenders(spec, appenders, loggers); // Configure root logger appenders configureRootLogger(spec, appenders, syslogAppenders); }
/** * @param spec * @return */ protected Map<String, SyslogAppender> buildSyslogAppenders(Spec spec) { Map<String, SyslogAppender> syslogAppenders = new HashMap<>(); Collection<String> syslogAppenderNames = spec.getSyslogAppenderNames(); for (String appenderName : syslogAppenderNames) { SyslogSpec sspec = spec.getSyslogSpec(appenderName); SyslogAppender syslogAppender = createSyslogAppender(sspec.name, sspec.host, sspec.port, sspec.protocol, sspec.appName, this, sspec.facilityName, sspec.enterpriseNumber, sspec.clientHostName); syslogAppenders.put(appenderName, syslogAppender); addAppender(syslogAppender); } return syslogAppenders; }
/** * Creates an instance of {@link SyslogAppender}. Parameters * match that of appearing in the same method in * {@link SyslogAppender}. * * @param name * @param host * @param port * @param protocolStr * @param appName * @param config * @param facilityName * @param enterpriseNumber * @param clientHostName * @return an instance of {@link SyslogAppender} */ protected static SyslogAppender createSyslogAppender(String name, String host, int port, String protocolStr, String appName, Configuration config, String facilityName, int enterpriseNumber, String clientHostName) { final SslConfiguration sslConfig = null; int connectTimeoutMillis = 0; int reconnectionDelayMillis = 0; boolean immediateFail = true; boolean immediateFlush = true; boolean ignoreExceptions = true; Facility facility = Facility.toFacility(facilityName, Facility.USER); String id = null; boolean includeMdc = false; String mdcId = "mdc-ignored-id"; String mdcPrefix = null; String eventPrefix = "|/" + clientHostName + "/" + appName + ".log|"; boolean newLine = true; String escapeNL = null; String msgId = appName; String mdcExcludes = null; String mdcIncludes = null; String mdcRequired = null; String format = "RFC5424"; Filter filter = null; Charset charsetName = Charset.defaultCharset(); String exceptionPattern = null; LoggerFields[] loggerFields = new LoggerFields[0]; boolean advertise = false; SyslogAppender sla = SyslogAppender.createAppender(host, port, protocolStr, sslConfig, connectTimeoutMillis, reconnectionDelayMillis, immediateFail, name, immediateFlush, ignoreExceptions, facility, id, enterpriseNumber, includeMdc, mdcId, mdcPrefix, eventPrefix, newLine, escapeNL, appName, msgId, mdcExcludes, mdcIncludes, mdcRequired, format, filter, config, charsetName, exceptionPattern, loggerFields, advertise); return sla; }
@Override public void initTargetAppender() { targetAppender = SyslogAppender.newSyslogAppenderBuilder() .withName(getTargetAppenderName()) .withHost("localhost") .withPort(514) .withProtocol(Protocol.UDP) .withLayout( PatternLayout.newBuilder().withPattern("%d{ISO8601} %-5level %logger - %msg%n").build()) .setFacility(Facility.LOCAL1) .build(); }