Class: ActivePostgres::Logger
- Inherits:
-
Object
- Object
- ActivePostgres::Logger
- Defined in:
- lib/active_postgres/logger.rb
Constant Summary collapse
- LEVELS =
{ debug: ::Logger::DEBUG, info: ::Logger::INFO, warn: ::Logger::WARN, error: ::Logger::ERROR, fatal: ::Logger::FATAL }.freeze
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#verbose ⇒ Object
readonly
Returns the value of attribute verbose.
Instance Method Summary collapse
- #debug(message) ⇒ Object
-
#diagnostic(title, content) ⇒ Object
Format diagnostic information nicely.
- #error(message) ⇒ Object
- #fatal(message) ⇒ Object
- #info(message) ⇒ Object
-
#initialize(verbose: false, log_file: nil) ⇒ Logger
constructor
A new instance of Logger.
- #progress(message) ⇒ Object
-
#section(title) ⇒ Object
Log a section header.
- #setup_logger(log_file) ⇒ Object
- #step(description) ⇒ Object
- #success(message) ⇒ Object
- #task(description) ⇒ Object
- #warn(message) ⇒ Object
Constructor Details
#initialize(verbose: false, log_file: nil) ⇒ Logger
Returns a new instance of Logger.
15 16 17 18 19 20 |
# File 'lib/active_postgres/logger.rb', line 15 def initialize(verbose: false, log_file: nil) @verbose = verbose @logger = setup_logger(log_file) @step_number = 0 @current_task = nil end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
13 14 15 |
# File 'lib/active_postgres/logger.rb', line 13 def logger @logger end |
#verbose ⇒ Object (readonly)
Returns the value of attribute verbose.
13 14 15 |
# File 'lib/active_postgres/logger.rb', line 13 def verbose @verbose end |
Instance Method Details
#debug(message) ⇒ Object
75 76 77 |
# File 'lib/active_postgres/logger.rb', line 75 def debug() logger.debug " #{sanitize()}" end |
#diagnostic(title, content) ⇒ Object
Format diagnostic information nicely
104 105 106 107 108 109 110 |
# File 'lib/active_postgres/logger.rb', line 104 def diagnostic(title, content) logger.debug "\n 📋 #{sanitize(title)}:" content.each_line do |line| logger.debug " #{sanitize(line.chomp)}" end logger.debug '' end |
#error(message) ⇒ Object
87 88 89 |
# File 'lib/active_postgres/logger.rb', line 87 def error() logger.error " #{sanitize()}" end |
#fatal(message) ⇒ Object
91 92 93 |
# File 'lib/active_postgres/logger.rb', line 91 def fatal() logger.fatal " #{sanitize()}" end |
#info(message) ⇒ Object
79 80 81 |
# File 'lib/active_postgres/logger.rb', line 79 def info() logger.info " #{sanitize()}" end |
#progress(message) ⇒ Object
99 100 101 |
# File 'lib/active_postgres/logger.rb', line 99 def progress() logger.info " ⏳ #{sanitize()}" end |
#section(title) ⇒ Object
Log a section header
113 114 115 116 117 |
# File 'lib/active_postgres/logger.rb', line 113 def section(title) logger.info sanitize("\n#{'=' * 60}") logger.info sanitize(title.center(60)) logger.info sanitize("#{'=' * 60}\n") end |
#setup_logger(log_file) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/active_postgres/logger.rb', line 22 def setup_logger(log_file) if log_file file_logger = ::Logger.new(log_file, 'daily') file_logger.level = ::Logger::DEBUG file_logger else ::Logger.new($stdout).tap do |l| l.level = verbose ? ::Logger::DEBUG : ::Logger::INFO l.formatter = proc do |severity, _datetime, _progname, msg| case severity when 'DEBUG' "#{msg}\n" if verbose when 'INFO' "#{msg}\n" when 'WARN' "⚠️ #{msg}\n" when 'ERROR' "❌ #{msg}\n" when 'FATAL' "💀 #{msg}\n" else "#{msg}\n" end end end end end |
#step(description) ⇒ Object
70 71 72 73 |
# File 'lib/active_postgres/logger.rb', line 70 def step(description) logger.info " → #{sanitize(description)}" yield if block_given? end |
#success(message) ⇒ Object
95 96 97 |
# File 'lib/active_postgres/logger.rb', line 95 def success() logger.info " ✅ #{sanitize()}" end |
#task(description) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/active_postgres/logger.rb', line 50 def task(description) @step_number += 1 @current_task = description logger.info "#{@step_number}. #{sanitize(description)}" start_time = Time.now result = yield duration = Time.now - start_time = "Completed in #{duration.round(2)}s" logger.debug " #{sanitize()}" result rescue StandardError => e = "Failed: #{e.}" logger.error " #{sanitize()}" raise ensure @current_task = nil end |
#warn(message) ⇒ Object
83 84 85 |
# File 'lib/active_postgres/logger.rb', line 83 def warn() logger.warn " #{sanitize()}" end |