ipatch
ipatch

Reputation: 4032

How to log to file using logger_file_backend with the phoenix framework

I'm presently building out an API using the phoenix framework v1.3 rc1 and everything works as intended on my local dev environment, but when I pushed the changes to a production box to test the API the phx app is returning 500 Internal Server Error. I'm trying to setup logging for the dev / prod environments using logger_file_backend by following the instructions on the github page, but I'm not seeing any log files being generated in the dev or prod environments.

config.exs

# Configures Elixir's Logger
config :logger, :console,
  backends: [{LoggerFileBackend, :error_log}]
  # format: "$time $metadata[$level] $message\n",
  # metadata: [:request_id]

# configuration for the {LoggerFileBackend, :error_log} backend
config :logger, :error_log,
  path: "/home/deploy/deployments/kegcopr_api/error.log",
  level: :error

prod.exs

# Do not print debug messages in production
# config :logger, level: :info
config :logger, format: "[$level] $message\n",
  backends: [{LoggerFileBackend, :error_log}, :console]

config :logger, :error_log,
  path: "/home/deploy/deployments/kegcopr_api/error.log",
  level: :error

dev.exs

# Do not include metadata nor timestamps in development logs
config :logger, :console, format: "[$level] $message\n",
  backends: [{LoggerFileBackend, :error_log}, :console]

config :logger, :error_log,
  path: "/opt/elixir/kegcopr_api/log/error.log",
  level: :debug

Upvotes: 8

Views: 7123

Answers (3)

Sheshank Kodam
Sheshank Kodam

Reputation: 515

Solution

  1. Check logs in the right location. See Elixir Release does not output log to file for more information.
  2. In my case, I was running the command MIX_ENV=test mix release --env=prod and realised that the output config is not outputting to console or file. I updated the config/test.exs with following changes mentioned below and here

    config :logger, :console, format: "[$level] $message\n"

Upvotes: 0

Sheshank Kodam
Sheshank Kodam

Reputation: 515

How to change the format of the logging? I would like to add date before time in the log.

I have the added $date to the :format in the configuration but the date does not show up in logging.

# tell logger to load a LoggerFileBackend processes
config :logger,
    backends: [{LoggerFileBackend, :hutt}],
    format: "$date $time $metadata[$level] $message\n"

# configuration for the {LoggerFileBackend, :hutt} backend
config :logger, :hutt,
       path: "log/hutt.log",
       level: :info

iex(1)> require Logger
Logger
iex(2)> Logger.info("hello")
:ok
tail -f log/hutt.log
12:33:13.550 [info] Running HuttWeb.Endpoint with Cowboy using http://0.0.0.0:5000
12:36:28.669 [info] hello

ANSWER

Adding format to the stanza worked

config :logger, :hutt,
       format: "$date $time $metadata[$level] $message\n",
       path: "log/hutt.log",
       level: :info

Upvotes: 1

Steve Pallen
Steve Pallen

Reputation: 4507

Try this config:

config :logger, 
  backends: [:console, {LoggerFileBackend, :error_log}],
  format: "[$level] $message\n"

config :logger, :error_log, 
  path: "/tmp/info.log",
  level: :debug

Its working for me.

iex(1)> require Logger
Logger
iex(2)> Logger.debug "more here"
:ok
iex(3)>
21:39:58.608 [debug] more here

$ tail -f /tmp/info.log
21:34:29.756 [info] testing..
21:38:23.380 [debug] test me
21:39:58.608 [debug] more here

Upvotes: 11

Related Questions