midori-rb / midori.rb

Lightweight, Flexible and Fast Ruby Web Framework
MIT License
870 stars 57 forks source link

Can't logging to STDOUT properly #150

Closed li-thy-um closed 6 years ago

li-thy-um commented 6 years ago

Using master branch Deploy on Aliyun swarm mode cluster with two containers on two different machines. When I stop the container, it will output all the missing logs.

Empty output when container is alive.

xxxx-node8:~# docker logs -f container

When I stop the container, it will puts all logs at once.

xxxx-node8:~# docker logs -f container
I, [2018-01-23T09:39:11.374340 #1]  INFO -- : Midori 0.5.0 is now running on 0.0.0.0:3000
I, [2018-01-23T09:39:47.184221 #1]  INFO -- : 10.255.0.34 - - "GET / HTTP/1.1" 200 OK 0.000187
I, [2018-01-23T09:39:49.306820 #1]  INFO -- : 10.255.0.34 - - "GET / HTTP/1.1" 200 OK 0.000080
I, [2018-01-23T09:39:56.277032 #1]  INFO -- : 10.255.0.47 - - "GET / HTTP/1.1" 200 OK 0.000073
I, [2018-01-23T09:39:59.395120 #1]  INFO -- : 10.255.0.47 - - "GET / HTTP/1.1" 200 OK 0.000076

My Dockerfile looks like this:

FROM ruby:2.4.1-alpine

WORKDIR /app

RUN echo "http://mirrors.aliyun.com/alpine/v3.4/main/" | tee /etc/apk/repositories

ADD ./Rakefile ./Rakefile
ADD ./config.ru ./config.ru
ADD Gemfile Gemfile.lock ./

RUN \
  apk update && apk add --no-cache git build-base && \
  bundle install --deployment --without development:test:doc && \
  apk del git build-base

ADD ./lib ./lib
ADD ./config ./config
ADD ./app ./app

NOTE: I double checked, logging is improper on both containers.

dsh0416 commented 6 years ago

This is a common docker use question. https://github.com/moby/moby/issues/1385 https://github.com/moby/moby/issues/18327

https://github.com/docker/compose/issues/1549

You may fix it through https://stackoverflow.com/questions/36417888/log-issue-in-rails4-with-docker-running-rake-task

It's also recommended to use log file instead of STDOUT in docker.

Midori::Configure.log = Logger.new("file location")

li-thy-um commented 6 years ago

@dsh0416 but why it will puts logs sync with midori.rb v0.5.0?

dsh0416 commented 6 years ago

@li-thy-um Nope, STDOUT behaves asynchronously by default. But ruby could not detect the tty buffer size inside docker, so it may not work properly. You may need to set the log to sync which would force ruby to flush every time it have something to print.