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
}.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) ⇒ 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)



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

def initialize(fancy = false)
  @fancy = fancy
  self.mode = :normal
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

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.



44
45
46
# File 'lib/discordrb/logger.rb', line 44

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.



73
74
75
76
# File 'lib/discordrb/logger.rb', line 73

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/discordrb/logger.rb', line 56

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