Class: UpdateRepo::Logger
- Inherits:
-
Object
- Object
- UpdateRepo::Logger
- Includes:
- Helpers
- Defined in:
- lib/update_repo/logger.rb
Overview
Class : Logger. This class encapsulates printing to screen and logging to file if requried.
Instance Method Summary collapse
-
#close ⇒ void
close the logfile, if it exists.
-
#generate_filename ⇒ string
generate a filename for the log, with or without a timestamp.
-
#initialize(enabled, timestamp) ⇒ void
constructor
Constructor for the Logger class.
-
#output(*string) ⇒ void
this function will simply pass the given string to ‘print’, and also log to file if that is specified.
Methods included from Helpers
Constructor Details
#initialize(enabled, timestamp) ⇒ void
Constructor for the Logger class.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/update_repo/logger.rb', line 16 def initialize(enabled, ) @settings = { enabled: enabled, timestamp: } # don't prepare a logfile unless it's been requested. return unless @settings[:enabled] # generate a filename depending on 'timestamp' setting. filename = generate_filename # open the logfile and set sync mode. @logfile = File.open(filename, 'w') @logfile.sync = true end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
close the logfile, if it exists
53 54 55 |
# File 'lib/update_repo/logger.rb', line 53 def close @logfile.close if @logfile end |
#generate_filename ⇒ string
generate a filename for the log, with or without a timestamp
30 31 32 33 34 35 36 |
# File 'lib/update_repo/logger.rb', line 30 def generate_filename if @settings[:timestamp] 'updaterepo-' + Time.new.strftime('%y%m%d-%H%M%S') + '.log' else 'updaterepo.log' end end |
#output(*string) ⇒ void
This method returns an undefined value.
this function will simply pass the given string to ‘print’, and also log to file if that is specified.
42 43 44 45 46 47 48 |
# File 'lib/update_repo/logger.rb', line 42 def output(*string) # log to screen regardless print(*string) # log to file if that has been enabled return unless @settings[:enabled] @logfile.write(string.join('').gsub(/\e\[(\d+)(;\d+)*m/, '')) end |