Search in sources :

Example 11 with ConfigurationException

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

the class Log4j1ConfigurationConverter method run.

private void run() {
    if (cla.getRecurseIntoPath() != null) {
        final AtomicInteger countOKs = new AtomicInteger();
        final AtomicInteger countFails = new AtomicInteger();
        try {
            Files.walkFileTree(cla.getRecurseIntoPath(), new SimpleFileVisitor<Path>() {

                @Override
                public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
                    if (cla.getPathIn() == null || file.getFileName().equals(cla.getPathIn())) {
                        verbose("Reading %s", file);
                        String newFile = file.getFileName().toString();
                        final int lastIndex = newFile.lastIndexOf(".");
                        newFile = lastIndex < 0 ? newFile + FILE_EXT_XML : newFile.substring(0, lastIndex) + FILE_EXT_XML;
                        final Path resolved = file.resolveSibling(newFile);
                        try (final InputStream input = new InputStreamWrapper(Files.newInputStream(file), file.toString());
                            final OutputStream output = Files.newOutputStream(resolved)) {
                            try {
                                convert(input, output);
                                countOKs.incrementAndGet();
                            } catch (ConfigurationException | IOException e) {
                                countFails.incrementAndGet();
                                if (cla.isFailFast()) {
                                    throw e;
                                }
                                e.printStackTrace();
                            }
                            verbose("Wrote %s", resolved);
                        }
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (final IOException e) {
            throw new ConfigurationException(e);
        } finally {
            verbose("OK = %,d, Failures = %,d, Total = %,d", countOKs.get(), countFails.get(), countOKs.get() + countFails.get());
        }
    } else {
        verbose("Reading %s", cla.getPathIn());
        try (final InputStream input = getInputStream();
            final OutputStream output = getOutputStream()) {
            convert(input, output);
        } catch (final IOException e) {
            throw new ConfigurationException(e);
        }
        verbose("Wrote %s", cla.getPathOut());
    }
}
Also used : Path(java.nio.file.Path) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConfigurationException(org.apache.logging.log4j.core.config.ConfigurationException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 12 with ConfigurationException

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

the class Log4j1ConfigurationParser method buildConfigurationBuilder.

/**
     * Parses a Log4j 1.2 properties configuration file in ISO 8859-1 encoding into a ConfigurationBuilder.
     *
     * @param input
     *            InputStream to read from is assumed to be ISO 8859-1, and will not be closed.
     * @return the populated ConfigurationBuilder, never {@literal null}
     * @throws IOException
     *             if unable to read the input
     * @throws ConfigurationException
     *             if the input does not contain a valid configuration
     */
public ConfigurationBuilder<BuiltConfiguration> buildConfigurationBuilder(final InputStream input) throws IOException {
    try {
        properties.load(input);
        strSubstitutorProperties = new StrSubstitutor(properties);
        strSubstitutorSystem = new StrSubstitutor(System.getProperties());
        final String rootCategoryValue = getLog4jValue(ROOTCATEGORY);
        final String rootLoggerValue = getLog4jValue(ROOTLOGGER);
        if (rootCategoryValue == null && rootLoggerValue == null) {
            // This is not a Log4j 1 properties configuration file.
            warn("Missing " + ROOTCATEGORY + " or " + ROOTLOGGER + " in " + input);
        // throw new ConfigurationException(
        // "Missing " + ROOTCATEGORY + " or " + ROOTLOGGER + " in " + input);
        }
        builder.setConfigurationName("Log4j1");
        // DEBUG
        final String debugValue = getLog4jValue("debug");
        if (Boolean.valueOf(debugValue)) {
            builder.setStatusLevel(Level.DEBUG);
        }
        // Root
        buildRootLogger(getLog4jValue(ROOTCATEGORY));
        buildRootLogger(getLog4jValue(ROOTLOGGER));
        // Appenders
        final Map<String, String> appenderNameToClassName = buildClassToPropertyPrefixMap();
        for (final Map.Entry<String, String> entry : appenderNameToClassName.entrySet()) {
            final String appenderName = entry.getKey();
            final String appenderClass = entry.getValue();
            buildAppender(appenderName, appenderClass);
        }
        // Loggers
        buildLoggers("log4j.category.");
        buildLoggers("log4j.logger.");
        buildProperties();
        return builder;
    } catch (final IllegalArgumentException e) {
        throw new ConfigurationException(e);
    }
}
Also used : StrSubstitutor(org.apache.logging.log4j.core.lookup.StrSubstitutor) ConfigurationException(org.apache.logging.log4j.core.config.ConfigurationException) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Aggregations

ConfigurationException (org.apache.logging.log4j.core.config.ConfigurationException)12 Annotation (java.lang.annotation.Annotation)2 AccessibleObject (java.lang.reflect.AccessibleObject)2 Map (java.util.Map)2 Properties (java.util.Properties)2 RootLoggerComponentBuilder (org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder)2 PluginAliases (org.apache.logging.log4j.core.config.plugins.PluginAliases)2 ConstraintValidator (org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Field (java.lang.reflect.Field)1 FileVisitResult (java.nio.file.FileVisitResult)1 Path (java.nio.file.Path)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 TreeMap (java.util.TreeMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Appender (org.apache.logging.log4j.core.Appender)1