Skip to content

Instantly share code, notes, and snippets.

@wildart

wildart/loggers.jl

Created Apr 29, 2015
Embed
What would you like to do?
Configurable loggers
module Logging
abstract Logger
type DefaultLogger <: Logger
stream::IO
color::Symbol
DefaultLogger() = new(STDERR, :blue)
DefaultLogger(color::Symbol) = new(STDERR, color)
end
fatal(l::Logger, msg) = print_with_color(l.color, l.stream, "FATAL:", chomp(string(msg)), "\n")
function fatal(msg...)
m = current_module()
l = if isconst(m, :logger) && isa(eval(:($m.logger)), Logger)
eval(:($m.logger))
else
warn("Logger is not configured. Using default configuration.")
Logging.DefaultLogger()
end
fatal(l, msg...)
end
export fatal, Logger
end
module A
using Logging
fatal("aaa")
end
module B
using Logging
const logger = Logging.DefaultLogger(:red)
fatal("bbb")
fatal(Logging.DefaultLogger(:yellow), "bbb")
end
module C
using Logging
const logger = 1
fatal("aaa")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment