Class: HLogger
- Inherits:
-
Object
- Object
- HLogger
- Defined in:
- lib/hengine/hlogger.rb
Constant Summary collapse
- HLOG_MAXSIZE =
log on screen if filename is stdout
10
Instance Method Summary collapse
-
#<<(str, level = 'DEBUG') ⇒ Object
hlogger << “hello world” hlogger.<<(“hello world”, “DEBUG”) # warning: if i put some space doesn’t work.
- #_levels ⇒ Object
- #_toNum ⇒ Object
- #close ⇒ Object
-
#initialize(filename, level: 'DEBUG') ⇒ HLogger
constructor
10MB.
- #level(value) ⇒ Object
- #timestamp ⇒ Object
- #toNum(strLevel) ⇒ Object
-
#write(str, level) ⇒ Object
You can use this function more beautiful than .<<(…).
Constructor Details
#initialize(filename, level: 'DEBUG') ⇒ HLogger
10MB
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/hengine/hlogger.rb', line 7 def initialize(filename, level: 'DEBUG') @levels = self._levels @levelToNum = self._toNum @level = @levelToNum[level] return if(filename == "stdout") filename = "logger.txt" if(filename == nil) if (File.exist?(filename) and File.size(filename).to_f / 1000000 >= HLOG_MAXSIZE) = DateTime.now.strftime "%d-%m-%Y-%H.%M.%S" File.rename(filename, "#{filename}.#{}") end @file = File.new(filename, "a+") end |
Instance Method Details
#<<(str, level = 'DEBUG') ⇒ Object
hlogger << “hello world” hlogger.<<(“hello world”, “DEBUG”) # warning: if i put some space doesn’t work
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/hengine/hlogger.rb', line 68 def << (str, level = 'DEBUG') level = self.toNum(level) if(level.class == String) return if(level > @level) t = self. strLevel = self.level(level) if(@file) @file << "[#{strLevel} #{t}]$ #{str}\n" else case self.level(level) when "ERROR" puts "[#{strLevel} hypersonic]$ #{str}".red when "FATAL" puts "[#{strLevel} hypersonic]$ #{str}".red when "DEBUG" puts "[#{strLevel} hypersonic]$ #{str}".green when "WARNING" puts "[#{strLevel} hypersonic]$ #{str}".hight_cyan when "INFO" puts "[#{strLevel} hypersonic]$ #{str}".hight_purple else puts "[#{strLevel} hypersonic]$ #{str}".white end end return self end |
#_levels ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/hengine/hlogger.rb', line 20 def _levels levels = [] levels << "OFF" levels << "FATAL" levels << "ERROR" levels << "WARNING" levels << "INFO" levels << "DEBUG" levels << "DEBUG2" levels << "DEBUG3" levels << "DEBUG4" levels << "ALL" return levels end |
#_toNum ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/hengine/hlogger.rb', line 36 def _toNum levelToNum = {} self._levels.each_with_index do |level, i| levelToNum[level] = i end return levelToNum end |
#close ⇒ Object
57 58 59 |
# File 'lib/hengine/hlogger.rb', line 57 def close @file.close if(@file) end |
#level(value) ⇒ Object
51 52 53 54 55 |
# File 'lib/hengine/hlogger.rb', line 51 def level(value) return @levels[value] end |
#timestamp ⇒ Object
61 62 63 |
# File 'lib/hengine/hlogger.rb', line 61 def return DateTime.now.strftime "%d-%m-%Y %H:%M:%S" end |
#toNum(strLevel) ⇒ Object
47 48 49 |
# File 'lib/hengine/hlogger.rb', line 47 def toNum(strLevel) return @levelToNum[strLevel] end |
#write(str, level) ⇒ Object
You can use this function more beautiful than .<<(…)
101 102 103 |
# File 'lib/hengine/hlogger.rb', line 101 def write(str, level) self.<<(str, level) end |