Created
April 29, 2015 19:57
-
-
Save wildart/0359fc4a880430e20baf to your computer and use it in GitHub Desktop.
Configurable loggers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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