Skip to content

Instantly share code, notes, and snippets.

@kryptykphysh
Created December 18, 2013 10:15
Show Gist options
  • Save kryptykphysh/8020064 to your computer and use it in GitHub Desktop.
Save kryptykphysh/8020064 to your computer and use it in GitHub Desktop.
Messing about with Logger
require 'logger'
# Create new log file, or append to existing one.
log_file = File.open('log_test.txt', File::WRONLY | File::APPEND)
# Create log to rotate at 1MB and keep 10 logs.
log = Logger.new(log_file, 10, 1024000)
# Set the program name for logging purposes.
log.progname = 'Log Test'
# Change the log format to include the program name.
log.formatter = proc do |severity, datetime, progname, msg|
"#{datetime}: #{progname}: #{severity} - #{msg}\n"
end
# Test different log messages.
log.fatal { "It's all gone wrong." }
log.error { "Now it's just an error." }
log.info { "Have some information." }
log.warn { "This is just a warning."}
log.debug { "Bugs! Bugs everywhere!" }
# Change logging level
log.sev_threshold = Logger::ERROR
# So this should still be logged.
log.error { "OMG! An error!" }
# But this shouldn't.
log.info { "More spurious info." }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment