Search in sources :

Example 11 with DefaultConfiguration

use of org.apache.logging.log4j.core.config.DefaultConfiguration in project logging-log4j2 by apache.

the class ScriptConditionTest method testSelectFilesToDelete.

@Test
public void testSelectFilesToDelete() {
    final Configuration config = new DefaultConfiguration();
    // creates the ScriptManager
    config.initialize();
    // script that returns pathList
    final Script script = new Script("test", "javascript", "pathList;");
    final ScriptCondition condition = new ScriptCondition(script, config);
    final List<PathWithAttributes> pathList = new ArrayList<>();
    final Path base = Paths.get("baseDirectory");
    final List<PathWithAttributes> result = condition.selectFilesToDelete(base, pathList);
    assertSame(result, pathList);
}
Also used : Path(java.nio.file.Path) Script(org.apache.logging.log4j.core.script.Script) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) ArrayList(java.util.ArrayList) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Test(org.junit.Test)

Example 12 with DefaultConfiguration

use of org.apache.logging.log4j.core.config.DefaultConfiguration in project logging-log4j2 by apache.

the class AbstractJpaAppenderTest method setUp.

public void setUp(final String configFileName) throws SQLException {
    this.connection = this.setUpConnection();
    System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "org/apache/logging/log4j/core/appender/db/jpa/" + configFileName);
    final LoggerContext context = LoggerContext.getContext(false);
    if (context.getConfiguration() instanceof DefaultConfiguration) {
        context.reconfigure();
    }
    StatusLogger.getLogger().reset();
}
Also used : DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 13 with DefaultConfiguration

use of org.apache.logging.log4j.core.config.DefaultConfiguration in project logging-log4j2 by apache.

the class SmtpAppender method createAppender.

/**
     * Create a SmtpAppender.
     *
     * @param name
     *            The name of the Appender.
     * @param to
     *            The comma-separated list of recipient email addresses.
     * @param cc
     *            The comma-separated list of CC email addresses.
     * @param bcc
     *            The comma-separated list of BCC email addresses.
     * @param from
     *            The email address of the sender.
     * @param replyTo
     *            The comma-separated list of reply-to email addresses.
     * @param subject The subject of the email message.
     * @param smtpProtocol The SMTP transport protocol (such as "smtps", defaults to "smtp").
     * @param smtpHost
     *            The SMTP hostname to send to.
     * @param smtpPortStr
     *            The SMTP port to send to.
     * @param smtpUsername
     *            The username required to authenticate against the SMTP server.
     * @param smtpPassword
     *            The password required to authenticate against the SMTP server.
     * @param smtpDebug
     *            Enable mail session debuging on STDOUT.
     * @param bufferSizeStr
     *            How many log events should be buffered for inclusion in the
     *            message?
     * @param layout
     *            The layout to use (defaults to HtmlLayout).
     * @param filter
     *            The Filter or null (defaults to ThresholdFilter, level of
     *            ERROR).
     * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
     *               they are propagated to the caller.
     * @return The SmtpAppender.
     */
@PluginFactory
public static SmtpAppender createAppender(@PluginConfiguration final Configuration config, @PluginAttribute("name") @Required final String name, @PluginAttribute("to") final String to, @PluginAttribute("cc") final String cc, @PluginAttribute("bcc") final String bcc, @PluginAttribute("from") final String from, @PluginAttribute("replyTo") final String replyTo, @PluginAttribute("subject") final String subject, @PluginAttribute("smtpProtocol") final String smtpProtocol, @PluginAttribute("smtpHost") final String smtpHost, @PluginAttribute(value = "smtpPort", defaultString = "0") @ValidPort final String smtpPortStr, @PluginAttribute("smtpUsername") final String smtpUsername, @PluginAttribute(value = "smtpPassword", sensitive = true) final String smtpPassword, @PluginAttribute("smtpDebug") final String smtpDebug, @PluginAttribute("bufferSize") final String bufferSizeStr, @PluginElement("Layout") Layout<? extends Serializable> layout, @PluginElement("Filter") Filter filter, @PluginAttribute("ignoreExceptions") final String ignore) {
    if (name == null) {
        LOGGER.error("No name provided for SmtpAppender");
        return null;
    }
    final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
    final int smtpPort = AbstractAppender.parseInt(smtpPortStr, 0);
    final boolean isSmtpDebug = Boolean.parseBoolean(smtpDebug);
    final int bufferSize = bufferSizeStr == null ? DEFAULT_BUFFER_SIZE : Integer.parseInt(bufferSizeStr);
    if (layout == null) {
        layout = HtmlLayout.createDefaultLayout();
    }
    if (filter == null) {
        filter = ThresholdFilter.createFilter(null, null, null);
    }
    final Configuration configuration = config != null ? config : new DefaultConfiguration();
    final SmtpManager manager = SmtpManager.getSmtpManager(configuration, to, cc, bcc, from, replyTo, subject, smtpProtocol, smtpHost, smtpPort, smtpUsername, smtpPassword, isSmtpDebug, filter.toString(), bufferSize);
    if (manager == null) {
        return null;
    }
    return new SmtpAppender(name, filter, layout, manager, ignoreExceptions);
}
Also used : DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) PluginConfiguration(org.apache.logging.log4j.core.config.plugins.PluginConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) SmtpManager(org.apache.logging.log4j.core.net.SmtpManager) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) PluginFactory(org.apache.logging.log4j.core.config.plugins.PluginFactory)

Example 14 with DefaultConfiguration

use of org.apache.logging.log4j.core.config.DefaultConfiguration in project logging-log4j2 by apache.

the class LateConfigTest method testReconfiguration.

@Test
public void testReconfiguration() throws Exception {
    final Configuration cfg = context.getConfiguration();
    assertNotNull("No configuration", cfg);
    assertTrue("Not set to default configuration", cfg instanceof DefaultConfiguration);
    final File file = new File(CONFIG);
    final LoggerContext loggerContext = LoggerContext.getContext(null, false, file.toURI());
    assertNotNull("No Logger Context", loggerContext);
    final Configuration newConfig = loggerContext.getConfiguration();
    assertTrue("Configuration not reset", cfg != newConfig);
    assertTrue("Reconfiguration failed", newConfig instanceof XmlConfiguration);
    context = LoggerContext.getContext(false);
    final Configuration sameConfig = context.getConfiguration();
    assertTrue("Configuration should not have been reset", newConfig == sameConfig);
}
Also used : DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) XmlConfiguration(org.apache.logging.log4j.core.config.xml.XmlConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) XmlConfiguration(org.apache.logging.log4j.core.config.xml.XmlConfiguration) File(java.io.File) Test(org.junit.Test)

Aggregations

DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)14 Configuration (org.apache.logging.log4j.core.config.Configuration)10 Test (org.junit.Test)7 Path (java.nio.file.Path)4 ArrayList (java.util.ArrayList)3 LoggerContext (org.apache.logging.log4j.core.LoggerContext)3 Script (org.apache.logging.log4j.core.script.Script)3 Setup (org.openjdk.jmh.annotations.Setup)3 File (java.io.File)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Timer (org.apache.logging.log4j.Timer)1 LogEvent (org.apache.logging.log4j.core.LogEvent)1 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)1 PluginConfiguration (org.apache.logging.log4j.core.config.plugins.PluginConfiguration)1 PluginFactory (org.apache.logging.log4j.core.config.plugins.PluginFactory)1 XmlConfiguration (org.apache.logging.log4j.core.config.xml.XmlConfiguration)1 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)1 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)1 SmtpManager (org.apache.logging.log4j.core.net.SmtpManager)1