SeqLoggers.jl
SeqLoggers.jl
is a tool for sending log events to a Seq
log server using the Julia
Programming language.
SeqLoggers.jl
extends the AbstractLogger
interface to create log events using the macros:
@debug
,@info
,@warn
and@error
.
Log events are subsequently posted to the Seq
log server using HTTP.jl
and the Seq
raw-event API.
Additionally, features from LoggingExtras.jl
are used to provide more complex logger types.
⚠️ Coping-pasting the examples on this page might introduce invisible extra characters that cannot be handled by theSeq
server. When in doubt, replicate the examples without copying the log event strings.
Install Seq
The Seq
software is avabilable for free for development purposes or single-user deployment (Installation instructions).
Logging in julia
Using the Logging
module, log events are created by inserting a logging statement into the source code using the macros @debug
, @info
, @warn
and @error
.
@info "Log Event with `Information` level"
The currently active global logger can be obtained by running
using Logging
global_logger() # ConsoleLogger(...)
As default, a ConsoleLogger
is provided, which prints the logging event directly to the Julia
REPL.
The global logger can be set to any logger newLogger<:AbstractLogger
by calling global_logger(newLogger)
.
Alternatively, a code section can be wrapped inside a with_logger
do
-block to use a specific logger for the execution of the code contained in the do
-block.
Logging.with_logger(newLogger) do
...
end
Within the scope of the do
-block, the active logger can be obtained by calling current_logger()
.
SeqLoggers.jl
SeqLoggers.jl
provides a new logger type SeqLogger<:AbstractLogger
to replace the default logger to enable the user to post log events to a Seq
log server.
Basics
A SeqLogger
is constructed by calling the constructor with the same name.
using SeqLoggers
seqLogger = SeqLogger("http://localhost:5341"; # `Seq` server url
minLevel=Logging.Info, # define minimal level for log events
apiKey="", # api-key for registered Apps
batchSize=1,
App="Trialrun", # additional log event properties
Env="UAT")
The resulting logger seqLogger
posts each log event separately to the Seq
server with url "http://localhost:5341"
.
If the performance overhead from posting the log events separately is to high, log events can be stored and posted in a batch. The constructor keyword argument batchSize
defines the size of a log event batch. Once the logger has received a number of log events equal to batchSize
, all events are sent to the Seq
log server in one post. By default, batchSize=10
.
Therefore, for proper functionality with batchSize>1
, it is required to use the SeqLogger
by calling with_logger
(and not add it as a global logger) to ensure that all log events will be sent to the log server.
Logging.with_logger(seqLogger) do
@info "Log me into `Seq` with property user = {user}" user="Me"
end
In this example, besides the global log event properties App="Trialrun"
and Env="Test"
also a local log event property user="Me"
was added.
Note, that all elements surrounded by curly brackets, e.g. {user}
, will be replaced (on the server-side) by the corresponding log event property if it exists.
LoggingExtras.jl
Interaction with SeqLogger
s can also be combined with the functionality of LoggingExtras.jl
.
using LoggingExtras
combinedLogger = TeeLogger(Logging.current_logger(), seqLogger)
In this example, the combinedLogger
logs both to the Julia
REPL (if the current logger was a ConsoleLogger
) and the Seq
log server defined by seqLogger
.
SeqLoggers.PostType
Explanation of In the SeqLogger
constructor, there is an second argument postType
.
For most cases, one can ignore this argument and use the default value Serial()
.
However, if the performance of the SeqLogger
is not satisfying, it might pay off to experiment with the different settings.
Serial():
the log event are posted without any multi-threading.Parallel():
the log event are posted usingThreads.@spawn
which allows to use multi-threading.Background(nWorkers):
the log event are posted usingWorkerUtilities.@spawn
which allows to use multi-threading and run the post action as a true background task wherenWorkers
is the amount of background workers used.
FAQ
- The default
Seq
log server can be accessed on http://localhost:5341.