Skip to content

Commit

Permalink
Docs for logging: write to file (#28927)
Browse files Browse the repository at this point in the history
* write log to file

* Use julia-repl for highlighting, fix indentation

(cherry picked from commit 2e603aa)
  • Loading branch information
Jan Magnusson authored and KristofferC committed Sep 8, 2018
1 parent 718b560 commit d610b84
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions stdlib/Logging/docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,43 @@ Similarly, the environment variable can be used to enable debug logging of
modules, such as `Pkg`, or module roots (see [`Base.moduleroot`](@ref)). To
enable all debug logging, use the special value `all`.

## Writing log events to a file

Sometimes it can be useful to write log events to a file. Here is an example
of how to use a task-local and global logger to write information to a text
file:

```julia-repl
# Load the logging module
julia> using Logging
# Open a textfile for writing
julia> io = open("log.txt", "w+")
IOStream(<file log.txt>)
# Create a simple logger
julia> logger = SimpleLogger(io)
SimpleLogger(IOStream(<file log.txt>), Info, Dict{Any,Int64}())
# Log a task-specific message
julia> with_logger(logger) do
@info("a context specific log message")
end
# Write all buffered messages to the file
julia> flush(io)
# Set the global logger to logger
julia> global_logger(logger)
SimpleLogger(IOStream(<file log.txt>), Info, Dict{Any,Int64}())
# This message will now also be written to the file
julia> @info("a global log message")
# Close the file
julia> close(io)
```


## Reference

Expand Down

0 comments on commit d610b84

Please sign in to comment.