如何进行“检查档案” (还是弦)在长生不老药中?
在Elixir中,我们可以io.impect Anystructure
获取Anystructure
的内部标准打印为输出。是否有类似的方法将其输出到文件(或作为更灵活的解决方案,将其输出到字符串)?
我已经在调试://elixir-lang.org/getting-started/io-and-the-file-system.html“ rel =“ nofollow noreferrer”> io ,但看不到解决方案。我也尝试过
{:ok, file} = File.open("test.log", [:append, {:delayed_write, 100, 20}])
structure = %{ a: 1, b: 2 }
IO.binwrite(file, structure)
File.close file
,但是结果
no function clause matching in IO.binwrite/2 [...]
def binwrite(device, iodata) when is_list(iodata) or is_binary(iodata)
我还谷歌搜索了一些“ elixir序列化”和“ elixir objoct to String”,但还没有找到任何有用的东西(例如:erlang.term_ter_to_binary
返回,好吧,二进制)。是否有一种简单的方法可以获得相同的结果,即io.inspect
在文件或字符串中打印?
In Elixir, we can IO.inspect anyStructure
to get anyStructure
's internals printed to output. Is there a similar method to output it to a file (or, as a more flexible solution, to a string)?
I've looked through some articles on debugging and io but don't see a solution. I've also tried
{:ok, file} = File.open("test.log", [:append, {:delayed_write, 100, 20}])
structure = %{ a: 1, b: 2 }
IO.binwrite(file, structure)
File.close file
but that results in
no function clause matching in IO.binwrite/2 [...]
def binwrite(device, iodata) when is_list(iodata) or is_binary(iodata)
I’ve also googled some "elixir serialize" and "elixir object to string", but haven't found anything useful (like :erlang.term_to_binary
which returns, well, binary). Is there a simple way to get the same result that IO.inspect
prints, into a file or a string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
已经有
Inspect/2
function(不是与
io.Inspect
)相同,只需使用它:您可以随后使用字符串做任何想做的事情。
There is already
inspect/2
function (not the same asIO.inspect
), just go with it:You can do whatever you wish with the string afterwards.
您可以给出
io.Inspect
一个附加参数,可以告诉它在哪里写入:它接受写入的过程的
pid
。Stringio
提供了这样一个过程,在关闭时返回字符串。You can give
IO.inspect
an additional param to tell it where to write to:It accepts a
pid
of a process to write to.StringIO
provides such a process, returning you a string on close.这不是真的。
io.inspect
使用 Inspect 协议。您看到的不是结构的内部,而是编写用于生成检查协议的结构实现。您可以提供不同的选项,以进行检查,以becess> Inspect.opts
,其中之一是structs:false
,它将以地图打印结构。例如,检查范围结构:
要回答您的问题并添加其他答案,这是一种使用
file.open!/3
重复使用打开文件并将多个检查调用记录到同一文件,然后关闭文件:这将产生以下
test.log
文件:This is not quite true;
IO.inspect
uses the Inspect protocol. What you see is not the internals of the struct, but whatever that struct's implementation of the Inspect protocol is written to produce. There are different options you can give to inspect, defined inInspect.Opts
, one of them isstructs: false
, which will print structs as maps.For example, inspecting a range struct:
To answer your question and to add to the other answers, here is a method that uses
File.open!/3
to reuse an open file and log multiple inspect calls to the same file, then close the file:This produces the following
test.log
file:您只需要组合返回binary 和 或任何其他函数转储到文件。
请注意
limit :: infinity
在检查到stdout
时,长结构将被截断,以获得更好的可读性。You simply need to combine
inspect/2
which returns a binary andFile.write/3
or any other function dumping to a file.Note the
limit: :infinity
option, without it the long structures will be truncated for better readability when inspecting tostdout
.