-
Notifications
You must be signed in to change notification settings - Fork 0
/
loggman.rb
64 lines (52 loc) · 1.41 KB
/
loggman.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true
require 'time'
require 'fileutils'
# Loggman class
class Loggman
LOG_PREFIX = 'log'
LOG_DURATION = 7 * 24 * 60 * 60 # 1 week
MAX_LOG_AGE = 60 * 24 * 60 * 60 # 2 months
def initialize(log_dir = nil)
@log_dir = log_dir || default_log_dir
@logfile = generate_logfile
end
def log(message, level = :info)
delete_old_logs
timestamp = Time.now.strftime('%Y-%m-%d %H:%M:%S')
File.open(@logfile, 'a') do |file|
file.puts "[#{timestamp}] [#{level.to_s.upcase}] #{message}"
end
end
def info(message)
log(message, :info)
end
def warn(message)
log(message, :warn)
end
def error(message)
log(message, :error)
end
def debug(message)
log(message, :debug)
end
private
def default_log_dir
backup_dir = MysqlDatabaseConfig.new(nil).backup_dir
File.join(backup_dir, 'logs')
end
def generate_logfile
start_time = Time.now
log_start_day = start_time.strftime('%Y-%m-%d')
end_time = start_time + LOG_DURATION
log_end_day = end_time.strftime('%Y-%m-%d')
log_filename = "#{LOG_PREFIX}-#{log_start_day}-#{log_end_day}.log"
log_path = File.join(@log_dir, log_filename)
FileUtils.mkdir_p(@log_dir)
log_path
end
def delete_old_logs
Dir.glob(File.join(@log_dir, "#{LOG_PREFIX}-*.log")).each do |logfile|
FileUtils.rm(logfile) if Time.now - File.mtime(logfile) > MAX_LOG_AGE
end
end
end