Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 310
ipatch Avatar asked May 12 '17 01:05

ipatch


2 Answers

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
like image 151
Steve Pallen Avatar answered Nov 12 '22 22:11

Steve Pallen


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
like image 29
Sheshank Kodam Avatar answered Nov 12 '22 21:11

Sheshank Kodam