Class: Logman

Inherits:
Object
  • Object
show all
Defined in:
lib/logman_rails.rb,
lib/logman_rails/log.rb

Defined Under Namespace

Classes: Log, Rails

Class Method Summary collapse

Class Method Details

.error(message, data = {}) ⇒ Object

Sends a error message



109
110
111
# File 'lib/logman_rails.rb', line 109

def self.error(message, data={})
	Logman.log(:error, message ,data)
end

.info(message, data = {}) ⇒ Object

Sends a success message



119
120
121
# File 'lib/logman_rails.rb', line 119

def self.info(message, data={})
	Logman.log(:info, message ,data)
end

.log(log_type, message, data = {}) ⇒ Object

Writes custom message to logman endpoint Logtype is symbol, avalabile symbols are:

Prameters:
- log_type => type of log, avalabile types are
  	:error :success :warn :info
- message => message in string format
- optional data


89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/logman_rails.rb', line 89

def self.log(log_type, message, data={})
	return if message.class != String || message.empty?
	
	map = {:error=>1, :success=>2, :warn=>3, :info=>4}
	ltype = map[log_type]
	ltype = 4 if ltype.nil? #info
	
	newlog = Log.new
	newlog.message = message
	newlog.log_type = ltype
	newlog.data = data
	Logman.send(newlog, Logman.options[:endpoint], Logman.options[:token])
end

.optionsObject



12
13
14
# File 'lib/logman_rails.rb', line 12

def self.options
	@@options
end

.options=(val) ⇒ Object



16
17
18
# File 'lib/logman_rails.rb', line 16

def self.options=(val)
	@@options = val
end

.send(log, endpoint, token) ⇒ Object

end of rails class



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/logman_rails.rb', line 65

def self.send(log, endpoint, token)
	begin
		uri = URI(endpoint)
		uri.path = '/api/write'
		uri.query = "key=#{token}"

		req = Net::HTTP::Post.new(uri.request_uri, initheader = {'Content-Type' =>'application/json'})
		req.body = log.to_json

		res = Net::HTTP.start(uri.hostname, uri.port) do |http|
		  http.request(req)
		end
	rescue
		puts 'Can not send message to logman endpoint'
	end
end

.success(message, data = {}) ⇒ Object

Sends a success message



114
115
116
# File 'lib/logman_rails.rb', line 114

def self.success(message, data={})
	Logman.log(:success, message ,data)
end

.warn(message, data = {}) ⇒ Object

Sends a warn message



104
105
106
# File 'lib/logman_rails.rb', line 104

def self.warn(message, data={})
	Logman.log(:warn, message ,data)
end