Posted By

noah on 01/24/10


Tagged

time file ruby size notification logging log4r rollingfileoutputter


Versions (?)

Log4r: how to use RollingFileOutputter: automatically cycle stale logfiles


 / Published in: Bash
 

(Log4r::RollingFileOutputter)(http://log4r.sourceforge.net/rdoc/files/log4r/formatter/formatter_rb.html) automatically renames/archives log files once they get too big or too old.

This is not my code, I found it in Google's cache and I found it useful, so I figured I should preserve it. If you're the author, get in touch and I'll credit/link you (and thanks!)

  1. # How to use RollingFileOutputter
  2.  
  3. $: << "../src"
  4.  
  5. require 'log4r'
  6.  
  7. include Log4r
  8.  
  9. puts "this will take a while"
  10.  
  11. # example of log file being split by time constraint 'maxtime'
  12.  
  13. config = {
  14.  
  15. "filename" => "logs/TestTime.log",
  16.  
  17. "maxtime" => 10,
  18.  
  19. "trunc" => true
  20.  
  21. }
  22.  
  23. timeLog = Logger.new 'WbExplorer'
  24.  
  25. timeLog.outputters = RollingFileOutputter.new("WbExplorer", config)
  26.  
  27. timeLog.level = DEBUG
  28.  
  29. # log something once a second for 100 seconds
  30.  
  31. 100.times { |t|
  32.  
  33. timeLog.info "blah #{t}"
  34.  
  35. sleep(1.0)
  36.  
  37. }
  38.  
  39. # example of log file being split by space constraint 'maxsize'
  40.  
  41. config = {
  42.  
  43. "filename" => "logs/TestSize.log",
  44.  
  45. "maxsize" => 16000,
  46.  
  47. "trunc" => true
  48.  
  49. }
  50.  
  51. sizeLog = Logger.new 'WbExplorer'
  52.  
  53. sizeLog.outputters = RollingFileOutputter.new("WbExplorer", config)
  54.  
  55. sizeLog.level = DEBUG
  56.  
  57. # log a large number of times
  58.  
  59. 100000.times { |t|
  60.  
  61. sizeLog.info "blah #{t}"
  62.  
  63. }
  64.  
  65. puts "done! check the two sets of log files in logs/ (TestTime and TestSize)"

Report this snippet  

You need to login to post a comment.