monolog.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # Monolog, an easy to use logger for ruby
  2. module Monolog
  3. module Logger
  4. attr_reader :data
  5. def initialize(data = nil)
  6. @data = data
  7. end
  8. def log(file, line, name, level, format, *args)
  9. end
  10. def close
  11. @data.close if @data && @data.respond_to?(:close)
  12. end
  13. end
  14. class FileLogger
  15. include Logger
  16. def initialize(filename)
  17. @data = File.open(filename, "at")
  18. end
  19. def log(file, line, name, format, *args)
  20. @data.printf("%s: %s: %s: %d: ", Time.now.to_s, name, file, line)
  21. @data.printf(format, *args)
  22. @data.printf("\n")
  23. end
  24. end
  25. class StdinLogger < FileLogger
  26. def initialize
  27. @data = $stdin
  28. end
  29. end
  30. class StderrLogger < FileLogger
  31. def initialize
  32. @data = $stderr
  33. end
  34. end
  35. class Log
  36. attr_reader :loggers
  37. attr_reader :levels
  38. def initialize
  39. @loggers = []
  40. @levels = {}
  41. end
  42. def add_logger(logger)
  43. @loggers << logger
  44. end
  45. def enable_level(name)
  46. @levels[name.to_sym] = true
  47. end
  48. def disable_level(name)
  49. @levels[name.to_sym] = false
  50. end
  51. def log_va(file, line, name, format, *args)
  52. level = @levels[name.to_sym]
  53. return nil unless level
  54. @loggers.each do | logger |
  55. logger.log(file, line, name, format, *args)
  56. end
  57. end
  58. def close
  59. @loggers.each do | logger |
  60. logger.close()
  61. end
  62. end
  63. end
  64. def self.setup
  65. @log = Log.new
  66. end
  67. def self.get_log
  68. return @log
  69. end
  70. def self.setup_all(name = nil, err = true, out = false)
  71. setup
  72. add_stderr_logger if err
  73. add_stdout_logger if out
  74. add_file_logger(name) if name
  75. enable_level(:INFO)
  76. enable_level(:WARNING)
  77. enable_level(:ERROR)
  78. enable_level(:FATAL)
  79. end
  80. def self.enable_level(l)
  81. @log ||= nil
  82. return unless @log
  83. @log.enable_level(l)
  84. end
  85. def self.disable_level(l)
  86. @log ||= nil
  87. return unless @log
  88. @log.disable_level(l)
  89. end
  90. def self.add_logger(l)
  91. @log ||= nil
  92. return unless @log
  93. @log.add_logger(l)
  94. end
  95. def self.add_stdin_logger
  96. self.add_logger(StdinLogger.new)
  97. end
  98. def self.add_stderr_logger
  99. self.add_logger(StderrLogger.new)
  100. end
  101. def self.add_file_logger(filename = "log.log")
  102. self.add_logger(FileLogger.new(filename))
  103. end
  104. def self.close
  105. @log ||= nil
  106. return unless @log
  107. @log.close
  108. end
  109. def self.log_va(file, line, name, format, *args)
  110. @log ||= nil
  111. return unless @log
  112. @log.log_va(file, line, name, format, *args)
  113. end
  114. def log(name, format, * args)
  115. file, line, fun = caller.first.to_s.split(':')
  116. Monolog.log_va(file, line, name, format, *args)
  117. end
  118. def log_error(format, *args)
  119. file, line, fun = caller.first.to_s.split(':')
  120. Monolog.log_va(file, line, :ERROR, format, *args)
  121. end
  122. def log_warning(format, *args)
  123. file, line, fun = caller.first.to_s.split(':')
  124. Monolog.log_va(file, line, :WARNING, format, *args)
  125. end
  126. def log_info(format, *args)
  127. file, line, fun = caller.first.to_s.split(':')
  128. Monolog.log_va(file, line, :INFO, format, *args)
  129. end
  130. def log_debug(format, *args)
  131. file, line, fun = caller.first.to_s.split(':')
  132. Monolog.log_va(file, line, :DEBUG, format, *args)
  133. end
  134. def log_fatal(format, *args)
  135. file, line, fun = caller.first.to_s.split(':')
  136. Monolog.log_va(file, line, :FATAL, format, *args)
  137. end
  138. extend(self)
  139. end