Logging inside Docker, NewRelic gem and STDOUT
By default newrelic_rpm gem logs to a file located in log/newrelic_agent.log. This is an issue for an app running inside a container, because the log file will be in a local /log folder within the container. This means the log will not be rotated, and will not be kept if the container is recreated. Access to the log is also cumbersome as it requires accessing the running container.
One workaround to keep logging to a file is using a mounted volume, you mount a host volume in your /app/log container folder, in order to persist log files and access them directly from the host.
However the best practice is using logging drivers, in this way Docker will manage whatever your applications log to STDOUT/STDERR. In my case we have Mesosphere DCOS configured to send application’s STDOUT/STDERR to ELK via journald logging driver.
So, I decided to change the app to log to STDOUT/STDERR, in Sinatra thats pretty simple, you just do:
And then I started to read the NewRelic ruby agent documentation to find a way to configure the agent to log to STDOUT, however I couldn’t find any reference about that in the docs. Then I jumped into the NewRelic rpm source code and found this:
So, the solution is just change the logging section in the config/newrelic.yml file to: