Class: LoggedTask::TaskLogger
- Inherits:
-
Object
- Object
- LoggedTask::TaskLogger
- Defined in:
- lib/logged_task.rb
Instance Method Summary collapse
- #error(msg, object = nil) ⇒ Object
- #info(msg, object = nil) ⇒ Object
-
#initialize(task_full_name) ⇒ TaskLogger
constructor
A new instance of TaskLogger.
- #summary ⇒ Object
- #time_str ⇒ String private
- #warn(msg, object = nil) ⇒ Object
- #write(msg, object, color = nil) ⇒ Object private
- #write_file(fd, msg, object, plain) ⇒ Object private
Constructor Details
#initialize(task_full_name) ⇒ TaskLogger
Returns a new instance of TaskLogger.
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/logged_task.rb', line 43 def initialize(task_full_name) names = task_full_name.split(':') task_name = names.pop path = Rails.root.join('log', 'rake_tasks') FileUtils.mkdir_p(path) time = Time.now.strftime('%Y-%m-%d_%H%M%S.%N') @log_file = File.new(path.join("#{time}-#{names.join(".")}.#{task_name}.log"), 'w') @warns_and_errors = [] @task_full_name = task_full_name end |
Instance Method Details
#error(msg, object = nil) ⇒ Object
71 72 73 74 75 |
# File 'lib/logged_task.rb', line 71 def error(msg, object=nil) msg = "[ERROR]#{time_str}: #{msg}" @warns_and_errors << { msg:, color: :red } write(msg, object, :red) end |
#info(msg, object = nil) ⇒ Object
57 58 59 |
# File 'lib/logged_task.rb', line 57 def info(msg, object=nil) write("[INFO]#{time_str}: #{msg}", object) end |
#summary ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/logged_task.rb', line 77 def summary # TODO: Write all warnings and errors together write("=== Summary of warnings and errors for task #{@task_full_name} ===", nil) if @warns_and_errors.empty? write('(NONE)', nil) else @warns_and_errors.each { | e | write(e[:msg], nil, e[:color]) } end write('', nil) end |
#time_str ⇒ String (private)
91 92 93 |
# File 'lib/logged_task.rb', line 91 def time_str Time.now.strftime('%Y-%m-%d %H:%M:%S.%3N') end |
#warn(msg, object = nil) ⇒ Object
63 64 65 66 67 |
# File 'lib/logged_task.rb', line 63 def warn(msg, object=nil) msg = "[WARN]#{time_str}: #{msg}" @warns_and_errors << { msg:, color: :yellow} write(msg, object, :yellow) end |
#write(msg, object, color = nil) ⇒ Object (private)
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/logged_task.rb', line 98 def write(msg, object, color=nil) write_file(@log_file, msg, object, true) @log_file.fsync if !color.nil? msg = Rainbow(msg).send(:color) end write_file($stdout, msg, object, false) end |
#write_file(fd, msg, object, plain) ⇒ Object (private)
113 114 115 116 |
# File 'lib/logged_task.rb', line 113 def write_file(fd, msg, object, plain) fd.puts msg fd.puts object.ai(plain: plain) unless object.nil? end |