Help us understand the problem. What is going on with this article?

Groovyでログを出す方法まとめ

More than 3 years have passed since last update.

XML設定ファイルを使う方法

設定ファイルの配置先は次の通り

  • 単なるスクリプトの設定は次のいずれか
    • クラスパス通っている場所
    • カレントディレクトリ
  • Gradleでビルドする場合 src/main/resources/logback.xml

参考:hishidamaさんのslf4jメモ http://www.ne.jp/asahi/hishidama/home/tech/java/slf4j.html

logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="INFO">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>
log_xml.groovy
@Grab("org.slf4j:slf4j-api:1.7.21")
@Grab("ch.qos.logback:logback-classic:1.1.7")

import org.slf4j.Logger
import org.slf4j.LoggerFactory

def log = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)

log.error "error log"
log.warn "warn log"
log.info "info log"
log.debug "debug log"
log.trace "trace log"

この例ではXMLファイルでINFOレベルに設定している。

output
2016-12-01 02:13:08.186 [main] ERROR ROOT - error log
2016-12-01 02:13:08.188 [main] WARN  ROOT - warn log
2016-12-01 02:13:08.189 [main] INFO  ROOT - info log

XML設定ファイルを省略する方法

デフォルトだとDEBUGレベルになる

log_no_xml.groovy
@Grab("org.slf4j:slf4j-api:1.7.21")
@Grab("ch.qos.logback:logback-classic:1.1.7")

import org.slf4j.Logger
import org.slf4j.LoggerFactory

def log = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)

log.error "error log"
log.warn "warn log"
log.info "info log"
log.debug "debug log"
log.trace "trace log"
output
02:16:10.956 [main] ERROR ROOT - error log
02:16:10.962 [main] WARN ROOT - warn log
02:16:10.962 [main] INFO ROOT - info log
02:16:10.962 [main] DEBUG ROOT - debug log

ログレベルを変更する方法

スクリプトならこれがおすすめ。

参考:XMLを書かずにGroovyからLogbackのログ出力をINFOレベル以上に限定する

log_no_xml.groovy
@Grab("org.slf4j:slf4j-api:1.7.21")
@Grab("ch.qos.logback:logback-classic:1.1.7")

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import ch.qos.logback.classic.Level

LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME).level = Level.INFO

def log = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)

log.error "error log"
log.warn "warn log"
log.info "info log"
log.debug "debug log"
log.trace "trace log"
output
02:16:10.956 [main] ERROR ROOT - error log
02:16:10.962 [main] WARN ROOT - warn log
02:16:10.962 [main] INFO ROOT - info log
02:16:10.962 [main] DEBUG ROOT - debug log

アノテーション使う方法

クラスに対して付ける

参考:http://www.canoo.com/blog/2010/09/20/log-groovys-new-and-extensible-logging-conveniences/
   http://aoking.hatenablog.jp/entry/2013/07/31/122305

log_annotation.groovy
@Grab("org.slf4j:slf4j-api:1.7.21")
@Grab("ch.qos.logback:logback-classic:1.1.3")

import groovy.util.logging.Slf4j

@Slf4j
class PrintLog {
  def display() {
    log.error "error log"
    log.warn "warn log"
    log.info "info log"
    log.debug "debug log"
    log.trace "trace log"
  }
}

def printLog = new PrintLog()
printLog.display()
output
10:26:40.126 [main] ERROR PrintLog - error log
10:26:40.136 [main] WARN  PrintLog - warn log
10:26:40.137 [main] INFO  PrintLog - info log
10:26:40.138 [main] DEBUG PrintLog - debug log

ログレベル変更(その1)

ファイル毎にログレベルを設定したい場合。

log_level_annotation.groovy
@Grab("org.slf4j:slf4j-api:1.7.21")
@Grab("ch.qos.logback:logback-classic:1.1.3")

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import groovy.util.logging.Slf4j
import ch.qos.logback.classic.Level

@Slf4j
class PrintLog {

  def display() {
    log.error "error log"
    log.warn "warn log"
    log.info "info log"
    log.debug "debug log"
    log.trace "trace log"
  }
}

LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME).level = Level.INFO

def printLog = new PrintLog()
printLog.display()
output
10:35:17.365 [main] ERROR PrintLog - error log
10:35:17.368 [main] WARN  PrintLog - warn log
10:35:17.368 [main] INFO  PrintLog - info log

ログレベル変更(その2)

メソッド毎にログレベルを設定したい場合(実際にはなさそう)

log_level_annotation.groovy
@Grab("org.slf4j:slf4j-api:1.7.21")
@Grab("ch.qos.logback:logback-classic:1.1.3")

import groovy.util.logging.Slf4j
import ch.qos.logback.classic.Level

@Slf4j
class PrintLog {

  def display() {
    log.level = Level.INFO

    log.error "error log"
    log.warn "warn log"
    log.info "info log"
    log.debug "debug log"
    log.trace "trace log"
  }
}

def printLog = new PrintLog()
printLog.display()
output
10:32:09.722 [main] ERROR PrintLog - error log
10:32:09.728 [main] WARN  PrintLog - warn log
10:32:09.728 [main] INFO  PrintLog - info log
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした