Module: Morpheus::Logging

Defined in:
lib/morpheus/logging.rb

Overview

Provides global Logging behavior By default, Morpheus::Logging.logger is set to STDOUT with level INFO

Defined Under Namespace

Modules: HasLogger Classes: DarkPrinter, Logger

Constant Summary collapse

DEFAULT_LOG_LEVEL =
ENV['DEBUG'] ? Logger::DEBUG : Logger::INFO
AUTHORIZATION_HEADER =
'Authorization'
SECRET_TOKEN_HEADERS =
['X-Morpheus-Token', 'X-Cypher-Token', 'X-Vault-Token', 'X-Morpheus-Lease']
@@log_level =
DEFAULT_LOG_LEVEL
@@logger =
nil

Class Method Summary collapse

Class Method Details

.debug?Boolean

is log level debug?

Returns:

  • (Boolean)


72
73
74
75
# File 'lib/morpheus/logging.rb', line 72

def self.debug?
  # self.log_level && self.log_level <= Logger::DEBUG
  self.logger.debug?
end

.log_levelObject

set the global log level



53
54
55
# File 'lib/morpheus/logging.rb', line 53

def self.log_level
  @@log_level
end

.loggerObject

get the global logger instance



35
36
37
38
39
40
# File 'lib/morpheus/logging.rb', line 35

def self.logger
  if !@@logger
    set_logger(STDOUT, @@log_level)
  end
  @@logger
end

whether or not to print stack traces

Returns:

  • (Boolean)


78
79
80
# File 'lib/morpheus/logging.rb', line 78

def self.print_stacktrace?
  self.debug?
end

.scrub_message(msg) ⇒ Object

mask well known secrets and password patterns



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/morpheus/logging.rb', line 83

def self.scrub_message(msg)
  if msg.is_a?(String)
    msg = msg.clone
    # looks for RestClient format (hash.inspect) and request/curl output name: value
    msg.gsub!(/Authorization\"\s?\=\>\s?\"Bearer [^"]+/i, 'Authorization"=>"Bearer ************')
    msg.gsub!(/Authorization\:\s?Bearer [^"'']+/i, 'Authorization: Bearer ************')
    # msg.gsub!(/#{AUTHORIZATION_HEADER}\"\s?\=\>\s?\"Bearer [^"]+/, "#{AUTHORIZATION_HEADER}"=>"Bearer ************")
    # msg.gsub!(/#{AUTHORIZATION_HEADER}\:\s?Bearer [^"'']+/, "#{AUTHORIZATION_HEADER}: Bearer ************")
    SECRET_TOKEN_HEADERS.each do |header|
      msg.gsub!(/#{header}\"\s?\=\>\s?\"[^"]+/, "#{header}\"=>\"************")
      msg.gsub!(/#{header}\:\s?[^"'']+/, "#{header}: ************")
    end
    msg.gsub!(/password\"\: "[^"]+/, 'password": "************') # json properties ending with password
    msg.gsub!(/Password\"\: "[^"]+/, 'Password": "************') # json properties ending with Password
    msg.gsub!(/password\"\s?\=\>\s?\"[^"]+/i, 'password"=>"************')
    msg.gsub!(/password\=\"[^"]+/i, 'password="************')
    msg.gsub!(/password\=[^"'&\Z]+/i, 'password=************') # buggy, wont work with ampersand or quotes in passwords! heh
    msg.gsub!(/passwordConfirmation\=[^" ]+/i, 'passwordConfirmation="************')
    msg.gsub!(/passwordConfirmation\=[^" ]+/i, 'passwordConfirmation=************')
  end
  msg
end

.set_log_level(level) ⇒ Object

set the global log level



58
59
60
61
62
63
64
# File 'lib/morpheus/logging.rb', line 58

def self.set_log_level(level)
  @@log_level = level.to_i
  if @@logger
    @@logger.level = @@log_level
  end
  @@log_level
end

.set_logger(logdev, log_level = @@log_level) ⇒ Object

set the global logger to another logger or filename



43
44
45
46
47
48
49
50
# File 'lib/morpheus/logging.rb', line 43

def self.set_logger(logdev, log_level = @@log_level)
  @@logger = logdev.is_a?(Logger) ? logdev : Logger.new(logdev)
  @@logger.level = log_level || DEFAULT_LOG_LEVEL
  @@logger.formatter = proc do |severity, datetime, progname, msg|
    "#{msg}\n"
  end
  @@logger
end