Log4j2で全設定をコードで行う方法

# Log4j2で設定ファイルを使用せずにコードで設定を行う方法のメモ

## サンプルソース
ConsoleAppenderとRollingFileAppenderを使用した例

```java:sample.java

// ログ出力パターン
// 例) 2016-10-05 11:30:53.749  INFO   Hello world
private static final String LOG_PATTERN = "%d{yyy-MM-dd HH:mm:ss.SSS}  %-5level %logger{0} %msg%n";

// ログファイル名
private static final String LOG_NAME = "sample.log";

// ローテーション後のログファイル命名パターン
private static final String LOG_NAME_PATTERN = "sample_%i.log";

// ConsoleAppender名
private static final String CONSOLE_APPENDER_NAME = "CONSOLE_APPENDER";

// RollingFileAppender名
private static final String ROLLING_APPENDER_NAME = "FILE_APPERNDER";

// ログファイル最大サイズ
private static final String LOG_FILE_SIZE = "10MB";

// ログファイル世代管理数
private static final String LOG_ROLLOVER_STRATEGY = "10";

// ログレベル
private static final Level LOG_LEVEL = Level.INFO;

/**
 * ロガー生成
 **/
private static Logger getLogger() {

	try {
		String logDir = "logs" +  File.separator;

		// create LoggerContext
		LoggerContext context = (LoggerContext) LogManager.getContext(false);
		Configuration config = context.getConfiguration();
		PatternLayout layout = PatternLayout.createLayout(
				LOG_PATTERN, 
				null, 
				null, 
				Charset.defaultCharset(), 
				false,
				false, 
				null, 
				null);

		SizeBasedTriggeringPolicy policy = SizeBasedTriggeringPolicy.createPolicy(LOG_FILE_SIZE);
		DefaultRolloverStrategy strategy = DefaultRolloverStrategy.createStrategy(
				LOG_ROLLOVER_STRATEGY, 
				"0", 
				null, 
				null, 
				config);

		RollingFileManager fileManager = RollingFileManager.getFileManager(
				logDir + LOG_NAME,
				logDir + LOG_NAME_PATTERN,
				false,
				false,
				policy,
				strategy,
				null,
				layout,
				128);
		policy.initialize(fileManager);

		// create Appenders
		Appender rollingAppender = RollingFileAppender.createAppender(
				logDir + LOG_NAME,
				logDir + LOG_NAME_PATTERN,
				"false",
				ROLLING_APPENDER_NAME,
				"false",
				"128",
				"true",
				policy,
				strategy,
				layout,
				(Filter) null,
				"false",
				"false",
				(String) null,
				config);
		rollingAppender.start();

		Appender consoleAppender = ConsoleAppender.createAppender(
				layout,
				null,
				null,
				CONSOLE_APPENDER_NAME,
				null,
				null);
		consoleAppender.start();

		// Appender ref
		AppenderRef ref = AppenderRef.createAppenderRef(CONSOLE_APPENDER_NAME, LOG_LEVEL, null);
		AppenderRef ref2 = AppenderRef.createAppenderRef(ROLLING_APPENDER_NAME, LOG_LEVEL, null);
		AppenderRef[] refs = new AppenderRef[] { ref, ref2 };

		// LoggerConfig
		LoggerConfig loggerConfig = LoggerConfig.createLogger(
				"true",
				LOG_LEVEL,
				LogManager.ROOT_LOGGER_NAME,
				"true",
				refs,
				null,
				config,
				null);
		loggerConfig.addAppender(rollingAppender, LOG_LEVEL, null);
		loggerConfig.addAppender(consoleAppender, LOG_LEVEL, null);

		config.addLogger(LogManager.ROOT_LOGGER_NAME, loggerConfig);
		context.updateLoggers(config);

	} catch (Exception e) {
		System.out.println(e);

	}
	return LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
}
```


XMLだとこんな感じ

``` xml:lo4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error">

    <!-- Properties -->
    <Properties>
        <Property name="pattern">%d{yyy-MM-dd HH:mm:ss.SSS}  %-5level %logger{0} %msg%n</Property>
    </Properties>

    <!-- Console Appender -->
    <Appenders>
        <Console name="CONSOLE_APPENDER" target="SYSTEM_OUT">
            <PatternLayout pattern="${pattern}" />
        </Console>

        <!-- Rolling File Appender -->
        <RollingFile name="FILE_APPERNDER"
            fileName="logs/sample.log"
            filePattern="logs/sample_%i.log">
            <PatternLayout pattern="${pattern}" />
            <Policies>
                <SizeBasedTriggeringPolicy size="10MB" />
                <TimeBasedTriggeringPolicy />
            </Policies>
            <DefaultRolloverStrategy max="10" />
        </RollingFile>
    </Appenders>

    <Loggers>
        <Root level="info">
            <AppenderRef ref="CONSOLE_APPENDER" />
            <AppenderRef ref="FILE_APPERNDER" />
        </Root>
    </Loggers>
</Configuration>
```