五流!!日曜プログラマーのつどい - java Logger


java Logger

javaにおけるLoggerについて調べたことをまとめる.
javaのLoggerであるslf4jを使用するには以下の二つのパッケージが必要となる。
  • slf4j
  • logback

slf4jはLogging APIの抽象インタフェースクラスを提供し、
logbackはLogging APIの実装部になる。

他の抽象インタフェースは「Commons-logging」がある。
また、実装部には「log4j」などがある

・さまざまなインタフェースと実装をブリッジするjarがslf4jに含まれている。
 e.g.) jcl-over-slf4j***.jar, jul-to-slf4j***.jar
・基本的な使用方法は、slf4jと実装をバインディングを行いLoggingを行う。
 ただし、複数の実装とバインディングをするとnopバインダーによって全ての出力が破棄される。
 使用する実装部によりバインディングを変える必要がある。本家 jar環形の説明png
  • slf4j+logbackの組み合わせのjar
    • slf4j-api.jar (slf4jのjar)
    • logback-classic.jar (logbackのjar)
    • logback-core.jar (logbackのjar)

Install

下記のサイトのdownloadページから最新のjarを取得する。
slf4j 本家
logback本家

javadoc

Setting

slf4j + logback

  • slf4j+logbackの組み合わせのjarへパスを通す。
    • slf4j-api.jar (slf4jのjar)
    • logback-classic.jar (logbackのjar)
    • logback-core.jar (logbackのjar)
過去のソースでslf4jと異なるインタフェースで実装している場合は、上記のjarにブリッジ用のjarへパスを通す必要がある。
slf4jではじめから開発する場合は上記の3つのjarで足りる。

logback.xml

logbackのパッケージ内「logback-examples\src\main\java\chapters\appenders\conf\logback-Console.xml」を内容を
logback.xmlに書き込むだけで、コンソール出力が可能になる。
logback.xmlは実行する際に、クラスパスが設定されている場所に配置する必要がある。

<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%-4relative [%thread] %-5level - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="trace">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

Libraries Paths

    • slf4j-api.jar (slf4jのjar)
    • logback-classic.jar (logbackのjar)
    • logback-core.jar (logbackのjar)

Maven

Example



static void main(String[] args) {

Logger logger = LoggerFactory.getLogger("aLogger");
Marker fatal = MarkerFactory.getMarker("FATAL");

logger.trace("test");
logger.debug("test");
logger.info("test");
logger.warn("test");
logger.error("test");
logger.error(fatal, "test");

/* debug(String msg)*/
logger.debug("hogehoge1");

/* debug(Marker marker, String msg)*/
logger.debug(fatal, "hogehoge2");

/* debug(Marker marker, String format, Object arg1)*/
/* {} が第三引数の値で置き換わる。 "{}"自体を出力したい場合は"\\{}"。 "\"を表示したい場合は"\\\\" */
logger.debug(fatal, "hogehoge3 - {}",  new Integer(0));

/* debug(Marker marker, String format, Object... argments)*/
logger.debug(fatal, "hogehoge4 - {}, {}, {}", "Integer", new Integer(0), "END");

/* debug(String msg, Throwable t); */
logger.debug("hogehoge5", new Exception("some error"));

/* debug(Marker marker, String msg, Throwable t); */
logger.debug(fatal, "hogehoge6" ,new Exception("some error"));
}

Output Console
139  [main] TRACE - test
141  [main] DEBUG - test
141  [main] INFO  - test
141  [main] WARN  - test
141  [main] ERROR - test
141  [main] ERROR - test
141  [main] DEBUG - hogehoge1
141  [main] DEBUG - hogehoge2
141  [main] DEBUG - hogehoge3 - 0
141  [main] DEBUG - hogehoge4 - Integer, 0, END
144  [main] DEBUG - hogehoge5
java.lang.Exception: some error
	at testMain.main(testMain.java:47) [bin/:na]
145  [main] DEBUG - hogehoge6
java.lang.Exception: some error
	at testMain.main(testMain.java:50) [bin/:na]
145  [main] DEBUG - {}
145  [main] DEBUG - \hoge

Errors

バインディングを複数行っている場合。

パスをつなげているjarが多いので、不要なjarのパスを削除する。
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:<path>/logback-1.0.13/logback-classic-1.0.13.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:<path>/slf4j-1.7.5/slf4j-simple-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]

参照HP