public Slf4jLog4j2AdapterActivateOption(Object logger) { super(null); try { org.apache.logging.log4j.core.Logger log4j2Logger = (org.apache.logging.log4j.core.Logger)loggerField.get( logger); super.logger = log4j2Logger; super.configuration = super.logger.getContext().getConfiguration(); } catch (Exception e) { throw new RuntimeException("logger must be instanceof org.apache.logging.slf4j.Log4jLogger", e); } }
public Log4j2ActivateOption(org.apache.logging.log4j.Logger logger) { if (logger != null) { if (logger instanceof org.apache.logging.log4j.core.Logger) { this.logger = (org.apache.logging.log4j.core.Logger)logger; configuration = this.logger.getContext().getConfiguration(); } else { throw new RuntimeException( "logger must instanceof org.apache.logging.log4j.core.Logger, " + logger.getClass().getName()); } } }
/** * Gets the associated Configuration for the configuration file this was constructed with. * * @return this LoggerContext's Configuration. */ public Configuration getConfiguration() { return loggerContext.getConfiguration(); }
public Map<String, Level> getLoggerLevels() { Configuration loggerConfig = ((LoggerContext) LogManager.getContext(false)).getConfiguration(); Map<String, Level> logLevelMap = new HashMap<>(); for (Map.Entry<String, LoggerConfig> entry : loggerConfig.getLoggers().entrySet()) { logLevelMap.put(entry.getKey(), entry.getValue().getLevel()); } return logLevelMap; }
@VisibleForTesting protected Collection<LoggerConfig> getLoggerConfigs() { final LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false); final Configuration configuration = loggerContext.getConfiguration(); return configuration.getLoggers().values(); }
/** * Given an appender name, as configured, get the parent directory of the appender's log file. * Note that if anything goes wrong, this will throw an Error and exit. */ private String logRootDir(String appenderName) { Appender appender = ((LoggerContext) LogManager.getContext()).getConfiguration().getAppender(appenderName); if (appenderName != null && appender != null && RollingFileAppender.class.isInstance(appender)) { return new File(((RollingFileAppender) appender).getFileName()).getParent(); } else { throw new RuntimeException("Log viewer could not find configured appender, or the appender is not a FileAppender. " + "Please check that the appender name configured in storm and log4j agree."); } }
private Appender getAppender(final String appenderName) { final LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false); final Configuration configuration = loggerContext.getConfiguration(); return configuration.getAppender(appenderName); }
@VisibleForTesting protected Level getLoggerLevel(final String loggerName) { final LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false); final Configuration configuration = loggerContext.getConfiguration(); final LoggerConfig loggerConfig = configuration.getLoggerConfig(loggerName); return loggerConfig.getLevel(); }
private void initializeLogging(final Level logLevel) { final LoggerContext context = (LoggerContext) LogManager.getContext(false); final org.apache.logging.log4j.core.config.Configuration config = context.getConfiguration(); config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME).setLevel(logLevel); config.getLoggerConfig(Main.class.getPackage().getName()).setLevel(logLevel); context.updateLoggers(config); }
@Test public void testConfiguredAppenders() throws Exception { final Configuration configuration = this.ctx.getConfiguration(); final Map<String, Appender> appenders = configuration.getAppenders(); assertThat(appenders, is(notNullValue())); assertThat(appenders.size(), is(equalTo(3))); }
@VisibleForTesting protected void setLoggerLevel(final String loggerName, final Level level) { final LoggerContext context = (LoggerContext) LogManager.getContext(false); final Configuration config = context.getConfiguration(); final LoggerConfig loggerConfig = config.getLoggerConfig(loggerName); if(loggerName.equals(loggerConfig.getName())) { loggerConfig.setLevel(level); } else { final LoggerConfig newLoggerConfig = new LoggerConfig(loggerName, level, loggerConfig.isAdditive()); newLoggerConfig.setLevel(level); config.addLogger(loggerName, newLoggerConfig); } context.updateLoggers(); }
private void testInitializeFromFilePath(final String path) { try (final LoggerContext loggerContext = Configurator.initialize(getClass().getName(), null, path)) { Assert.assertNotNull(loggerContext.getConfiguration().getAppender("List")); } } }
private void addInstrumentedAppender(final MetricRegistry metrics, final Level level) { final InstrumentedAppender appender = new InstrumentedAppender(metrics, null, null, false); appender.start(); final LoggerContext context = (LoggerContext) LogManager.getContext(false); final org.apache.logging.log4j.core.config.Configuration config = context.getConfiguration(); config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME).addAppender(appender, level, null); context.updateLoggers(config); }
private void validateAppender(final LoggerContext loggerContext, final String expectedFilePattern) { final RollingFileAppender appender = loggerContext.getConfiguration().getAppender("fooAppender"); Assert.assertNotNull(appender); Assert.assertEquals(expectedFilePattern, appender.getFilePattern()); LogManager.getLogger("root").info("just to show it works."); } }
@Override public void setLevel(Level level) { this.level = level; org.apache.logging.log4j.Level l = org.apache.logging.log4j.Level.toLevel(level.getName(), org.apache.logging.log4j.Level.ERROR); logger.setLevel(l); logger.getContext().getConfiguration().getLoggerConfig(this.logger.getName()).setLevel(l); }
@Test public void testHeaderFooter() { final String header = "# Header"; final String footer = "# Footer "; final AbstractCsvLayout layout = CsvLogEventLayout.createLayout(ctx.getConfiguration(), "Excel", null, null, null, null, null, null, null, header, footer); testLayout(CSVFormat.DEFAULT, layout, header, footer); }
@Test public void testGetLoggerConfigEmpty() throws Exception { final Configuration config = this.ctx.getConfiguration(); assertEquals(config.getRootLogger(), config.getLoggerConfig(Strings.EMPTY)); }
@Test public void testUsePlatformDefaultIfNoCharset() throws Exception { final PatternLayout layout = PatternLayout.newBuilder().withPattern("%m") .withConfiguration(ctx.getConfiguration()).build(); assertEquals(Charset.defaultCharset(), layout.getCharset()); }
@Test public void testHeaderFooterThreadContext() throws Exception { final PatternLayout layout = PatternLayout.newBuilder().withPattern("%d{UNIX} %m") .withConfiguration(ctx.getConfiguration()).withHeader("${ctx:header}").withFooter("${ctx:footer}") .build(); ThreadContext.put("header", "Hello world Header"); ThreadContext.put("footer", "Hello world Footer"); final byte[] header = layout.getHeader(); assertNotNull("No header", header); assertTrue("expected \"Hello world Header\", actual " + Strings.dquote(new String(header)), new String(header).equals(new String("Hello world Header"))); }
@Test public void testRegex() throws Exception { final PatternLayout layout = PatternLayout.newBuilder().withPattern(regexPattern) .withConfiguration(ctx.getConfiguration()).build(); final LogEvent event = Log4jLogEvent.newBuilder() // .setLoggerName(this.getClass().getName()).setLoggerFqcn("org.apache.logging.log4j.core.Logger") // .setLevel(Level.INFO) // .setMessage(new SimpleMessage("Hello, world!")).build(); assertToByteArray("org/apache/logging/log4j/core/layout/PatternLayoutTest Hello, world!", layout, event); assertEncode("org/apache/logging/log4j/core/layout/PatternLayoutTest Hello, world!", layout, event); }