001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.config;
018
019import java.io.Serializable;
020
021import org.apache.logging.log4j.Level;
022import org.apache.logging.log4j.core.Appender;
023import org.apache.logging.log4j.core.Layout;
024import org.apache.logging.log4j.core.appender.ConsoleAppender;
025import org.apache.logging.log4j.core.layout.PatternLayout;
026import org.apache.logging.log4j.util.PropertiesUtil;
027
028/**
029 * The default configuration writes all output to the Console using the default logging level. You configure default
030 * logging level by setting the system property "org.apache.logging.log4j.level" to a level name. If you do not
031 * specify the property, Log4j uses the ERROR Level. Log Events will be printed using the basic formatting provided
032 * by each Message.
033 */
034public class DefaultConfiguration extends AbstractConfiguration {
035
036    /**
037     * The name of the default configuration.
038     */
039    public static final String DEFAULT_NAME = "Default";
040    /**
041     * The System Property used to specify the logging level.
042     */
043    public static final String DEFAULT_LEVEL = "org.apache.logging.log4j.level";
044    /**
045     * The default Pattern used for the default Layout.
046     */
047    public static final String DEFAULT_PATTERN = "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n";
048
049    /**
050     * Constructor to create the default configuration.
051     */
052    public DefaultConfiguration() {
053        super(ConfigurationSource.NULL_SOURCE);
054        
055        setName(DEFAULT_NAME);
056        final Layout<? extends Serializable> layout = PatternLayout.newBuilder()
057            .withPattern(DEFAULT_PATTERN)
058            .withConfiguration(this)
059            .build();
060        final Appender appender =
061                ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "Console", "false", "true");
062        appender.start();
063        addAppender(appender);
064        final LoggerConfig root = getRootLogger();
065        root.addAppender(appender, null, null);
066
067        final String levelName = PropertiesUtil.getProperties().getStringProperty(DEFAULT_LEVEL);
068        final Level level = levelName != null && Level.valueOf(levelName) != null ?
069            Level.valueOf(levelName) : Level.ERROR;
070        root.setLevel(level);
071    }
072
073    @Override
074    protected void doConfigure() {
075    }
076}