Class: NexusSemanticLogger::AppenderFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/nexus_semantic_logger/appender_filter.rb

Class Method Summary collapse

Class Method Details

.add_signal_handler(log_names_level_signal = "WINCH", info_signal = "SYS") ⇒ Object

Change LOG_LEVEL and LOG_NAMES_DEFAULT_LEVEL on a running process by sending signals. Each signal rotates through the levels, wrapping around. Based on SemanticLogger.add_signal_handler. Note that USR1/USR2 are already used by puma. WINCH/SYS should be unused these days.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/nexus_semantic_logger/appender_filter.rb', line 86

def self.add_signal_handler(log_names_level_signal = "WINCH", info_signal = "SYS")
  if log_names_level_signal
    Signal.trap(log_names_level_signal) do
      current_level = env_names_default_level
      next_level = get_next_log_level(current_level)
      @@names_default_level = next_level
      puts "#{log_names_level_signal} signal changed LOG_NAMES_DEFAULT_LEVEL from #{current_level} to #{next_level}"
    rescue => err
      puts "Error handling signal #{log_names_level_signal}: #{err}"
      puts err.backtrace
    end
  end

  if info_signal
    Signal.trap(info_signal) do
      current_level = env_names_default_level
      puts "#{info_signal} signal reports LOG_LEVEL=#{env_level} LOG_NAMES_DEFAULT_LEVEL=#{current_level}"
    rescue => err
      puts "Error handling signal #{info_signal}: #{err}"
      puts err.backtrace
    end
  end
end

.env_levelObject



35
36
37
# File 'lib/nexus_semantic_logger/appender_filter.rb', line 35

def self.env_level
  @@level ||= ENV.fetch('LOG_LEVEL', Rails.application.config.log_level).downcase
end

.env_names_debugObject



47
48
49
# File 'lib/nexus_semantic_logger/appender_filter.rb', line 47

def self.env_names_debug
  @@names_debug ||= fetch_env_names('LOG_NAMES_DEBUG')
end

.env_names_default_levelObject



39
40
41
# File 'lib/nexus_semantic_logger/appender_filter.rb', line 39

def self.env_names_default_level
  @@names_default_level ||= ENV.fetch('LOG_NAMES_DEFAULT_LEVEL', Rails.application.config.log_level).downcase
end

.env_names_errorObject



59
60
61
# File 'lib/nexus_semantic_logger/appender_filter.rb', line 59

def self.env_names_error
  @@names_error ||= fetch_env_names('LOG_NAMES_ERROR')
end

.env_names_fatalObject



63
64
65
# File 'lib/nexus_semantic_logger/appender_filter.rb', line 63

def self.env_names_fatal
  @@names_fatal ||= fetch_env_names('LOG_NAMES_FATAL')
end

.env_names_infoObject



51
52
53
# File 'lib/nexus_semantic_logger/appender_filter.rb', line 51

def self.env_names_info
  @@names_info ||= fetch_env_names('LOG_NAMES_INFO')
end

.env_names_traceObject



43
44
45
# File 'lib/nexus_semantic_logger/appender_filter.rb', line 43

def self.env_names_trace
  @@names_trace ||= fetch_env_names('LOG_NAMES_TRACE')
end

.env_names_warnObject



55
56
57
# File 'lib/nexus_semantic_logger/appender_filter.rb', line 55

def self.env_names_warn
  @@names_warn ||= fetch_env_names('LOG_NAMES_WARN')
end

.fetch_env_names(var) ⇒ Object



78
79
80
# File 'lib/nexus_semantic_logger/appender_filter.rb', line 78

def self.fetch_env_names(var)
  ENV.fetch(var, '').split(',').to_set
end

.filter_lambdaObject



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
# File 'lib/nexus_semantic_logger/appender_filter.rb', line 6

def self.filter_lambda
  -> (log) {
    # log API see https://logger.rocketjob.io/log_struct.html
    # and lib/semantic_logger/levels.rb
    # level: :trace=>0, :debug=>1, :info=>2, :warn=>3, :error=>4, :fatal=>5
    current_log_level = SemanticLogger::Levels.index(env_names_default_level)

    # Names allow overriding the level that will be appended,
    # else the global level is used to determine appending.
    append = false
    if log.name.in?(env_names_trace)
      append = true
    elsif log.name.in?(env_names_debug)
      append = log.level_index >= 1
    elsif log.name.in?(env_names_info)
      append = log.level_index >= 2
    elsif log.name.in?(env_names_warn)
      append = log.level_index >= 3
    elsif log.name.in?(env_names_error)
      append = log.level_index >= 4
    elsif log.name.in?(env_names_fatal)
      append = log.level_index >= 5
    else
      append = log.level_index >= current_log_level
    end
    append
  }
end

.flushObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/nexus_semantic_logger/appender_filter.rb', line 67

def self.flush
  @@level = nil
  @@names_default_level = nil
  @@names_trace = nil
  @@names_debug = nil
  @@names_info = nil
  @@names_warn = nil
  @@names_error = nil
  @@names_fatal = nil
end

.get_next_log_level(current_log_level) ⇒ Object



110
111
112
113
114
115
# File 'lib/nexus_semantic_logger/appender_filter.rb', line 110

def self.get_next_log_level(current_log_level)
  current_log_level_index = SemanticLogger::Levels.index(current_log_level)
  next_log_level_index = current_log_level_index + 1
  next_log_level_index = 0 if next_log_level_index >= SemanticLogger::Levels.all_levels.size
  SemanticLogger::Levels.level(next_log_level_index)
end