Class: Terraspace::Logger

Inherits:
Logger
  • Object
show all
Defined in:
lib/terraspace/logger.rb,
lib/terraspace/logger/formatter.rb

Defined Under Namespace

Classes: Formatter

Constant Summary collapse

@@stdin_capture =
''
@@buffer =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Logger

Returns a new instance of Logger.



5
6
7
8
9
# File 'lib/terraspace/logger.rb', line 5

def initialize(*args)
  super
  self.formatter = Formatter.new
  self.level = ENV['TS_LOG_LEVEL'] || :info # note: only respected when config.logger not set in config/app.rb
end

Class Method Details

.bufferObject



43
44
45
# File 'lib/terraspace/logger.rb', line 43

def buffer
  @@buffer
end

.clearObject

for test framework



53
54
55
56
# File 'lib/terraspace/logger.rb', line 53

def clear
  Terraspace::Command.reset_dispatch_command
  @@buffer = []
end

.logsObject



47
48
49
50
# File 'lib/terraspace/logger.rb', line 47

def logs
  # force_encoding https://jch.github.io/posts/2013-03-05-ruby-incompatible-encoding.html
  @@buffer.map { |s| s.force_encoding('UTF-8') }.join('')
end

.stdin_captureObject



38
39
40
# File 'lib/terraspace/logger.rb', line 38

def stdin_capture
  @@stdin_capture
end

Instance Method Details

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



11
12
13
14
15
16
17
18
19
20
# File 'lib/terraspace/logger.rb', line 11

def format_message(severity, datetime, progname, msg)
  line = if @logdev.dev == $stdout || @logdev.dev == $stderr
    msg # super simple format if stdout
  else
    super # use the configured formatter
  end
  out = line.force_encoding('UTF-8') =~ /\n$/ ? line : "#{line}\n"
  @@buffer << out
  out
end

#stdin_capture(text) ⇒ Object



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

def stdin_capture(text)
  @@buffer << "#{text}\n"
  @@stdin_capture = text
end

#stdout(msg, newline: true) ⇒ Object

Used to allow terraform output to always go to stdout Terraspace output goes to stderr by default See: terraspace/shell.rb



25
26
27
28
29
# File 'lib/terraspace/logger.rb', line 25

def stdout(msg, newline: true)
  out = newline ? "#{msg}\n" : msg
  @@buffer << out
  print out
end