Class: Navo::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/navo/logger.rb

Overview

Manages the display of output and writing of log files.

The goal is to make the tool easier to read when running from the command line, but preserving all useful information in log files as well so in-depth debugging can be performed.

Each test suite creates its own Output instance to write to so individual log lines go to their own files, but they all share a common destination that is synchronized when writing to stdout/stderr for the application as a whole.

Constant Summary collapse

UI_COLORS =
{
  unknown: 35, # purple
  fatal: 31,   # red
  error: 31,   # red
  warn: 33,    # yellow
  info: nil,   # normal
  debug: 90,   # gray
}

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(suite: nil) ⇒ Logger

Returns a new instance of Logger.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/navo/logger.rb', line 45

def initialize(suite: nil)
  @suite = suite

  if suite
    log_file = File.open(suite.log_file, File::CREAT | File::WRONLY | File::APPEND)
    @logger = ::Logger.new(log_file)
    @logger.level = self.class.level
  end

  @color_hash = {}
end

Class Attribute Details

.levelObject

Returns the value of attribute level.



29
30
31
# File 'lib/navo/logger.rb', line 29

def level
  @level
end

.loggerObject (readonly)

Returns the value of attribute logger.



27
28
29
# File 'lib/navo/logger.rb', line 27

def logger
  @logger
end

.mutexObject (readonly)

Returns the value of attribute mutex.



31
32
33
# File 'lib/navo/logger.rb', line 31

def mutex
  @mutex
end

Class Method Details

.output=(out) ⇒ Object



33
34
35
36
# File 'lib/navo/logger.rb', line 33

def output=(out)
  @logger = ::Logger.new(out)
  @mutex = Mutex.new
end

Instance Method Details

#console(message, severity: :info, flush: true) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/navo/logger.rb', line 57

def console(message, severity: :info, flush: true)
  # In order to improve the output displayed, we don't want to print a line
  # for every chunk of log received--only logs which have newlines.
  # Thus we buffer the output and flush it once we have the full amount.
  @buffer_severity ||= severity
  @buffer_level ||= ::Logger.const_get(severity.upcase)
  @buffer ||= ''
  @buffer += message

  flush_buffer if flush
end

#flush_bufferObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/navo/logger.rb', line 81

def flush_buffer
  if @buffer
    # This is shared amongst potentially many threads, so serialize access
    if @buffer_level >= self.class.level
      self.class.mutex.synchronize do
        self.class.logger << pretty_message(@buffer_severity, @buffer)
      end
    end

    @buffer = nil
    @buffer_severity = nil
    @buffer_level = nil
  end
end

#log(severity, message, flush: true) ⇒ Object



69
70
71
72
73
# File 'lib/navo/logger.rb', line 69

def log(severity, message, flush: true)
  level = ::Logger.const_get(severity.upcase)
  @logger.add(level, message) if @logger
  console(message, severity: severity, flush: flush)
end