Class: Relaton::Logger::Log

Inherits:
Logger
  • Object
show all
Defined in:
lib/relaton/logger/log.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logdev, shift_age = 0, shift_size = 1_048_576, **args) ⇒ Log

rubocop:disable Lint/MissingSuper



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/relaton/logger/log.rb', line 5

def initialize(logdev, shift_age = 0, shift_size = 1_048_576, **args) # rubocop:disable Lint/MissingSuper
  self.levels = args.delete(:levels) || [UNKNOWN, FATAL, ERROR, WARN, INFO]
  self.level = @levels.min
  self.progname = args.delete(:progname)
  @default_formatter = FormatterString.new
  self.datetime_format = args.delete(:datetime_format)
  self.formatter = args.delete(:formatter)
  self.formatter = self.formatter.new if self.formatter.is_a? Class
  @logdev = nil
  @level_override = {}
  if logdev && logdev != File::NULL
    @logdev = LogDevice.new(logdev, shift_age: shift_age, shift_size: shift_size, **args)
  end
end

Instance Attribute Details

#levelsObject

Returns the value of attribute levels.



3
4
5
# File 'lib/relaton/logger/log.rb', line 3

def levels
  @levels
end

Instance Method Details

#add(severity, message = nil, progname = nil, **args) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/relaton/logger/log.rb', line 42

def add(severity, message = nil, progname = nil, **args) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  severity ||= UNKNOWN
  return true if @logdev.nil? || !@levels.include?(severity)

  if block_given?
    progname ||= message || @progname
    message = yield
  else
    progname ||= @progname
  end
  @logdev.write format_message(format_severity(severity), Time.now, progname, message, **args)
  true
end

#add_level(level) ⇒ Object



25
26
27
28
# File 'lib/relaton/logger/log.rb', line 25

def add_level(level)
  @levels << Severity.coerce(level)
  self.level = @levels.min
end

#format_message(severity, datetime, progname, msg, **args) ⇒ Object



56
57
58
# File 'lib/relaton/logger/log.rb', line 56

def format_message(severity, datetime, progname, msg, **args)
  (@formatter || @default_formatter).call(severity, datetime, progname, msg, **args)
end

#remove_level(level) ⇒ Object



30
31
32
33
# File 'lib/relaton/logger/log.rb', line 30

def remove_level(level)
  @levels.delete Severity.coerce(level)
  self.level = @levels.min
end

#truncateObject



60
61
62
# File 'lib/relaton/logger/log.rb', line 60

def truncate
  @logdev.truncate
end

#unknown(message = nil, progname = nil, **args, &block) ⇒ Object



35
36
37
38
# File 'lib/relaton/logger/log.rb', line 35

def unknown(message = nil, progname = nil, **args, &block)
  level = Object.const_get "Logger::#{__callee__.to_s.upcase}"
  add(level, message, progname, **args, &block)
end