Skip to content

Instantly share code, notes, and snippets.

@wildart
Created April 29, 2015 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wildart/0359fc4a880430e20baf to your computer and use it in GitHub Desktop.
Save wildart/0359fc4a880430e20baf to your computer and use it in GitHub Desktop.
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