Module: TingYun::Logger::CreateLoggerHelper

Included in:
AgentLogger
Defined in:
lib/ting_yun/logger/create_logger_helper.rb

Class Method Summary collapse

Class Method Details

.check_log_fileObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ting_yun/logger/create_logger_helper.rb', line 24

def check_log_file
  unless File.exist? @file_path
    begin
      @log = ::Logger.new(@file_path)
      set_log_format
      set_log_level
    rescue => e
      @log = ::Logger.new(STDOUT)
      warn("check_log_file:  Failed creating logger for file #{@file_path}, using standard out for logging.", e)
    end
  end
end

.create_log(root, override_logger) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ting_yun/logger/create_logger_helper.rb', line 9

def create_log(root, override_logger)
  if !override_logger.nil?
    @log = override_logger
  elsif ::TingYun::Agent.config[:'nbs.agent_enabled'] == false
    create_null_logger
  else
    if wants_stdout?
      @log = ::Logger.new(STDOUT)
    else
      create_log_to_file(root)
    end
  end
end

.create_log_to_file(root) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ting_yun/logger/create_logger_helper.rb', line 52

def create_log_to_file(root)
  path = find_or_create_file_path(::TingYun::Agent.config[:agent_log_file_path], root)
  unless path
    @log = ::Logger.new(STDOUT)
    warn("Error creating log directory #{::TingYun::Agent.config[:agent_log_file_path]}, using standard out for logging.")
  else
    @file_path = "#{path}/#{::TingYun::Agent.config[:agent_log_file_name]}"
    begin
      @log = ::Logger.new(@file_path)
    rescue => e
      @log = ::Logger.new(STDOUT)
      warn("Failed creating logger for file #{@file_path}, using standard out for logging.", e)
    end
  end
end

.create_new_logfileObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ting_yun/logger/create_logger_helper.rb', line 37

def create_new_logfile
  file_path = TingYun::Agent.logger.file_path
  shift_age = TingYun::Agent.config[:agent_log_file_number]
  if File.size(file_path) >= (TingYun::Agent.config[:agent_log_file_size])*1024*1024
    (shift_age-3).downto(0) do |i|
      index_file = file_path.gsub('.log', "_#{i}.log")
      if FileTest.exist?(index_file)
        File.rename(index_file, file_path.gsub('.log', "_#{i+1}.log"))
      end
    end
    TingYun::Agent.agent.untraced_graceful_disconnect
    File.rename(file_path, file_path.gsub('.log', "_0.log"))
  end
end

.create_null_loggerObject



68
69
70
# File 'lib/ting_yun/logger/create_logger_helper.rb', line 68

def create_null_logger
  @log = ::TingYun::Logger::NullLogger.new
end

.find_or_create_file_path(path_setting, root) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/ting_yun/logger/create_logger_helper.rb', line 73

def find_or_create_file_path(path_setting, root)
  for abs_path in [File.expand_path(path_setting),
                   File.expand_path(File.join(root, path_setting))] do
    if File.directory?(abs_path) || (FileUtils.mkdir_p(abs_path) rescue nil)
      return abs_path[%r{^(.*?)/?$}]
    end
  end
  nil
end