Class: SmartProxyDynflowCore::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_proxy_dynflow_core/log.rb

Defined Under Namespace

Classes: ProxyAdapter, ProxyStructuredFormater

Constant Summary collapse

BASE_LOG_SIZE =

1 MiB

1024 * 1024
LOGGER_NAME =
'dynflow-core'.freeze

Class Method Summary collapse

Class Method Details

.exception(context_message, exception, options = {}) ⇒ Object

Standard way for logging exceptions to get the most data in the log. By default it logs via warn level, this can be changed via options



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/smart_proxy_dynflow_core/log.rb', line 100

def exception(context_message, exception, options = {})
  level = options[:level] || :warn
  unless ::Logging::LEVELS.keys.include?(level.to_s)
    raise "Unexpected log level #{level}, expected one of #{::Logging::LEVELS.keys}"
  end
  # send class, message and stack as structured fields in addition to message string
  backtrace = exception.backtrace ? exception.backtrace : []
  extra_fields = {
    exception_class: exception.class.name,
    exception_message: exception.message,
    exception_backtrace: backtrace
  }
  extra_fields[:foreman_code] = exception.code if exception.respond_to?(:code)
  with_fields(extra_fields) do
    @logger.public_send(level) do
      ([context_message, "#{exception.class}: #{exception.message}"] + backtrace).join("\n")
    end
  end
end

.instanceObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/smart_proxy_dynflow_core/log.rb', line 49

def instance
  return ::Proxy::LogBuffer::Decorator.instance unless Settings.instance.standalone
  return @logger if @logger
  layout = Logging::Layouts.pattern(pattern: Settings.instance.file_logging_pattern + "\n")
  notime_layout = Logging::Layouts.pattern(pattern: Settings.instance.system_logging_pattern + "\n")
  log_file = Settings.instance.log_file || ''
  @logger = Logging.logger[LOGGER_NAME]
  @reopen = ReopenAppender.new("Reopen dummy appender", @logger)
  @logger.add_appenders(@reopen)
  if !Settings.instance.loaded || log_file.casecmp('STDOUT').zero?
    @logger.add_appenders(Logging.appenders.stdout(LOGGER_NAME, layout: layout))
  elsif log_file.casecmp('SYSLOG').zero?
    unless @syslog_available
      puts "Syslog is not supported on this platform, use STDOUT or a file"
      exit(1)
    end
    @logger.add_appenders(Logging.appenders.syslog(LOGGER_NAME, layout: notime_layout, facility: ::Syslog::Constants::LOG_LOCAL5))
  elsif log_file.casecmp('JOURNAL').zero? || log_file.casecmp('JOURNALD').zero?
    begin
      @logger.add_appenders(Logging.appenders.journald(LOGGER_NAME, LOGGER_NAME: :proxy_logger, layout: notime_layout, facility: ::Syslog::Constants::LOG_LOCAL5))
    rescue NoMethodError
      @logger.add_appenders(Logging.appenders.stdout(LOGGER_NAME, layout: layout))
      @logger.warn "Journald is not available on this platform. Falling back to STDOUT."
    end
  else
    begin
      keep = Settings.instance.file_rolling_keep
      size = BASE_LOG_SIZE * Settings.instance.file_rolling_size
      age = Settings.instance.file_rolling_age
      if size.positive?
        @logger.add_appenders(Logging.appenders.rolling_file(LOGGER_NAME, layout: layout, filename: log_file, keep: keep, size: size, age: age, roll_by: 'number'))
      else
        @logger.add_appenders(Logging.appenders.file(LOGGER_NAME, layout: layout, filename: log_file))
      end
    rescue ArgumentError => ae
      @logger.add_appenders(Logging.appenders.stdout(LOGGER_NAME, layout: layout))
      @logger.warn "Log file #{log_file} cannot be opened. Falling back to STDOUT: #{ae}"
    end
  end
  @logger.level = ::Logging.level_num(Settings.instance.log_level)
  @logger
end

.reload!Object



35
36
37
38
39
40
# File 'lib/smart_proxy_dynflow_core/log.rb', line 35

def reload!
  Logging.logger[LOGGER_NAME].appenders.each(&:close)
  Logging.logger[LOGGER_NAME].clear_appenders
  @logger = nil
  instance
end

.reopenObject



42
43
44
45
46
47
# File 'lib/smart_proxy_dynflow_core/log.rb', line 42

def reopen
  return if @logger.nil? || @reopen.nil?
  if Settings.instance.log_file !~ /^(STDOUT|SYSLOG|JOURNALD?)$/i
    @reopen.set
  end
end

.with_fields(fields = {}) ⇒ Object



92
93
94
95
96
# File 'lib/smart_proxy_dynflow_core/log.rb', line 92

def with_fields(fields = {})
  ::Logging.ndc.push(fields) do
    yield
  end
end