This repository was archived by the owner on Feb 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathlog_facade.rb
55 lines (50 loc) · 1.77 KB
/
log_facade.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
module Autumn
# This class is a facade for Ruby's `Logger` that adds additional information
# to log entries. LogFacade will pass any method calls onto a Logger instance,
# but reformat log entries to include an Autumn object's type and name.
#
# For example, if you wanted a LogFacade for a Leaf named "Scorekeeper", you
# could instantiate one:
#
# ```` ruby
# facade = LogFacade.new(logger, 'Leaf', 'Scorekeeper')
# ````
#
# And a call such as:
#
# ```` ruby
# facade.info "Starting up"
# ````
#
# Would be reformatted as "Scorekeeper (Leaf): Starting up".
#
# In addition, this class will log messages to `STDOUT` if the `debug` global
# option is set. Instantiation of this class is handled by {Genesis} and
# should not normally be done by the user.
class LogFacade
# @return [String] The Autumn object type (typically "Stem" or "Leaf").
attr :type
# @return [String] The name of the Autumn object.
attr :name
# Creates a new facade for `logger` that prepends type and name information
# to each log message.
#
# @param [Logger] logger A logger instance.
# @param [String] type The Autumn object type (e.g., "Stem" or "Leaf").
# @param [String] name The Autumn object name (leaf or stem name).
def initialize(logger, type, name)
@type = type
@name = name
@logger = logger
@stdout = Speciator.instance.season(:logging) == 'debug'
end
# @private
def method_missing(meth, *args)
if args.size == 1 && args.only.kind_of?(String)
args = ["#{name} (#{type}): #{args.only}"]
end
@logger.send meth, *args
puts (args.first.kind_of?(Exception) ? (args.first.to_s + "\n" + args.first.backtrace.join("\n")) : args.first) if @stdout
end
end
end