Module: Logging

Defined in:
lib/opensecret/plugins.io/logs/logging.rb

Overview

– – [MIXIN] magic is deployed to hand out DevOps quality logging – features to any class that includes the logging module. – – When logging facilities are not ready we need to log just to – stdout but when they are we need to use them. – – mixin power enables one class to give the logfile path and all – classes will suddenly retrieve a another logger and use that. – – include Logging – def doImportant – log.warn(ere) “unhappy about doing this” – do_anyway – log.debug(ere) “all good it was okay” – end – – ———————– – What are Mixins? – ———————– – – Refer to the below link for excellent coverage of mixins. – ruby-doc.com/docs/ProgrammingRuby/html/tut_modules.html

Constant Summary collapse

@@log_path =
nil
@@log_class =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.initializr_strObject

– – Get the logger class initiaze string. – The OS platform “Windows” vs “Linux” dictates – what is returned as the “tee” command used to – redirect logging output bot the console and a – standard log file is not available on Windows. – – Return the initializer for the logging statement. – – ———— – Windows – ———— – – Continue logging solely to STANDARD OUT. – We could log to a file but not to both. – – ———— – Linux – ———— – – Send back TEE command so that logs pipe to – both standard out and the designated logfile. –



137
138
139
140
141
142
# File 'lib/opensecret/plugins.io/logs/logging.rb', line 137

def self.initializr_str

  return STDOUT if Gem.win_platform?
  return "| tee #{@@log_path}"

end

Instance Method Details

#ereObject

– – log.info(ere) woz ere – – [ere] is borrowed from the common graffiti phrase (i woz ere) – – In software terms it allows the logger to print 3 crucial – pieces of information for the troubleshooter (detective) to – determine who called the logger (who was here). The – – [1] - [module name] of the caller – [2] - [method name] the call came from – [3] - [line number] of the call – –



62
63
64
65
66
67
68
69
70
# File 'lib/opensecret/plugins.io/logs/logging.rb', line 62

def ere

  module_name = File.basename caller_locations(1,1).first.absolute_path, ".rb"
  method_name = caller_locations(1,1).first.base_label
  line_number = caller_locations(1,1).first.lineno

  "#{module_name} | #{method_name} | (line #{line_number}) "

end

#get_loggerObject

– – If logger is used at the [BEGINNING] when – the runtime logfile path has not yet been – ascertained then STDOUT is used. – – However if we have been informed (via the – set_logfile_path) that the logfile path is – now known the logger returned will be set – to log to the specified file. – – [memoization] should be used so that this – method gets called only [TWICE]. – – [1] - at the [beginning] when the logfile path is not known – [2] - just [after] the logfile dir is evaluated and created –



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/opensecret/plugins.io/logs/logging.rb', line 88

def get_logger

  expensive_initializr = "EXPENSIVE(x2) => This log message should only appear [TWICE]."
  Logger.new(STDOUT).warn "#{expensive_initializr}"

  return get_stdout_logger if @@log_path.nil?

  file_logger = Logger.new( Logging.initializr_str )
  original_formatter = Logger::Formatter.new

  file_logger.formatter = proc { |severity, datetime, progname, msg|
########      original_formatter.call(severity, datetime, progname, msg.dump.chomp.strip.delete("\"\\"))
    original_formatter.call( severity, datetime, progname, msg.dump.chomp.strip )
  }

  if CmdLine.has? "--debug" then
    file_logger.level = Logger::DEBUG
  else
    file_logger.level = Logger::INFO
  end

  return file_logger

end

#get_stdout_loggerObject

– – Get a simple STDOUT logger using the – designated log level. – – As the early stage when we do not have a – log file destination the logs are set to – the DEBUG (max) level. – – If you need just INFO level logs right – from the get go - go amend this method. –



156
157
158
159
160
161
162
# File 'lib/opensecret/plugins.io/logs/logging.rb', line 156

def get_stdout_logger

  stdout_logger = Logger.new(STDOUT)
  stdout_logger.level = Logger::DEBUG
  return stdout_logger

end

#logObject

– – Classes that include (MIXIN) this logging module will – have access to this logger method. – – [memoization] is implemented here for performance and – will only initiate a logger under 2 circumstances – – [1] - the first call (returns a STDOUT logger) – [2] - the call after the logfile path is set – (returns a more sophisticated logger) –



41
42
43
44
45
# File 'lib/opensecret/plugins.io/logs/logging.rb', line 41

def log

  @@log_class ||= get_logger

end

#nickname(object_path) ⇒ Object

– – Return the filename and its two immediate – parent folders (parent and grandparent). – – When this is not possible when the filepath – is near the filesystem’s root - it returns – whatever is possible. – – IMPLEMENT ABOVE FUNCTIONALITY WHEN NEEDED. – CURRENTLY IT JUST BLUNDERS IN WITH NEITHER – RHYME NOR REASON. –



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/opensecret/plugins.io/logs/logging.rb', line 190

def nickname object_path

  object_name   = File.basename object_path
  parent_folder = File.dirname  object_path
  parent_name   = File.basename parent_folder
  granny_folder = File.dirname  parent_folder
  granny_name   = File.basename granny_folder

  return [granny_name,parent_name,object_name].join("/")

end

#set_logfile_path(path_to_set) ⇒ Object

– – Set logfile path and [INVALIDATE] the – cache so that memoization reads the new – @log_class object. –



170
171
172
173
174
175
# File 'lib/opensecret/plugins.io/logs/logging.rb', line 170

def set_logfile_path path_to_set

  @@log_path = path_to_set
  @@log_class = nil

end