[[Log4j2]] > Log4jのログレベルをアプリケーションから設定する * Log4jのログレベルをアプリケーションから設定する [#w87067db] #setlinebreak(on); #contents ** getLoggerの前にRootロガーのログレベルを変更しておく [#w52d30d6] #mycode2(){{ class Example { static { Level level = Level.WARN; LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false); Configuration configuration = loggerContext.getConfiguration(); LoggerConfig loggerConfig = configuration.getLoggerConfig(LogManager.ROOT_LOGGER_NAME); loggerConfig.setLevel(level); loggerContext.updateLoggers(); } static final Logger logger = LogManager.getLogger(Example.class); public static void main(String[] args){ logger.info("info log!!!"); logger.debug("debug log!!!"); logger.warn("warn log!!!"); logger.error("error log!!!"); logger.fatal("fatal log!!!"); } } }} ** 変更したログレベルの保持 [#s109055d] 変更したRootロガーのログレベルは、以降の別クラス等の getLogger でも有効な為、 アプリケーションの最初に実行される処理でログレベルを変更しておけば、都度ログレベルを設定する必要は無い。 以下、環境変数からログレベルを取得してRootロガーに設定するサンプル。 ※メインクラスは service.Sample1Service 例) constants.SampleConstants.java #mycode2(){{ public class SampleConstants { public static final String APPNAME = "SampleApp"; public static final String LOG_LEVEL; // 初期化 static { // 環境変数からログレベルを取得 String logLevel = System.getenv("LOG_LEVEL"); LOG_LEVEL = Strings.isNullOrEmpty(logLevel) ? "INFO" : logLevel; // ログレベルをRootロガーに設定 setLogLevelToLogger(); } private static void setLogLevelToLogger() { Level level = Level.INFO; switch (SampleConstants.LOG_LEVEL) { case "DEBUG": level = Level.DEBUG; break; case "WARN": level = Level.WARN; break; case "ERROR": level = Level.ERROR; break; } LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false); Configuration configuration = loggerContext.getConfiguration(); LoggerConfig loggerConfig = configuration.getLoggerConfig(LogManager.ROOT_LOGGER_NAME); loggerConfig.setLevel(level); loggerContext.updateLoggers(); } } }} service.Sample1Service.java #mycode2(){{ public class Sample1Service { // SampleConstantsのstaticイニシャライザでログレベルを設定しているので、 // SampleConstants の適当な定数を使用する。(staticイニシャライザを呼びたいので) private static final String APPNAME = constants.SampleConstants.APPNAME; static final Logger logger = LogManager.getLogger(Sample1Service.class); public static void main(String[] args) { logger.info("START APPNAME: " + APPNAME); logger.info("info log!!!"); logger.debug("debug log!!!"); logger.warn("warn log!!!"); logger.error("error log!!!"); logger.fatal("fatal log!!!"); logger.info("END"); } } }} build.gradle #mycode2(){{ apply plugin: 'java' apply plugin: 'application' apply plugin: 'eclipse' mainClassName = 'service.Sample1Service' repositories { jcenter() } dependencies { compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.11.0' compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.0' } }} *** 実験1 [#hd588f9c] #myterm2(){{ gradle run }} 結果 #myterm2(){{ 2018-06-11 21:36:37,576000 INFO Sample1Service.main START 2018-06-11 21:36:37,581000 INFO Sample1Service.main info log!!! 2018-06-11 21:36:37,581000 WARN Sample1Service.main warn log!!! 2018-06-11 21:36:37,582000 ERROR Sample1Service.main error log!!! 2018-06-11 21:36:37,583000 FATAL Sample1Service.main fatal log!!! 2018-06-11 21:36:37,583000 INFO Sample1Service.main END }} *** 実験2 [#w7190943] #myterm2(){{ export LOG_LEVEL=WARN gradle run }} 結果 #myterm2(){{ 2018-06-11 21:38:05,010000 WARN Sample1Service.main warn log!!! 2018-06-11 21:38:05,013000 ERROR Sample1Service.main error log!!! 2018-06-11 21:38:05,013000 FATAL Sample1Service.main fatal log!!! }}