class Discordrb::Logger

Logs debug messages

Constants

FORMAT_BOLD

The ANSI format code that makes something bold

FORMAT_RESET

The ANSI format code that resets formatting

MODES

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

Attributes

fancy[W]

@return [true, false] whether this logger is in extra-fancy mode!

streams[RW]

@return [Array<IO>, Array<#puts & flush>] the streams the logger should write to.

token[W]

@return [String, nil] The bot token to be redacted or nil if it shouldn’t.

Public Class Methods

new(fancy = false, streams = [$stdout]) click to toggle source

Creates a new logger. @param fancy [true, false] Whether this logger uses fancy mode (ANSI escape codes to make the output colourful) @param streams [Array<IO>, Array<#puts & flush>] the streams the logger should write to.

# File lib/discordrb/logger.rb, line 21
def initialize(fancy = false, streams = [$stdout])
  @fancy = fancy
  self.mode = :normal

  @streams = streams
end

Public Instance Methods

debug=(value) click to toggle source

Sets the logging mode to :debug @param value [true, false] Whether debug mode should be on. If it is off the mode will be set to :normal.

# File lib/discordrb/logger.rb, line 54
def debug=(value)
  self.mode = value ? :debug : :normal
end
log_exception(e) click to toggle source

Logs an exception to the console. @param e [Exception] The exception to log.

# File lib/discordrb/logger.rb, line 83
def log_exception(e)
  error("Exception: #{e.inspect}")
  e.backtrace.each { |line| error(line) }
end
mode=(value) click to toggle source

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

@param value [Symbol] What logging mode to use

# 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

Private Instance Methods

fancy_write(stream, message, mode, thread_name, timestamp) click to toggle source
# File lib/discordrb/logger.rb, line 110
def fancy_write(stream, message, mode, thread_name, timestamp)
  stream.puts "#{timestamp} #{FORMAT_BOLD}#{thread_name.ljust(16)}#{FORMAT_RESET} #{mode[:format_code]}#{mode[:short]}#{FORMAT_RESET} #{message}"
  stream.flush
end
simple_write(stream, message, mode, thread_name, timestamp) click to toggle source
# File lib/discordrb/logger.rb, line 115
def simple_write(stream, message, mode, thread_name, timestamp)
  stream.puts "[#{mode[:long]} : #{thread_name} @ #{timestamp}] #{message}"
  stream.flush
end
write(message, mode) click to toggle source
# File lib/discordrb/logger.rb, line 90
def write(message, mode)
  thread_name = Thread.current[:discordrb_name]
  timestamp = Time.now.strftime(LOG_TIMESTAMP_FORMAT)

  # Redact token if set
  log = if @token && @token != ''
          message.to_s.gsub(@token, 'REDACTED_TOKEN')
        else
          message.to_s
        end

  @streams.each do |stream|
    if @fancy && !stream.is_a?(File)
      fancy_write(stream, log, mode, thread_name, timestamp)
    else
      simple_write(stream, log, mode, thread_name, timestamp)
    end
  end
end