Created
December 18, 2013 10:15
-
-
Save kryptykphysh/8020064 to your computer and use it in GitHub Desktop.
Messing about with 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
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