Configure log directory for Phoenix app

Hello!
I’m deploying my Phoenix app using mix_systemd package, to run it as systemd unit file. The app’s runtime is in /srv/appname/current,

Problem: The app tries to create log files under /srv/appname/current/log/error.log and /srv/appname/current/console.log. I would like to configure it to create the log files under /var/log/appname.

I have no idea where this file creation comes from. My configuration does not contain logging to files:

 Application.get_env :logger, :backends
[:console, Sentry.LoggerBackend]

Despite this, I’m getting these errors and the process crashes:

Jul 22 17:18:50 steinem proca[102362]: 17:18:50.230 [error] Failed to open log file log/error.log with error permission denied
Jul 22 17:18:50 steinem proca[102362]: 17:18:50.231 [error] Failed to open log file log/console.log with error permission denied

I do not know which elixir module emits these errors (no traceback). Is this some “standard” behaviour for releases? How can I control it?

Starting a release as daemon will make it write logs to disk by default (as nobody is watching stdout). Your path doesn’t match that default though.

1 Like

It also happens when i just run mix phx.server in development mode. When i make the “log” dir unwritable (chmod 0 log) I get:

17:30:46.558 [error] Failed to open log file log/console.log with error permission denied
17:30:46.558 [error] Failed to open log file log/error.log with error permission denied

How can I figure out what is producing these messages? Too bad I can’t grep elixir binaries!

… oh actually I can!
I spot lager library:

deps/lager/src/lager_file_backend.erl
131:                    ?INT_LOG(error, "Failed to open log file ~ts with error ~s", [Name, file:format_error(Reason)]),
692:                "Failed to open log file " ++ TestLog ++ " with error permission denied",
739:                "Failed to open log file " ++ TestLog ++ " with error permission denied",

lets investigate…

So lager is an erlang logger library pulled in by rabbitmq client package.

It needs to be configured in one of compiled config files, so config.exs or prod.exs for instance:

config :lager,
  log_root: '/var/log/appname'
3 Likes

I’d also look if you actually need lager. It’s been replaced in recent versions by erlangs :logger and the integration of it into elixir.

1 Like

In the case of @marcin it is dependency of dependency (RabbitMQ). Hopefully in future lager will also be backed by logger so there will be only one place to configure logging (but we still will need to find a way to configure logger in development, but that is discussion for some other time).

1 Like