Alex Craft
Alex Craft

Reputation: 15336

Meaningful auto-generated name for PID for debug purposes?

There's some process started with Task.Supervisor.start_child, if something went wrong Elixir (Erlang) prints error message like this:

23:56:06.257 [error] Task #PID<0.216.0> started from #PID<0.137.0> terminating

It's hard to understand what process crashed. I wonder if there's a way to give a meaningful auto-generated names to processes? So the error will be more descriptive, like:

23:56:06.257 [error] Task #PID-REQUEST-HANDLER<0.216.0> started from #PID-SOCKET-LOOP<0.137.0> terminating

Upvotes: 2

Views: 68

Answers (1)

Hamidreza Soleimani
Hamidreza Soleimani

Reputation: 2544

You can name each desired process with register/2 function:

Erlang:

register(RegName, PidOrPort) -> true

Elixir:

register(pid | port, atom) :: true

Then what you need is implementing your logger to use process_info(PID, registered_name) function to get the registered name of the desired process by its PID and formatting the log text with that name.

Update: Also it is good to know that error_logger is an event manager (gen_event). Error, warning, crash, progress and info events are sent to the error logger from the Erlang runtime system and the different Erlang/OTP applications. It has some default event handlers, as well as sasl which is an OTP application that adds more logging features to your application. You can also write your own event handler and add it to error_logger event manager.

Upvotes: 1

Related Questions