Javaメモ > Log4j2 †ダウンロード/インストール †https://logging.apache.org/log4j/2.x/maven-artifacts.html Gradleの場合の例 †build.gradle 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' } 設定ファイル †classpatnが通っているディレクトリに log4j2.xml または log4j2.json を配置する。 log4j2.xml <?xml version="1.0" encoding="UTF-8"?> <Configuration> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{DEFAULT_MICROS} %-5level %c{1}.%M %msg%n"/> </Console> </Appenders> <Loggers> <Root level="debug"> <AppenderRef ref="Console"/> </Root> </Loggers> </Configuration> https://logging.apache.org/log4j/2.x/manual/layouts.html#PatternLayout 使い方 †https://logging.apache.org/log4j/2.x/manual/configuration.html package net.magata; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; public class Example { // Loggerの取り方が jog4j 1系と違うので注意 static final Logger logger = LogManager.getLogger(Example.class.getName()); public static void main(String[] args){ logger.info("START"); logger.trace("Trace Message"); logger.debug("Debug Message"); logger.info("Info Message"); logger.warn("Warn Message"); logger.error("Error Message"); logger.fatal("Fatal Message"); logger.info("END"); } } 結果 †2018-04-16 21:41:50,235000 INFO Example.main START 2018-04-16 21:41:50,238000 DEBUG Example.main Debug Message 2018-04-16 21:41:50,239000 INFO Example.main Info Message 2018-04-16 21:41:50,239000 WARN Example.main Warn Message 2018-04-16 21:41:50,239000 ERROR Example.main Error Message 2018-04-16 21:41:50,244000 FATAL Example.main Fatal Message 2018-04-16 21:41:50,244000 INFO Example.main END |