Class: CustomLogs::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/custom_logs/logger.rb

Constant Summary collapse

@@options =
nil

Class Method Summary collapse

Class Method Details

.dump_users(resource) ⇒ Object



47
48
49
# File 'lib/custom_logs/logger.rb', line 47

def dump_users(resource)
  Socket.write({users: resource.all}.to_json)
end

.filter(hash) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/custom_logs/logger.rb', line 11

def filter(hash)

  # truncate long and not strings values
  if options[:include_params]
    hash[:params] = truncate_hash(hash[:params], options[:truncate_length], options[:truncate_value]).to_json
  else
    hash.delete(:params)
  end

  "#{hash.to_json}"

end

.optionsObject



24
25
26
# File 'lib/custom_logs/logger.rb', line 24

def options
  @@options ||= ParseConfig.get
end

.truncate_hash(hash, length, truncate_value) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/custom_logs/logger.rb', line 28

def truncate_hash(hash, length, truncate_value)
  new_hash = {}

  hash.to_hash.each_pair do |key, value|
    if value.blank?
      new_hash[key] = ''
    elsif value.instance_of? String
      new_hash[key] = value.truncate(length)
    elsif value.instance_of? Hash
      new_hash[key] = truncate_hash(value, length, truncate_value)
    else
      # we don't include key when value is not String
      # new_hash[key] = truncate_value
    end
  end

  new_hash
end

.write(hash) ⇒ Object



7
8
9
# File 'lib/custom_logs/logger.rb', line 7

def write(hash)
  Socket.write(filter(hash))
end