module Morpheus::Logging

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

Constants

AUTHORIZATION_HEADER
DEFAULT_LOG_LEVEL
SECRET_TOKEN_HEADERS

Public Class Methods

debug?() click to toggle source

is log level debug?

# File lib/morpheus/logging.rb, line 72
def self.debug?
  # self.log_level && self.log_level <= Logger::DEBUG
  self.logger.debug?
end
log_level() click to toggle source

set the global log level

# File lib/morpheus/logging.rb, line 53
def self.log_level
  @@log_level
end
logger() click to toggle source

get the global logger instance

# File lib/morpheus/logging.rb, line 35
def self.logger
  if !@@logger
    set_logger(STDOUT, @@log_level)
  end
  @@logger
end
print_stacktrace?() click to toggle source

whether or not to print stack traces

scrub_message(msg) click to toggle source

mask well known secrets and password patterns

# 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?\"[^"]+/, 'password"=>"************')
    msg.gsub!(/Password\"\s?\=\>\s?\"[^"]+/, 'Password"=>"************')
    msg.gsub!(/password\=\"[^"]+/, 'password="************')
    msg.gsub!(/Password\=\"[^"]+/, 'Password="************')
    msg.gsub!(/password\=[^"'&]+/, 'password=************') # buggy, wont work with ampersand or quotes in passwords! heh
    msg.gsub!(/Password\=[^"'&]+/, 'Password=************') 
    msg.gsub!(/passwordConfirmation\=[^" ]+/i, 'passwordConfirmation="************')
    msg.gsub!(/passwordConfirmation\=[^" ]+/i, 'passwordConfirmation=************')
  end
  msg
end
set_log_level(level) click to toggle source

set the global log level

# 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) click to toggle source

set the global logger to another logger or filename

# 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