Class: Hatchet::HipChatAppender

Inherits:
Object
  • Object
show all
Includes:
LevelManager
Defined in:
lib/hatchet-hipchat/hipchat_appender.rb

Overview

Public: Class for sending log messages to a HipChat room.

API reference: www.hipchat.com/docs/api/method/rooms/message

Constant Summary collapse

CLIENT =

Internal: The class to use to create HipChat clients.

HipChat::API
COLOR_MAP =

Internal: Map for log level to message color.

{
  :debug => 'gray',
  :info  => 'gray',
  :warn  => 'yellow',
  :error => 'red',
  :fatal => 'red'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ HipChatAppender

Public: Creates a new instance.

By default the appender is created with a SimpleFormatter.

block - Optional block that can be used to customize the appender. The

appender itself is passed to the block.

Yields:

  • (_self)

Yield Parameters:



48
49
50
51
52
# File 'lib/hatchet-hipchat/hipchat_appender.rb', line 48

def initialize
  @name = 'Hatchet'
  @formatter = SimpleFormatter.new
  yield self if block_given?
end

Instance Attribute Details

#api_tokenObject

Public: The API token to use to connect to HipChat’s API.



31
32
33
# File 'lib/hatchet-hipchat/hipchat_appender.rb', line 31

def api_token
  @api_token
end

#formatterObject

Public: The formatter used to format messages before sending them to the HipChat room.



27
28
29
# File 'lib/hatchet-hipchat/hipchat_appender.rb', line 27

def formatter
  @formatter
end

#nameObject

Public: The name to post the messages as (default: Hatchet).



39
40
41
# File 'lib/hatchet-hipchat/hipchat_appender.rb', line 39

def name
  @name
end

#room_idObject

Public: The ID of the room to send messages to.



35
36
37
# File 'lib/hatchet-hipchat/hipchat_appender.rb', line 35

def room_id
  @room_id
end

Instance Method Details

#add(level, context, message) ⇒ Object

Internal: Sends a message to HipChat.

level - The level of the message. context - The context of the message. message - The unformatted message.

Returns nothing.



62
63
64
65
# File 'lib/hatchet-hipchat/hipchat_appender.rb', line 62

def add(level, context, message)
  message = @formatter.format(level, context, message)
  client.rooms_message @room_id, @name, message, 0, COLOR_MAP[level], 'text'
end

#clientObject

Internal: Returns the client used to send messages to HipChat.



69
70
71
# File 'lib/hatchet-hipchat/hipchat_appender.rb', line 69

def client
  @client ||= CLIENT.new(@api_token)
end