Refine search
public static void main(final String[] args) { System.setProperty("log4j.skipJansi", "false"); // LOG4J2-2087: explicitly enable try (final LoggerContext ctx = Configurator.initialize(ConsoleAppenderAnsiMessagesMain.class.getName(), "target/test-classes/log4j2-console-highlight-default.xml")) { LOG.fatal("Fatal message."); LOG.error("Error message."); LOG.warn("Warning message."); LOG.info("Information message."); LOG.debug("Debug message."); LOG.trace("Trace message."); LOG.error("Error message.", new IOException("test")); } }
@Test public void debugChangeLevel() { logger.debug("Debug message 1"); final List<LogEvent> events = app.getEvents(); assertEventCount(events, 1); Configurator.setLevel(logger.getName(), Level.OFF); logger.debug("Debug message 2"); assertEventCount(events, 1); Configurator.setLevel(logger.getName(), Level.DEBUG); logger.debug("Debug message 3"); assertEventCount(events, 2); }
@Test public void debugChangeRootLevel() { logger.debug("Debug message 1"); final List<LogEvent> events = app.getEvents(); assertEventCount(events, 1); Configurator.setRootLevel(Level.OFF); logger.debug("Debug message 2"); assertEventCount(events, 1); Configurator.setRootLevel(Level.DEBUG); logger.debug("Debug message 3"); assertEventCount(events, 2); }
public void test(final String[] args) { System.setProperty("log4j.skipJansi", "false"); // LOG4J2-2087: explicitly enable // System.out.println(System.getProperty("java.class.path")); final String config = args == null || args.length == 0 ? "target/test-classes/log4j2-console-xex-ansi.xml" : args[0]; final LoggerContext ctx = Configurator.initialize(ConsoleAppenderAnsiMessagesMain.class.getName(), config); final Logger logger = LogManager.getLogger(ConsoleAppenderJAnsiXExceptionMain.class); try { Files.getFileStore(Paths.get("?BOGUS?")); } catch (final Exception e) { final IllegalArgumentException logE = new IllegalArgumentException("Bad argument foo", e); logger.error("Gotcha!", logE); } finally { Configurator.shutdown(ctx); } }
@Test public void validateXmlSchema() throws Exception { final File file = new File("target", "XmlCompactFileAppenderValidationTest.log.xml"); file.delete(); final Logger log = LogManager.getLogger("com.foo.Bar"); log.warn("Message 1"); log.info("Message 2"); log.debug("Message 3"); Configurator.shutdown(this.loggerContext); this.validateXmlSchema(file); }
@Test public void debugChangeLevelAllChildrenLoggers() { // Use logger AND child loggers logger.debug("Debug message 1"); loggerChild.debug("Debug message 1 child"); loggerGrandchild.debug("Debug message 1 grandchild"); final List<LogEvent> events = app.getEvents(); assertEventCount(events, 3); Configurator.setAllLevels(logger.getName(), Level.OFF); logger.debug("Debug message 2"); loggerChild.warn("Warn message 2 child"); loggerGrandchild.fatal("Fatal message 2 grandchild"); assertEventCount(events, 3); Configurator.setAllLevels(logger.getName(), Level.DEBUG); logger.debug("Debug message 3"); loggerChild.warn("Trace message 3 child"); loggerGrandchild.trace("Fatal message 3 grandchild"); assertEventCount(events, 5); }
commander.parse(args); if (config.isDebug()) { Configurator.setRootLevel(org.apache.logging.log4j.Level.DEBUG); if (privateKey instanceof RSAPrivateKey) { rsaPrivateKey = ((RSAPrivateKey) privateKey).getPrivateExponent(); LOGGER.info("RSA privateKey:" + rsaPrivateKey.toString()); } else { CONSOLE.info("PrivateKey file does not look like an RSA private key!"); CONSOLE.info("Could not read private key"); LOGGER.warn(E); return;
@Test public void testNoLoggers() throws Exception { ctx = Configurator.initialize("Test1", "bad/log4j-loggers.xml"); LogManager.getLogger("org.apache.test.TestConfigurator"); final Configuration config = ctx.getConfiguration(); assertNotNull("No configuration", config); final String name = DefaultConfiguration.DEFAULT_NAME + "@" + Integer.toHexString(config.hashCode()); assertEquals("Unexpected Configuration.", name, config.getName()); }
public static void main(final String[] args) { try (final LoggerContext ctx = Configurator.initialize(Jira739Test.class.getName(), "target/test-classes/LOG4J2-739.xml")) { for (int i = 0; i < 10; i++) { LOG.trace("Entering Log4j Example " + i + " times"); LOG.error("Ohh!Failed!"); LOG.trace("Exiting Log4j Example." + i + " times"); } } }
@Test public void test() throws Exception { // To ensure our custom plugin is NOT included in the log4j plugin metadata file, // we make sure the class does not exist until after the build is finished. // So we don't create the custom plugin class until this test is run. final File orig = new File("target/test-classes/customplugin/FixedStringLayout.java.source"); final File f = new File(orig.getParentFile(), "FixedStringLayout.java"); assertTrue("renamed source file OK", orig.renameTo(f)); compile(f); assertTrue("reverted source file OK", f.renameTo(orig)); // load the compiled class Class.forName("customplugin.FixedStringLayout"); // now that the custom plugin class exists, we load the config // with the packages element pointing to our custom plugin ctx = Configurator.initialize("Test1", "customplugin/log4j2-741.xml"); config = ctx.getConfiguration(); listAppender = config.getAppender("List"); final Logger logger = LogManager.getLogger(PluginManagerPackagesTest.class); logger.info("this message is ignored"); final List<String> messages = listAppender.getMessages(); assertEquals(messages.toString(), 1, messages.size()); assertEquals("abc123XYZ", messages.get(0)); }
public static void main(final String[] args) { MainMapLookup.setMainArguments(args); try (final LoggerContext ctx = Configurator.initialize(MainInputArgumentsLookupTest.class.getName(), "target/test-classes/log4j-lookup-main.xml")) { LogManager.getLogger().error("this is an error message"); } }
final File file = new File("target/test-classes/log4j2-config.xml"); assertTrue("setLastModified should have succeeded.", file.setLastModified(System.currentTimeMillis() - 120000)); ctx = Configurator.initialize("Test1", "target/test-classes/log4j2-config.xml"); final Logger logger = LogManager.getLogger("org.apache.test.TestConfigurator"); Configuration config = ctx.getConfiguration(); assertNotNull("No configuration", config); assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName()); final Map<String, Appender> map = config.getAppenders(); assertNotNull("Appenders map should not be null.", map); assertThat(map, hasSize(greaterThan(0))); TimeUnit.SECONDS.sleep(config.getWatchManager().getIntervalSeconds()+1); for (int i = 0; i < 17; ++i) { logger.debug("Test message " + i); if (is(theInstance(config)).matches(ctx.getConfiguration())) { Thread.sleep(500); final Configuration newConfig = ctx.getConfiguration(); assertThat("Configuration not reset", newConfig, is(not(theInstance(config)))); Configurator.shutdown(ctx); config = ctx.getConfiguration(); assertEquals("Unexpected Configuration.", NullConfiguration.NULL_NAME, config.getName());
public static void main(final String[] args) throws Exception { final File file = new File(CONFIG); try (final LoggerContext ctx = Configurator.initialize("LogTest", LogRolloverTest.class.getClassLoader(), file.toURI())) { final Logger logger = LogManager.getLogger("TestLogger"); for (long i = 0;; i += 1) { logger.debug("Sequence: " + i); Thread.sleep(250); } } } }
public void test(final String[] args) { System.setProperty("log4j.skipJansi", "false"); // LOG4J2-2087: explicitly enable // System.out.println(System.getProperty("java.class.path")); final String config = args == null || args.length == 0 ? "target/test-classes/log4j2-console-msg-ansi.xml" : args[0]; try (final LoggerContext ctx = Configurator.initialize(ConsoleAppenderAnsiMessagesMain.class.getName(), config)) { final Logger logger = LogManager.getLogger(ConsoleAppenderJAnsiMessageMain.class); logger.info(ansi().fg(RED).a("Hello").fg(CYAN).a(" World").reset()); // JAnsi format: // logger.info("@|red Hello|@ @|cyan World|@"); for (final Entry<Object, Object> entry : System.getProperties().entrySet()) { logger.info("@|KeyStyle {}|@ = @|ValueStyle {}|@", entry.getKey(), entry.getValue()); } } }
@Test public void validateXmlSchemaThrowable() throws Exception { final File file = new File("target", "XmlCompactFileAppenderValidationTest.log.xml"); file.delete(); final Logger log = LogManager.getLogger("com.foo.Bar"); try { throw new IllegalArgumentException("IAE"); } catch (final IllegalArgumentException e) { log.warn("Message 1", e); } Configurator.shutdown(this.loggerContext); this.validateXmlSchema(file); }
static void test(final String[] args, final String config) { // System.out.println(System.getProperty("java.class.path")); try (final LoggerContext ctx = Configurator .initialize(ConsoleAppenderDefaultSuppressedThrowable.class.getName(), config)) { final IOException ioEx = new IOException("test suppressed"); ioEx.addSuppressed(new IOException("test suppressed 1", new IOException("test 1"))); final IOException ioEx2 = new IOException("test 2"); ioEx2.addSuppressed(new IOException("test 3")); ioEx.addSuppressed(new IOException("test suppressed 2", ioEx2)); final IOException e = new IOException("test", ioEx); LOG.error("Error message {}, suppressed?", "Hi", e); System.out.println("printStackTrace"); e.printStackTrace(); } }
public static void defaultIfMissing(String logger, Level defaultLvl) { final Configuration loggingConfig = LoggerContext.getContext(false).getConfiguration(); final LoggerConfig loggerConfig = loggingConfig.getLoggers().get(logger); if (loggerConfig != null) { LOGGER.info("{} log level is {}", logger, loggerConfig.getLevel()); } else { LOGGER.info("Setting {} log level to {}", logger, defaultLvl); Configurator.setLevel(logger, defaultLvl); } } }
/** * @param args */ public static void main(final String[] args) { try (final LoggerContext ctx = Configurator.initialize(FormatterLoggerManualExample.class.getName(), "target/test-classes/log4j2-console.xml");) { final User user = new User(); logger.debug("User %s with birthday %s", user.getName(), user.getBirthdayCalendar()); logger.debug("User %1$s with birthday %2$tm %2$te, %2$tY", user.getName(), user.getBirthdayCalendar()); logger.debug("Integer.MAX_VALUE = %,d", Integer.MAX_VALUE); logger.debug("Long.MAX_VALUE = %,d", Long.MAX_VALUE); } }
@Override public void applyDelegate(Config config) { Security.addProvider(new BouncyCastleProvider()); if (isDebug()) { Configurator.setAllLevels("de.rub.nds.tlsattacker", Level.DEBUG); } else if (quiet) { Configurator.setAllLevels("de.rub.nds.tlsattacker", Level.OFF); } LOGGER.debug("Using the following security providers"); for (Provider p : Security.getProviders()) { LOGGER.debug("Provider {}, version, {}", p.getName(), p.getVersion()); } // remove stupid Oracle JDK security restriction (otherwise, it is not // possible to use strong crypto with Oracle JDK) UnlimitedStrengthEnabler.enable(); } }
public static void setLevel(Logger logger, Level level) { if (!LogManager.ROOT_LOGGER_NAME.equals(logger.getName())) { Configurator.setLevel(logger.getName(), level); } else { final LoggerContext ctx = LoggerContext.getContext(false); final Configuration config = ctx.getConfiguration(); final LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName()); loggerConfig.setLevel(level); ctx.updateLoggers(); } // we have to descend the hierarchy final LoggerContext ctx = LoggerContext.getContext(false); for (final LoggerConfig loggerConfig : ctx.getConfiguration().getLoggers().values()) { if (LogManager.ROOT_LOGGER_NAME.equals(logger.getName()) || loggerConfig.getName().startsWith(logger.getName() + ".")) { Configurator.setLevel(loggerConfig.getName(), level); } } }