Class: SyslogClient::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/syslog_client/base.rb

Direct Known Subclasses

DealFlow, Editor, Exception, MatchShop

Class Method Summary collapse

Class Method Details

.db_nameObject



79
80
81
# File 'lib/syslog_client/base.rb', line 79

def db_name
  @@db_name ||= 'crawlerdata_web'
end

.db_name=(name) ⇒ Object



72
73
74
75
76
77
# File 'lib/syslog_client/base.rb', line 72

def db_name=(name)
  unless name.is_a?(String) && name.present?
    raise ArgumentError, 'db_name shoude be an presence instance of String!'
  end
  @@db_name = name
end

.loggerObject



42
43
44
# File 'lib/syslog_client/base.rb', line 42

def logger
  @@logger ||= Logglier.new('udp://192.168.2.180:8080')
end

.logger=(logglier) ⇒ Object



35
36
37
38
39
40
# File 'lib/syslog_client/base.rb', line 35

def logger=(logglier)
  unless logglier.is_a?(Logglier)
    raise ArgumentError, 'The logger should be an instance of Logglier!'
  end
  @@logger = logglier
end

.mongodbObject



68
69
70
# File 'lib/syslog_client/base.rb', line 68

def mongodb
  @@mongodb ||= Mongo::Connection.new
end

.mongodb=(connection) ⇒ Object



61
62
63
64
65
66
# File 'lib/syslog_client/base.rb', line 61

def mongodb=(connection)
  unless connection.is_a?(Mongo::Connection) || connection.is_a?(Mongo::MongoClient)
    raise ArgumentError, 'The mongodb should be an instance of Mongo::Connection or Mongo::MongoClient!'
  end
  @@mongodb = connection
end

.record_log(log) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/syslog_client/base.rb', line 25

def record_log(log)
  case @@store_name.to_sym
  when :syslog
    logger.info(log.to_json)
  when :mongodb
    type = log.delete(:type)
    mongodb[db_name][type.to_s.tableize].save(log)
  end
end

.send_log(options) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/syslog_client/base.rb', line 8

def send_log(options)
  return if options.blank?
  infos = log_of_options(options)
  return if infos.blank?
  infos = infos.is_a?(::Array) ? infos : [infos]

  now = Time.now.strftime('%Y-%m-%d %H:%M:%S')
  infos.each do |info|
    if info[:operation_time] && info[:operation_time].respond_to?(:strftime)
      info[:operation_time] = info[:operation_time].strftime('%Y-%m-%d %H:%M:%S')
    else
      info[:operation_time] = now
    end
    record_log(info)
  end
end

.store_nameObject



57
58
59
# File 'lib/syslog_client/base.rb', line 57

def store_name
  @@store_name ||= :syslog
end

.store_name=(store) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/syslog_client/base.rb', line 46

def store_name=(store)
  case store.to_sym
  when :syslog
    @@store_name = store
  when :mongodb
    @@store_name = store
  else
    raise ArgumentError, "The store_name allowed in :syslog, :mongodb"
  end
end