public void watch(Class<?> loggerClass, Level level) { this.loggerClass = loggerClass; Appender appender = new AbstractAppender(APPENDER_NAME, null, PatternLayout.createDefaultLayout()) { @Override public void append(LogEvent event) { logEvents.add(event); } }; appender.start(); final LoggerContext ctx = getLoggerContext(); LoggerConfig loggerConfig = ctx.getConfiguration().getLoggerConfig(loggerClass.getName()); oldLevel = loggerConfig.getLevel(); loggerConfig.setLevel(level); loggerConfig.addAppender(appender, level, null); ctx.updateLoggers(); }
public static void bindLogger() { LoggerContext context = (LoggerContext) LogManager.getContext(false); Configuration config = context.getConfiguration(); Map<String, ESLogger> loggers = Maps.newConcurrentHashMap(); Appender appender = new AbstractAppender("", null, null) { @Override public void append(LogEvent event) { String name = event.getLoggerName(); ESLogger logger = loggers.computeIfAbsent(name, key -> new ESLogger(key, null, (LoggerImpl) LoggerFactory.getLogger(key))); logger.log(event.getLevel(), event.getMarker(), event.getMessage(), event.getThrown()); } }; appender.start(); config.addAppender(appender); LoggerConfig loggerConfig = new LoggerConfig("", Level.ALL, false); loggerConfig.addAppender(appender, null, null); config.addLogger("", loggerConfig); context.updateLoggers(); }
@Override public void start() { final Map<String, Appender> map = config.getAppenders(); for (final AppenderRef ref : appenderRefs) { final String name = ref.getRef(); final Appender appender = map.get(name); if (appender != null) { final Filter filter = appender instanceof AbstractAppender ? ((AbstractAppender) appender).getFilter() : null; appenders.put(name, new AppenderControl(appender, ref.getLevel(), filter)); } else { LOGGER.error("Appender " + ref + " cannot be located. Reference ignored"); } } super.start(); }
/** * Gets an instance of the class. * * @param config The current Configuration. * @param options pattern options, an array of two elements: pattern, max length (defaults to 100 on invalid value). * @return instance of class. */ public static MaxLengthConverter newInstance(final Configuration config, final String[] options) { if (options.length != 2) { LOGGER.error("Incorrect number of options on maxLength: expected 2 received {}: {}", options.length, options); return null; } if (options[0] == null) { LOGGER.error("No pattern supplied on maxLength"); return null; } if (options[1] == null) { LOGGER.error("No length supplied on maxLength"); return null; } final PatternParser parser = PatternLayout.createPatternParser(config); final List<PatternFormatter> formatters = parser.parse(options[0]); return new MaxLengthConverter(formatters, AbstractAppender.parseInt(options[1], 100)); }
/** * Factory method for creating a JDBC appender within the plugin manager. * * @see Builder * @deprecated use {@link #newBuilder()} */ @Deprecated public static <B extends Builder<B>> JdbcAppender createAppender(final String name, final String ignore, final Filter filter, final ConnectionSource connectionSource, final String bufferSize, final String tableName, final ColumnConfig[] columnConfigs) { Assert.requireNonEmpty(name, "Name cannot be empty"); Objects.requireNonNull(connectionSource, "ConnectionSource cannot be null"); Assert.requireNonEmpty(tableName, "Table name cannot be empty"); Assert.requireNonEmpty(columnConfigs, "ColumnConfigs cannot be empty"); final int bufferSizeInt = AbstractAppender.parseInt(bufferSize, 0); final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true); return JdbcAppender.<B>newBuilder() .setBufferSize(bufferSizeInt) .setColumnConfigs(columnConfigs) .setConnectionSource(connectionSource) .setTableName(tableName) .withName(name) .withIgnoreExceptions(ignoreExceptions) .withFilter(filter) .build(); }
public LogVerifier() { this.appender = new AbstractAppender(LogVerifier.class.getSimpleName(), LevelRangeFilter.createFilter(Level.ALL, Level.ALL, Filter.Result.ACCEPT, Filter.Result.ACCEPT), PatternLayout.createDefaultLayout()) { @Override public void append(LogEvent event) { LogVerifier.this.append(event); } }; }
/** * Factory method for creating a NoSQL appender within the plugin manager. * * @param name The name of the appender. * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise * they are propagated to the caller. * @param filter The filter, if any, to use. * @param bufferSize If an integer greater than 0, this causes the appender to buffer log events and flush whenever * the buffer reaches this size. * @param provider The NoSQL provider that provides connections to the chosen NoSQL database. * @return a new NoSQL appender. */ @PluginFactory public static NoSQLAppender createAppender( @PluginAttribute("name") final String name, @PluginAttribute("ignoreExceptions") final String ignore, @PluginElement("Filter") final Filter filter, @PluginAttribute("bufferSize") final String bufferSize, @PluginElement("NoSqlProvider") final NoSQLProvider<?> provider) { if (provider == null) { LOGGER.error("NoSQL provider not specified for appender [{}].", name); return null; } final int bufferSizeInt = AbstractAppender.parseInt(bufferSize, 0); final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true); final String managerName = "noSqlManager{ description=" + name + ", bufferSize=" + bufferSizeInt + ", provider=" + provider + " }"; final NoSQLDatabaseManager<?> manager = NoSQLDatabaseManager.getNoSQLDatabaseManager( managerName, bufferSizeInt, provider ); if (manager == null) { return null; } return new NoSQLAppender(name, filter, ignoreExceptions, manager); }
/** * Factory method for creating a JDBC appender within the plugin manager. * * @param name The name of the appender. * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise * they are propagated to the caller. * @param filter The filter, if any, to use. * @param connectionSource The connections source from which database connections should be retrieved. * @param bufferSize If an integer greater than 0, this causes the appender to buffer log events and flush whenever * the buffer reaches this size. * @param tableName The name of the database table to insert log events into. * @param columnConfigs Information about the columns that log event data should be inserted into and how to insert * that data. * @return a new JDBC appender. */ @PluginFactory public static JDBCAppender createAppender( @PluginAttribute("name") final String name, @PluginAttribute("ignoreExceptions") final String ignore, @PluginElement("Filter") final Filter filter, @PluginElement("ConnectionSource") final ConnectionSource connectionSource, @PluginAttribute("bufferSize") final String bufferSize, @PluginAttribute("tableName") final String tableName, @PluginElement("ColumnConfigs") final ColumnConfig[] columnConfigs) { final int bufferSizeInt = AbstractAppender.parseInt(bufferSize, 0); final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true); final StringBuilder managerName = new StringBuilder("jdbcManager{ description=").append(name) .append(", bufferSize=").append(bufferSizeInt).append(", connectionSource=") .append(connectionSource.toString()).append(", tableName=").append(tableName).append(", columns=[ "); int i = 0; for (final ColumnConfig column : columnConfigs) { if (i++ > 0) { managerName.append(", "); } managerName.append(column.toString()); } managerName.append(" ] }"); final JDBCDatabaseManager manager = JDBCDatabaseManager.getJDBCDatabaseManager( managerName.toString(), bufferSizeInt, connectionSource, tableName, columnConfigs ); if (manager == null) { return null; } return new JDBCAppender(name, filter, ignoreExceptions, manager); }
/** * Factory method for creating a NoSQL appender within the plugin manager. * * @param name * The name of the appender. * @param ignore * If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise they * are propagated to the caller. * @param filter * The filter, if any, to use. * @param bufferSize * If an integer greater than 0, this causes the appender to buffer log events and flush whenever the * buffer reaches this size. * @param provider * The NoSQL provider that provides connections to the chosen NoSQL database. * @return a new NoSQL appender. * @deprecated since 2.10.1; use {@link Builder}. */ @SuppressWarnings("resource") @Deprecated public static NoSqlAppender createAppender( // @formatter:off final String name, final String ignore, final Filter filter, final String bufferSize, final NoSqlProvider<?> provider) { // @formatter:on if (provider == null) { LOGGER.error("NoSQL provider not specified for appender [{}].", name); return null; } final int bufferSizeInt = AbstractAppender.parseInt(bufferSize, 0); final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true); final String managerName = "noSqlManager{ description=" + name + ", bufferSize=" + bufferSizeInt + ", provider=" + provider + " }"; final NoSqlDatabaseManager<?> manager = NoSqlDatabaseManager.getNoSqlDatabaseManager(managerName, bufferSizeInt, provider); if (manager == null) { return null; } return new NoSqlAppender(name, filter, null, ignoreExceptions, manager); }
@Override public void start() { for (final AppenderRef ref : appenderRefs) { final String name = ref.getRef(); final Appender appender = config.getAppender(name); if (appender != null) { final Filter filter = appender instanceof AbstractAppender ? ((AbstractAppender) appender).getFilter() : null; appenders.put(name, new AppenderControl(appender, ref.getLevel(), filter)); } else { LOGGER.error("Appender " + ref + " cannot be located. Reference ignored"); } } super.start(); }