0
0

More than 3 years have passed since last update.

Ruby on Jetsでターミナルにログを出力する方法

Posted at

ターミナルにLoggerを出力する方法

loggerが記載されている箇所を変更する。

config/application.rb
# config.logger = Jets::Logger.new($strerr)
config.logger = Jets::Logger.new($stdout)

loggerを出力する際はRuby on Railsのように、例えばlogger.debug ('hi')という記載をするのでは無く、Jets.logger.debug ('hi')のようにJetsを明言します。
これを記載しなければ、エラーが起こります。

posts_controller.rb
# GET /posts
  def index
    Jets.logger.debug ('log')
    p ('log')
    @posts = Post.all
    Jets.logger.debug (@posts.to_a)
  end

以下のようにログが出力されます。

app_1  | I, [2020-11-20T02:44:43.363645 #60]  INFO -- : Started GET "/posts" for 172.18.0.1 at 2020-11-20 02:44:43 +0000
app_1  | I, [2020-11-20T02:44:43.363994 #60]  INFO -- : Processing PostsController#index
app_1  | I, [2020-11-20T02:44:43.364474 #60]  INFO -- :   Event: { 省略 }
app_1  | I, [2020-11-20T02:44:43.364651 #60]  INFO -- :   Parameters: {}
app_1  | D, [2020-11-20T02:44:43.364999 #60] DEBUG -- : log
app_1  | "log"
app_1  | D, [2020-11-20T02:44:43.381540 #60] DEBUG -- : [#<Post id: 2, title: "create", created_at: "2020-11-20 01:39:31", updated_at: "2020-11-20 01:39:31">, #<Post id: 3, title: "test", created_at: "2020-11-20 01:43:45", updated_at: "2020-11-20 01:43:45">, #<Post id: 4, title: "post", created_at: "2020-11-20 01:43:53", updated_at: "2020-11-20 01:43:53">, #<Post id: 5, title: "new", created_at: "2020-11-20 02:31:57", updated_at: "2020-11-20 02:31:57">]

changelogを確認すると、1.5.2のリリースよりLoggerが利用できるようになったようです。

参考サイト

0
0
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
0
0