sadcad

sadcad

How do you capture logs and write them to `stderr`?

I have the following line Code.eval_string(code)
which gives me the result of the inputted code and I write it to stdout

I want to capture any logs as well and write them to stderr but I’m not sure how to do that ? is it by using ExUnit ?

I am aware of the security implications

Most Liked

josevalim

josevalim

Creator of Elixir

Here is a way to do it. Keep in mind it shouldn’t’ be used in actual systems, as there is a race condition here since you won’t have a stderr registered for a brief second. It should be fine for scripts like yours though:

iex(1)> Process.unregister(:standard_error)
true
iex(2)> {:ok, device} = StringIO.open("")
{:ok, #PID<0.109.0>}
iex(3)> Process.register(device, :standard_error)
true
iex(4)> IO.warn "nothing will show"
:ok
josevalim

josevalim

Creator of Elixir

Yes, nothing is written to stderr because you have replaced the stderr device by one that captures it. You can do the reverse work and register the :standard_error back to the original process. I will leave this as an exercise. :slight_smile:

lackac

lackac

@sadcad after reading this thread two times it seems to me that all information you need has been included here already.

One last piece of the puzzle that you might be missing still is that you can save the original device registered as :standard_error and use it later to either write to it directly or reregister it once you’re done with the eval and write the captured output after. Something like:

iex(2)> original_stderr = Process.whereis(:standard_error)
#PID<0.59.0>
iex(3)> Process.unregister(:standard_error)
true
iex(4)> {:ok, dev} = StringIO.open("")
{:ok, #PID<0.184.0>}
iex(5)> Process.register(dev, :standard_error)
true
iex(6)> IO.puts :stderr, "Oops!"
:ok
iex(7)> captured = StringIO.flush(dev)
"Oops!\n"
iex(8)> IO.write(original_stderr, captured)
Oops!
:ok

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement