#development #elixir #javascript #phoenix

When you are developing a Phoenix application, you might want to see the server logs in the browser console. This can be useful to debug issues that only happen in the server side.

There are two changes you need to make to your Phoenix application to achieve this:

  1. You need to enable the web_console_logger in your dev.exs configuration file:

    1config :my_app, MyAppWeb.Endpoint,
    2live_reload: [
    3+   web_console_logger: true,
    4    patterns: [
    5    ~r"priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$",
    6    ~r"priv/gettext/.*(po)$",
    7    ~r"lib/my_app_web/(controllers|live|components)/.*(ex|heex)$"
    8    ]
    9]
    
  2. Then in your assets/js/app.js enable the server logs when live reload is attached:

    1window.addEventListener("phx:live_reload:attached", ({detail: reloader}) => {
    2// Enable server log streaming to client.
    3// Disable with reloader.disableServerLogs()
    4reloader.enableServerLogs()
    5window.liveReloader = reloader
    6})
    

That's it! Happy hacking!