Class: Micetrap::Logger

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

Constant Summary collapse

TIMESTAMP_FORMAT =
"%Y-%m-%d__%H-%M"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_name) ⇒ Logger

Returns a new instance of Logger.



7
8
9
10
11
12
13
# File 'lib/micetrap/logger.rb', line 7

def initialize(service_name)
  @service_name = service_name
  @filename = ['micetrap',
               service_name,
               Time.now.strftime(TIMESTAMP_FORMAT)
              ].join('_') + '.log'
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



5
6
7
# File 'lib/micetrap/logger.rb', line 5

def filename
  @filename
end

Instance Method Details

#fileObject



15
16
17
# File 'lib/micetrap/logger.rb', line 15

def file
  @file ||= File.new(@filename, 'a')
end

#log_message(line) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/micetrap/logger.rb', line 28

def log_message(line)
  logged = "\n#{Time.now} ::: #{line} :::"
  File.open(@filename, 'a') do |f|
    f.write logged
  end
  puts logged
end

#log_probe(line, remote_host, remote_port) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/micetrap/logger.rb', line 19

def log_probe(line, remote_host, remote_port)
  content = line.strip.length > 0 ? line : '(empty line)'
  logged = "\n#{Time.now} Recorded a probe coming from #{remote_host}:#{remote_port} containing the following: #{content}"
  File.open(@filename, 'a') do |f|
    f.write logged
  end
  puts logged
end