Skip to content

Instantly share code, notes, and snippets.

@midhunkrishna
Created November 6, 2018 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save midhunkrishna/4f1f13638c22513344e298f06da917c3 to your computer and use it in GitHub Desktop.
Save midhunkrishna/4f1f13638c22513344e298f06da917c3 to your computer and use it in GitHub Desktop.
simple ruby threadsafe logger
# 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