Class: FancyLogger

Inherits:
Logger
  • Object
show all
Defined in:
lib/fancy_logger.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#enabled=(value) ⇒ Object (writeonly)

Sets the attribute enabled

Parameters:

  • value

    the value to set the attribute enabled to.



50
51
52
# File 'lib/fancy_logger.rb', line 50

def enabled=(value)
  @enabled = value
end

Class Method Details

.default_configObject



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
# File 'lib/fancy_logger.rb', line 7

def self.default_config
  @default_config ||= Outline.new do
    timestamp_format "%F %r"
    show_help_message true
    
    styles do
      debug do
        foreground :black
        background :cyan
      end
      
      info do
        foreground :default
        background :default
      end
      
      warn do
        foreground :yellow
        background :default
        blink true
      end
      
      error do
        foreground :red
        background :default
      end
      
      fatal do
        foreground :black
        background :red
        bold true
        underline true
      end
      
      unknown do
        foreground :black
        background :white
        underline true
      end
    end
  end
end

Instance Method Details

#config(&blk) ⇒ Object



52
53
54
55
56
57
# File 'lib/fancy_logger.rb', line 52

def config(&blk)
  @config ||= self.class.default_config
  @config = Outline.new(data: self.class.default_config, &blk) if block_given?
  
  @config
end

#format_message(severity, timestamp, progname, msg) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/fancy_logger.rb', line 69

def format_message(severity, timestamp, progname, msg)
  @the_help_message_has_been_displayed_before ||= false
  @enabled ||= true
  
  if @enabled
    message = ""
    
    if @the_help_message_has_been_displayed_before == false && config.show_help_message == true
      @the_help_message_has_been_displayed_before = true
      message += help_message
    end
    timestamp = styled_timestamp(severity, timestamp)
    
    message += "[#{timestamp}] #{msg}\n"
  end
end

#help_messageObject



59
60
61
62
63
64
65
66
67
# File 'lib/fancy_logger.rb', line 59

def help_message
  message = "Log Styles:\n"
  
  config.styles.to_h.keys.each do |severity|
    message += "  #{styled_string(severity, severity)}\n"
  end
  
  message += "\n\n"
end