Class: Dockly::Util::Logger

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin

included, logger

Methods included from Delegate

#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

.outputObject



95
96
97
# File 'lib/dockly/util/logger.rb', line 95

def output
  @output ||= STDOUT
end


91
92
93
# File 'lib/dockly/util/logger.rb', line 91

def print_method
  (@print_method == false) ? false : true
end

Instance Attribute Details

#outputObject

Returns the value of attribute output.



2
3
4
# File 'lib/dockly/util/logger.rb', line 2

def output
  @output
end

#prefixObject

Returns the value of attribute prefix.



2
3
4
# File 'lib/dockly/util/logger.rb', line 2

def prefix
  @prefix
end

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

Returns:

  • (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 format_message(level, message)
  [
    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,
    message
  ].compact.reject(&:empty?).join(' ')
end

#get_last_methodObject



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, message)
  output.puts(format_message(level, message)) if self.class.enabled?
end

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

Yields:



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