4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

java jul2logback

Last updated at Posted at 2016-03-06

目的

何番煎じかわからないけど、自分用メモ
「java.util.logging」から「logback」に出力する設定

設定

maven3設定

pom.xml

<project
  xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
  <modelVersion>4.0.0</modelVersion>
  <groupId>foo</groupId>
  <artifactId>bar</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <properties>
    <slf4j.version>1.7.18</slf4j.version>
    <logback.version>1.1.6</logback.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>jul-to-slf4j</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>${logback.version}</version>
    </dependency>
  </dependencies>
</project>

logback設定(コンソールのみ)

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

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

サンプル

コード

package sample;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.logging.LogManager;
import java.util.logging.Logger;

import org.slf4j.bridge.SLF4JBridgeHandler;

public class Jul2slf4jSample {

	public void execute() {

		// change jul loglevel(once).
		StringBuilder lss = new StringBuilder();
		lss.append(".level=ALL\n");
		try (ByteArrayInputStream in = new ByteArrayInputStream(lss.toString().getBytes())) {
			LogManager.getLogManager().readConfiguration(in);
		} catch (IOException e) {
			// ByteArrayInputStream#close does not throw IOException.
			;
		}

		// slf4j initialize(once).
		// If Default logging.properties use ,This is necessary.
		// SLF4JBridgeHandler.removeHandlersForRootLogger();
		if(!SLF4JBridgeHandler.isInstalled()){
			SLF4JBridgeHandler.install();
		}

		// log output.
		Logger log = Logger.getLogger(this.getClass().getName());

		log.severe("severe");
		log.warning("warning");
		log.info("info");
		log.config("config");
		log.fine("fine");
		log.finer("finer");
		log.finest("finest");

	}

}

出力

2016-03-07 13:16:44.972 [main] ERROR sample.Jul2slf4jSample - severe
2016-03-07 13:16:44.975 [main] WARN  sample.Jul2slf4jSample - warning
2016-03-07 13:16:44.975 [main] INFO  sample.Jul2slf4jSample - info
2016-03-07 13:16:44.975 [main] INFO  sample.Jul2slf4jSample - config
2016-03-07 13:16:44.976 [main] DEBUG sample.Jul2slf4jSample - fine
2016-03-07 13:16:44.976 [main] DEBUG sample.Jul2slf4jSample - finer

ecilpse用コードテンプレート

XMLファイル保存し、Preferences->Editor->Templateでインポートする。
「jul」と入力し自動補完すると、ログ出力定型文が挿入される。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE templates>
<templates>
  <template autoinsert="false" context="java" deleted="false" description="jul log standard" enabled="true" name="jul">private static final Logger log =
    Logger.getLogger(${enclosing_type}.class.getName());&#13;
    ${imp:import(java.util.logging.Logger)}</template>
</templates>
4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?