Class: Contrast::Logger::Log

Inherits:
Object
  • Object
show all
Includes:
Components::Interface, Singleton
Defined in:
lib/contrast/logger/log.rb

Overview

This class functions to serve as a wrapper around our logging, as we need to be able to dynamically update level based on updates to TeamServer.

Constant Summary collapse

DEFAULT_NAME =
'contrast.log'
DEFAULT_LEVEL =
::Ougai::Logging::Severity::INFO
VALID_LEVELS =
::Ougai::Logging::Severity::SEV_LABEL
STDOUT_STR =
'STDOUT'
STDERR_STR =
'STDERR'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Components::Interface

included

Constructor Details

#initializeLog

Returns a new instance of Log.



33
34
35
# File 'lib/contrast/logger/log.rb', line 33

def initialize
  update
end

Instance Attribute Details

#previous_levelObject (readonly)

Returns the value of attribute previous_level.



30
31
32
# File 'lib/contrast/logger/log.rb', line 30

def previous_level
  @previous_level
end

#previous_pathObject (readonly)

Returns the value of attribute previous_path.



30
31
32
# File 'lib/contrast/logger/log.rb', line 30

def previous_path
  @previous_path
end

Instance Method Details

#loggerObject



74
75
76
# File 'lib/contrast/logger/log.rb', line 74

def logger
  @_logger
end

#update(log_file = nil, log_level = nil) ⇒ Object

Given new settings from TeamServer, update our logging to use the new file and level, assuming they weren’t set by local configuration.

Parameters:

  • log_file (String) (defaults to: nil)

    the file to which to log

  • log_level (String) (defaults to: nil)

    the level at which to log



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/contrast/logger/log.rb', line 42

def update log_file = nil, log_level = nil
  config = CONFIG.root.agent.logger

  config_path  = config.path&.length.to_i.positive? ? config.path : nil
  config_level = config.level&.length&.positive? ? config.level : nil

  # config > settings > default
  path        = valid_path(config_path   || log_file)
  level_const = valid_level(config_level || log_level)

  path_change = path != previous_path
  level_change = level_const != previous_level

  # don't needlessly recreate logger
  return if @_logger && !(path_change || level_change)

  @previous_path  = path
  @previous_level = level_const

  @_logger = build(path: path, level_const: level_const)
  # If we're logging to a new path, then let's start it w/ our helpful
  # data gathering messages
  log_update if path_change
rescue StandardError => e
  if logger
    logger.error('Unable to process update to LoggerManager.', e)
  else
    puts 'Unable to process update to LoggerManager.'
    puts e.backtrace.join("\n")
  end
end

#write_permission?(path) ⇒ Boolean

StringIO is a valid path because it logs directly to a string buffer

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
# File 'lib/contrast/logger/log.rb', line 79

def write_permission? path
  return false if path.nil?
  return true if path.is_a?(StringIO)
  return File.writable?(path) if File.exist?(path)

  dir_name = File.dirname(File.absolute_path(path))
  File.writable?(dir_name)
end