6
votes

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
3
If you run the app in console mode on the production server, you will see the error message. For example, if your using exrm or distillery, run it as bin/my_app console.Steve Pallen
@StevePallen thanks for the suggestion, but I'm deploying this app using gatling which apparently does not play nice with the command you suggested.ipatch
How are you calling Logger.debug in your code?Steve Pallen
I've never used gatling, but I don't know how anyone can work with a production server without being able to run the app in console mode. This is the first thing I do when I'm have problems with a production app.Steve Pallen
I just looked at the gatling docs. It's just a set of tools to create a release with Distillery and release it. ssh into your box and got to the root directory of your app. then run bin/my_app stop and then bin/my_app console. You should then see the error messages in the console.Steve Pallen

3 Answers

10
votes

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
1
votes

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

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"