LokiLogger.Logger(server::Union{String,URI}; labels::Dict)
LokiLogger.Logger(fmt::Function, server::Union{String,URI}; labels::Dict)

Create a logger that send messages to a Loki server.

The log messages are attributed with the labels given in the labels dictionary. If no labels are specified the default labels are:

  • "host" => gethostname()
  • "app" => "LokiLogger.jl"

The fmt argument is used for formatting. There are two builtin formatting functions:

  • LokiLogger.logfmt (default): formats the log message in the logfmt format,
  • LokiLogger.json: formats the log message as JSON.

Custom functions must take two arguments: an io::IO to write the message to, and args::NamedTuple that contains all the logger arguments, see help for LoggingExtras.handle_message_args for details.


# Create a logger with a single label and default (logfmt) formatting
logger = LokiLogger.Logger("http://localhost:3100"; labels = Dict("app" => "myapp"))

# Create a logger with json output formatting
logger = LokiLogger.Logger(LokiLogger.json, "http://localhost:3100")

# Create a logger with custom formatting
logger = LokiLogger.Logger("http://localhost:3100") do io, args
    # Only output the level and the message
    print(io, args.level, ": ", args.message)
json(io::IO, args)

Format the log message as JSON and write to io.

Example logline:

{"level":"info","msg":"hello, world","module":"Main","file":"/run.jl","line":2,"group":"run","id":"Main_6972c827"}
logfmt(io::IO, args)

Format the log message in logfmt key-value format and print to io.

Example logline:

level=info msg="hello, world" module=Main file="/run.jl" line=2 group=run id=Main_6972c827