Created
November 6, 2018 07:31
-
-
Save midhunkrishna/4f1f13638c22513344e298f06da917c3 to your computer and use it in GitHub Desktop.
simple ruby threadsafe logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage: | |
# DebugTools::Logger.log(String) | |
require 'fileutils' | |
module DebugTools | |
class Logger | |
def self.log(message) | |
FileUtils.touch(path) unless File.exist?(path) | |
lock(path) do |file| | |
file.puts(message) | |
end | |
end | |
def self.lock(path, &block) | |
File.open(path, 'a') do | file | | |
file.flock(File::LOCK_EX) | |
block.call(file) | |
file.flock(File::LOCK_UN) | |
end | |
end | |
def self.path | |
"#{Rails.root}/log/debugtools.log" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment