Class: ProcessBot::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/process_bot/logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options:) ⇒ Logger

Returns a new instance of Logger.



4
5
6
# File 'lib/process_bot/logger.rb', line 4

def initialize(options:)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#broadcast_log(output, type) ⇒ Object



62
63
64
65
66
# File 'lib/process_bot/logger.rb', line 62

def broadcast_log(output, type)
  return unless should_broadcast?(type)

  options.events.call(:on_log, output: output, type: type)
end

#debug_enabled?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/process_bot/logger.rb', line 81

def debug_enabled?
  truthy_option?(:debug)
end

#error(output) ⇒ Object



8
9
10
# File 'lib/process_bot/logger.rb', line 8

def error(output)
  logs(output, type: :stderr)
end

#fp_logObject



34
35
36
# File 'lib/process_bot/logger.rb', line 34

def fp_log
  @fp_log ||= File.open(log_file_path, "a") if log_to_file?
end

#log(output, type: :stdout) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/process_bot/logger.rb', line 12

def log(output, type: :stdout)
  write_output(output, type)
  broadcast_log(output, type)

  return unless log_to_file?
  return if type == :debug && !debug_enabled?

  write_log_file(output)
end

#log_file_pathObject



26
27
28
# File 'lib/process_bot/logger.rb', line 26

def log_file_path
  options.fetch(:log_file_path)
end

#log_to_file?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/process_bot/logger.rb', line 30

def log_to_file?
  options.present?(:log_file_path)
end

#logging_enabled?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/process_bot/logger.rb', line 85

def logging_enabled?
  truthy_option?(:log) || truthy_option?(:logging) || debug_enabled?
end

#logs(output, type: :info, **args) ⇒ Object



22
23
24
# File 'lib/process_bot/logger.rb', line 22

def logs(output, type: :info, **args)
  log("#{output}\n", type: type, **args)
end

#should_broadcast?(type) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/process_bot/logger.rb', line 68

def should_broadcast?(type)
  case type
  when :stdout, :stderr
    true
  when :info
    logging_enabled?
  when :debug
    debug_enabled?
  else
    false
  end
end

#truthy_option?(key) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
98
# File 'lib/process_bot/logger.rb', line 89

def truthy_option?(key)
  value = options[key]
  return false if value.nil?
  return value if value == true || value == false

  normalized = value.to_s.strip.downcase
  return false if normalized == "false" || normalized == "0" || normalized == ""

  true
end

#write_log_file(output) ⇒ Object



57
58
59
60
# File 'lib/process_bot/logger.rb', line 57

def write_log_file(output)
  fp_log.write(output)
  fp_log.flush
end

#write_output(output, type) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/process_bot/logger.rb', line 38

def write_output(output, type)
  case type
  when :stdout
    $stdout.print output
    $stdout.flush
  when :stderr
    $stderr.print output
    $stderr.flush
  when :info
    $stdout.print output if logging_enabled?
    $stdout.flush if logging_enabled?
  when :debug
    $stdout.print output if debug_enabled?
    $stdout.flush if debug_enabled?
  else
    raise "Unknown type: #{type}"
  end
end