Class: Dockly::Util::Logger
- Inherits:
-
Object
- Object
- Dockly::Util::Logger
- Extended by:
- Mixin
- Defined in:
- lib/dockly/util/logger.rb
Defined Under Namespace
Modules: Mixin
Constant Summary collapse
- LEVELS =
[:debug, :info, :warn, :error, :fatal, :unknown].freeze
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#output ⇒ Object
Returns the value of attribute output.
-
#prefix ⇒ Object
Returns the value of attribute prefix.
-
#print_method ⇒ Object
Returns the value of attribute print_method.
Class Method Summary collapse
Instance Method Summary collapse
- #format_level(level) ⇒ Object
- #format_message(level, message) ⇒ Object
- #get_last_method ⇒ Object
-
#initialize(prefix = "", output = Dockly::Util::Logger.output, print_method = Dockly::Util::Logger.print_method) ⇒ Logger
constructor
A new instance of Logger.
- #log(level, message) ⇒ Object
-
#print_method? ⇒ Object
Returns the value of attribute print_method.
- #with_prefix(new_prefix = "", output = nil, print_method = nil) {|self.class.new([prefix, new_prefix].compact.reject(&:empty?).join(' '), output, print_method)| ... } ⇒ Object
Methods included from Mixin
Methods included from Delegate
Constructor Details
#initialize(prefix = "", output = Dockly::Util::Logger.output, print_method = Dockly::Util::Logger.print_method) ⇒ Logger
Returns a new instance of Logger.
7 8 9 10 11 |
# File 'lib/dockly/util/logger.rb', line 7 def initialize(prefix = "", output = Dockly::Util::Logger.output, print_method = Dockly::Util::Logger.print_method) @prefix = prefix @print_method = print_method @output = output end |
Class Attribute Details
.output ⇒ Object
95 96 97 |
# File 'lib/dockly/util/logger.rb', line 95 def output @output ||= STDOUT end |
.print_method ⇒ Object
91 92 93 |
# File 'lib/dockly/util/logger.rb', line 91 def print_method (@print_method == false) ? false : true end |
Instance Attribute Details
#output ⇒ Object
Returns the value of attribute output.
2 3 4 |
# File 'lib/dockly/util/logger.rb', line 2 def output @output end |
#prefix ⇒ Object
Returns the value of attribute prefix.
2 3 4 |
# File 'lib/dockly/util/logger.rb', line 2 def prefix @prefix end |
#print_method ⇒ Object
Returns the value of attribute print_method.
2 3 4 |
# File 'lib/dockly/util/logger.rb', line 2 def print_method @print_method end |
Class Method Details
.disable! ⇒ Object
78 79 80 |
# File 'lib/dockly/util/logger.rb', line 78 def disable! @logger_enabled = false end |
.enable! ⇒ Object
82 83 84 |
# File 'lib/dockly/util/logger.rb', line 82 def enable! @logger_enabled = true end |
.enabled? ⇒ Boolean
86 87 88 89 |
# File 'lib/dockly/util/logger.rb', line 86 def enabled? enable! if @logger_enabled.nil? @logger_enabled end |
Instance Method Details
#format_level(level) ⇒ Object
34 35 36 |
# File 'lib/dockly/util/logger.rb', line 34 def format_level(level) (char = level.to_s[0]) ? char.upcase : nil end |
#format_message(level, message) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/dockly/util/logger.rb', line 21 def (level, ) [ format_level(level), Time.now.iso8601, Process.pid.to_s, Thread.current.object_id.to_s, Thread.current[:rake_task].to_s, prefix, get_last_method, ].compact.reject(&:empty?).join(' ') end |
#get_last_method ⇒ Object
38 39 40 41 42 43 |
# File 'lib/dockly/util/logger.rb', line 38 def get_last_method if print_method? file_and_method = caller.reject { |trace| trace =~ /dockly\/util\/logger\.rb|block \(\d+ levels\)/ }.first file_and_method.match(/:in `(.+)'$/)[1] end end |
#log(level, message) ⇒ Object
13 14 15 |
# File 'lib/dockly/util/logger.rb', line 13 def log(level, ) output.puts((level, )) if self.class.enabled? end |
#print_method? ⇒ Object
Returns the value of attribute print_method.
3 4 5 |
# File 'lib/dockly/util/logger.rb', line 3 def print_method @print_method end |
#with_prefix(new_prefix = "", output = nil, print_method = nil) {|self.class.new([prefix, new_prefix].compact.reject(&:empty?).join(' '), output, print_method)| ... } ⇒ Object
45 46 47 48 49 |
# File 'lib/dockly/util/logger.rb', line 45 def with_prefix(new_prefix = "", output = nil, print_method = nil) output ||= self.output print_method ||= self.print_method yield(self.class.new([prefix, new_prefix].compact.reject(&:empty?).join(' '), output, print_method)) end |