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"
FORMAT_BOLD =

The ANSI format code that makes something bold

"\u001B[1m"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Creates a new logger.

Parameters:

  • fancy (true, false) (defaults to: false)

    Whether this logger uses fancy mode (ANSI escape codes to make the output colourful)

  • streams (Array<IO>, Array<#puts & #flush>) (defaults to: [$stdout])

    the streams the logger should write to.



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)

Returns whether this logger is in extra-fancy mode!.

Returns:

  • (true, false)

    whether this logger is in extra-fancy mode!



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

def fancy=(value)
  @fancy = value
end

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

Returns the streams the logger should write to.

Returns:

  • (Array<IO>, Array<#puts & #flush>)

    the streams the logger should write to.



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

def streams
  @streams
end

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

Returns The bot token to be redacted or nil if it shouldn't.

Returns:

  • (String, nil)

    The bot token to be redacted or nil if it shouldn't.



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

Parameters:

  • value (true, false)

    Whether debug mode should be on. If it is off the mode will be set to :normal.



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.

Parameters:

  • e (Exception)

    The exception to log.



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

Parameters:

  • value (Symbol)

    What logging mode to use



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