2
1

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.

クラシックスタイルでSinatraのログを手軽に出すメモ

Last updated at Posted at 2016-11-04

自分用です。プロトタイプ的なものをSinatraで作る機会が多いんだけど、いつもログ周りで一瞬引っかかるのでメモしておきます。なぜクラシックスタイルなのかといえば、普段手癖で小さなAPIを書くときにはだいたいクラシックスタイルで書くからです。

configure の中にまとめるのは趣味です。

config.ru
require './app.rb'

configure {
    Dir.mkdir("log") unless File.exist?("log")

    access_log = File.new("#{settings.root}/log/access.log","a+")
    access_log.sync = true
    use Rack::CommonLogger, access_log

    error_log = File.new("#{settings.root}/log/error.log","a+")
    error_log.sync = true

    app_log = File.new("#{settings.root}/log/application.log","a+")
    app_log.sync = true
    app_logger = Logger.new(app_log)

    before {
        env["rack.errors"] = error_log
        env["rack.logger"] = app_logger
    }
}

run Sinatra::Application

rack.errors は「出力先」なのに対し、rack.loggerは「利用するロガー」である点がややこしいところです。まあそれを言ったら「アクセスログを出すとかエラー出力のキャプチャはSinatra側というよりHTTPサーバ側の都合だよね」的なものとかあれこれあるわけなんですが。

参考

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?