Class: Cinch::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/cinch/logger.rb,
lib/cinch/logger/zcbot_logger.rb,
lib/cinch/logger/formatted_logger.rb

Overview

This is the base logger class from which all loggers have to inherit.

Version:

  • 2.0.0

Direct Known Subclasses

FormattedLogger, ZcbotLogger

Defined Under Namespace

Classes: FormattedLogger, ZcbotLogger

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, level: :debug) ⇒ Logger

Returns a new instance of Logger.

Parameters:

  • output (IO)

    The I/O object to write log data to



25
26
27
28
29
# File 'lib/cinch/logger.rb', line 25

def initialize(output, level: :debug)
  @output = output
  @mutex  = Mutex.new
  @level  = level
end

Instance Attribute Details

#levelArray<:debug, :log, :info, :warn, :error, :fatal>

Returns The minimum level of events to log.

Returns:

  • (Array<:debug, :log, :info, :warn, :error, :fatal>)

    The minimum level of events to log



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

def level
  @level
end

#mutexMutex (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Mutex)


18
19
20
# File 'lib/cinch/logger.rb', line 18

def mutex
  @mutex
end

#outputIO (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (IO)


22
23
24
# File 'lib/cinch/logger.rb', line 22

def output
  @output
end

Instance Method Details

#debug(message)

This method returns an undefined value.

Logs a debugging message.

Parameters:

Version:

  • 2.0.0



36
37
38
# File 'lib/cinch/logger.rb', line 36

def debug(message)
  log(message, :debug)
end

#error(message)

This method returns an undefined value.

Logs an error message.

Parameters:

Since:

  • 2.0.0



45
46
47
# File 'lib/cinch/logger.rb', line 45

def error(message)
  log(message, :error)
end

#exception(e)

This method returns an undefined value.

Logs an exception.

Parameters:

  • e (Exception)

Since:

  • 2.0.0



99
100
101
# File 'lib/cinch/logger.rb', line 99

def exception(e)
  log(e.message, :exception, :error)
end

#fatal(message)

This method returns an undefined value.

Logs a fatal message.

Parameters:

Since:

  • 2.0.0



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

def fatal(message)
  log(message, :fatal)
end

#incoming(message)

This method returns an undefined value.

Logs an incoming IRC message.

Parameters:

Since:

  • 2.0.0



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

def incoming(message)
  log(message, :incoming, :log)
end

#info(message)

This method returns an undefined value.

Logs an info message.

Parameters:

Since:

  • 2.0.0



63
64
65
# File 'lib/cinch/logger.rb', line 63

def info(message)
  log(message, :info)
end

#log(messages, event = :debug, level = event)

This method returns an undefined value.

Logs a message.

Parameters:

  • messages (String, Array)

    The message(s) to log

  • event (:debug, :incoming, :outgoing, :info, :warn, :exception, :error, :fatal) (defaults to: :debug)

    The kind of event that triggered the message

  • level (:debug, :info, :warn, :error, :fatal) (defaults to: event)

    The level of the message

Version:

  • 2.0.0



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cinch/logger.rb', line 112

def log(messages, event = :debug, level = event)
  return unless will_log?(level)

  @mutex.synchronize do
    Array(messages).each do |message|
      message = format_general(message)
      message = format_message(message, event)

      next if message.nil?

      @output.puts message.encode("locale", invalid: :replace, undef: :replace)
    end
  end
end

#outgoing(message)

This method returns an undefined value.

Logs an outgoing IRC message.

Parameters:

Since:

  • 2.0.0



90
91
92
# File 'lib/cinch/logger.rb', line 90

def outgoing(message)
  log(message, :outgoing, :log)
end

#warn(message)

This method returns an undefined value.

Logs a warning message.

Parameters:

Since:

  • 2.0.0



72
73
74
# File 'lib/cinch/logger.rb', line 72

def warn(message)
  log(message, :warn)
end

#will_log?(level) ⇒ Boolean

Returns Whether the currently set logging level will allow the passed in level to be logged.

Parameters:

  • level (:debug, :info, :warn, :error, :fatal)

Returns:

  • (Boolean)

    Whether the currently set logging level will allow the passed in level to be logged

Since:

  • 2.0.0



131
132
133
# File 'lib/cinch/logger.rb', line 131

def will_log?(level)
  LevelOrder.index(level) >= LevelOrder.index(@level)
end