4
4

More than 5 years have passed since last update.

Celluloid::Loggerのerrorがthorのerrorに負ける

Last updated at Posted at 2015-03-25

celluloidを使う機会が出たので、ついでにCelluloid::Loggerを使ってみたところ、Celluloid.logger.errorがthorのerrorに優先されてしまい期待通りの動きをしてくれなかった。

期待通り動かない場合

  • 以下を実行するとsample.logにerrorが出力されずにstdoutに出力される
require 'thor'
require 'celluloid'
include Celluloid::Logger
Celluloid.logger = ::Logger.new("sample.log")

class SampleThor < Thor
  desc "test", "sample"
  def test
    error "test"
  end
end

SampleThor.start(ARGV)

それならばThor::Shell::Basicをoverrideしてしまえ

  • Thor::Shell::Basic.errorをCelluloid.logger.errorにしてしまう
Thor::Shell::Basic.class_eval do
  def error(statement)
    Celluloid.logger.error(statement) if Celluloid.logger
  end
end
  • 上のサンプルを書き換えると期待通りsample.logにエラーが出力される
    debug,info,warnはCelluloid::Loggerのものがそのまま使える
require 'thor'
require 'celluloid'
include Celluloid::Logger
Celluloid.logger = ::Logger.new("sample.log")

Thor::Shell::Basic.class_eval do
  def error(statement)
    Celluloid.logger.error(statement) if Celluloid.logger
  end
end

class SampleThor < Thor
  desc "test", "sample"
  def test
    error "test"
  end
end

SampleThor.start(ARGV)
4
4
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
4