log4J2の基本

今回は日本語でなるべく簡単にlog4j2について話したいと思う。

まずはじめに、loggerと聞くとまぁだいたいログを作るもんだなーって感じがある。

例えばコンパイルの時とかアプリがrunnigしている時。

最初はこのloggerというものに自分の想像があり、それは多分どこかでloggerのインスタンスを作って、例えば"ここからここ"までな感じでコードの中でstartとstopを指定してあげればできるはずだと思った。

 

そして結果から言うとそんなに簡単ではない。

これをわかりやすく説明するため、以下のリンク先の内容を簡単に日本語にしてみた。(original source is here(Introducing Log4J 2 - Enterprise Class Logging - Spring Framework Guru)簡単に言うと最初に把握しておくポイントは二つある:

  • まず、logにはエラーによって異なるレベルがあるhttps://i2.wp.com/springframework.guru/wp-content/uploads/2016/02/Log_Levels.png?ssl=1

まずこれを覚えたほうがいい。

例えば: "While developing in your local machine, it is common to set the log level to DEBUG. This will give you detailed log messages for your development use. While on production, its typical set the log level to ERROR. This is to avoid filling your logs with excessive debug information. And while logging is very efficient, there is still a cost. In your application, once you have retrieved a logger, you call one of the printing methods debug()info()warn()error()fatal(), and log() on the logger to log messages."

  • 次に、Appenderとは何か?

Once you capture logging information through a logger, you need to send it to an output destination. The output destination is called an appender, and it is attached to the logger.

日本語で言うと、どういう方式、あるいはどこでそのlogの結果を見たいか。例えば、コンソールで?ファイル内で?何かのGUIで?みたいな