Class: Discordrb::Logger

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

Overview

Logs debug messages

Constant Summary collapse

MODES =

The modes this logger can have. This is probably useless unless you want to write your own Logger

{
  debug: { long: 'DEBUG', short: 'D', format_code: '' },
  good: { long: 'GOOD', short: '', format_code: "\u001B[32m" }, # green
  info: { long: 'INFO', short: 'i', format_code: '' },
  warn: { long: 'WARN', short: '!', format_code: "\u001B[33m" }, # yellow
  error: { long: 'ERROR', short: '', format_code: "\u001B[31m" }, # red
  out: { long: 'OUT', short: '', format_code: "\u001B[36m" }, # cyan
  in: { long: 'IN', short: '', format_code: "\u001B[35m" }, # purple
  ratelimit: { long: 'RATELIMIT', short: 'R', format_code: "\u001B[41m" } # red background
}.freeze
FORMAT_RESET =

The ANSI format code that resets formatting

"\u001B[0m".freeze
FORMAT_BOLD =

The ANSI format code that makes something bold

"\u001B[1m".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fancy = false, streams = [STDOUT]) ⇒ Logger

Creates a new logger.


21
22
23
24
25
26
# File 'lib/discordrb/logger.rb', line 21

def initialize(fancy = false, streams = [STDOUT])
  @fancy = fancy
  self.mode = :normal

  @streams = streams
end

Instance Attribute Details

#fancy=(value) ⇒ true, false (writeonly)


10
11
12
# File 'lib/discordrb/logger.rb', line 10

def fancy=(value)
  @fancy = value
end

#streamsArray<IO>, Array<#puts & #flush>


16
17
18
# File 'lib/discordrb/logger.rb', line 16

def streams
  @streams
end

#token=(value) ⇒ String? (writeonly)


13
14
15
# File 'lib/discordrb/logger.rb', line 13

def token=(value)
  @token = value
end

Instance Method Details

#debug=(value) ⇒ Object

Sets the logging mode to :debug


54
55
56
# File 'lib/discordrb/logger.rb', line 54

def debug=(value)
  self.mode = value ? :debug : :normal
end

#log_exception(e) ⇒ Object

Logs an exception to the console.


83
84
85
86
# File 'lib/discordrb/logger.rb', line 83

def log_exception(e)
  error("Exception: #{e.inspect}")
  e.backtrace.each { |line| error(line) }
end

#mode=(value) ⇒ Object

Sets the logging mode Possible modes are:

  • :debug logs everything
  • :verbose logs everything except for debug messages
  • :normal logs useful information, warnings and errors
  • :quiet only logs warnings and errors
  • :silent logs nothing

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

def mode=(value)
  case value
  when :debug
    @enabled_modes = %i[debug good info warn error out in ratelimit]
  when :verbose
    @enabled_modes = %i[good info warn error out in ratelimit]
  when :normal
    @enabled_modes = %i[info warn error ratelimit]
  when :quiet
    @enabled_modes = %i[warn error]
  when :silent
    @enabled_modes = %i[]
  end
end