Understanding Logging in Julia

Hi, guys.
I am trying to understand how to use Julia Logging system. For that I am now reading through the julia docs and the corresponding source code.

I have a question about the implementation of SimpleLogger (see logger.jl). In the function handle_message, instead of writing all the messages directly to IOStream, the messages are first written into an additional IOBuffer and then the contents of the IOBuffer are copied to the IOStream.

What are the benefits of such design?

To do I/O you need to call the Operating System and this Syscall have some overhead, this is what write does on a IOStream (actually it call the C function that in turn call the OS).

Buffers are often used to group multiple I/O operations in a single Syscall to reduce this overhead.

The idea is to accumulate the data you want to write in memory (the IOBuffer) and, when there are enough data to write, transfer this chunk of data to the IOStream.

1 Like