Class: Rubycord::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/rubycord/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.



19
20
21
22
23
24
# File 'lib/rubycord/logger.rb', line 19

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!



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

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.



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

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.



11
12
13
# File 'lib/rubycord/logger.rb', line 11

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.



52
53
54
# File 'lib/rubycord/logger.rb', line 52

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.



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

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rubycord/logger.rb', line 64

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