Class: Bridgetown::LogWriter

Inherits:
Logger
  • Object
show all
Defined in:
lib/bridgetown-core/log_writer.rb

Direct Known Subclasses

Rack::Logger

Instance Method Summary collapse

Constructor Details

#initializeLogWriter

Returns a new instance of LogWriter.



5
6
7
# File 'lib/bridgetown-core/log_writer.rb', line 5

def initialize
  super($stdout, formatter: proc { |_, _, _, msg| msg.to_s })
end

Instance Method Details

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bridgetown-core/log_writer.rb', line 15

def add(severity, message = nil, progname = nil)
  severity ||= UNKNOWN
  @logdev = logdevice(severity)

  return true if @logdev.nil? || severity < @level

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

#closeObject



46
47
48
# File 'lib/bridgetown-core/log_writer.rb', line 46

def close
  # No LogDevice in use
end

#enable_prefixObject



9
10
11
12
13
# File 'lib/bridgetown-core/log_writer.rb', line 9

def enable_prefix
  self.formatter = proc do |_, _, _, msg|
    "\e[32m[Bridgetown]\e[0m #{msg}"
  end
end

#error(progname = nil) ⇒ Object

Log an ERROR message



42
43
44
# File 'lib/bridgetown-core/log_writer.rb', line 42

def error(progname = nil, &)
  add(ERROR, nil, progname.red, &)
end

#warn(progname = nil) ⇒ Object

Log a WARN message



37
38
39
# File 'lib/bridgetown-core/log_writer.rb', line 37

def warn(progname = nil, &)
  add(WARN, nil, progname.yellow, &)
end