Class: Simplelogger
- Inherits:
-
Object
- Object
- Simplelogger
- Defined in:
- lib/simplelogger.rb
Instance Attribute Summary collapse
-
#color ⇒ Object
readonly
Returns the value of attribute color.
-
#console_style ⇒ Object
Returns the value of attribute console_style.
-
#enable_debug ⇒ Object
Returns the value of attribute enable_debug.
-
#enable_nocolor ⇒ Object
Returns the value of attribute enable_nocolor.
-
#enable_uuencode ⇒ Object
Returns the value of attribute enable_uuencode.
-
#enable_verbose ⇒ Object
Returns the value of attribute enable_verbose.
-
#hostname ⇒ Object
readonly
Returns the value of attribute hostname.
-
#logfile ⇒ Object
readonly
Returns the value of attribute logfile.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#program_name ⇒ Object
Returns the value of attribute program_name.
-
#time_format ⇒ Object
Returns the value of attribute time_format.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
- #_print(string) ⇒ Object
- #_puts(string) ⇒ Object
- #close ⇒ Object
- #current_time ⇒ Object
- #debug(string) ⇒ Object
- #error(string) ⇒ Object
- #fail(string) ⇒ Object
- #fatal(string) ⇒ Object
- #info(string) ⇒ Object
- #init(string) ⇒ Object
-
#initialize(opts = {}) ⇒ Simplelogger
constructor
A new instance of Simplelogger.
- #log(level, string) ⇒ Object
- #notice(string) ⇒ Object
- #stderr(string) ⇒ Object
- #stdout(string) ⇒ Object
- #success(string) ⇒ Object
- #underscore(string) ⇒ Object
- #uuencode(string) ⇒ Object
- #warning(string) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Simplelogger
Returns a new instance of Simplelogger.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/simplelogger.rb', line 17 def initialize(opts = {}) # Options: # :file => string # log file name # :program_name => string # program name # :enable_debug => boolean # display and log debug messages, default no logging to console & file # :enable_nocolor => boolean # disable colorful messages on console, default enabled # :enable_verbose => boolean # display messages on console (except debug), default messages are only logged to the log file # :enable_uuencode => boolean # uuencode log messages # :time_format => string # user time format, e.g. "%Y-%m-%d_%H-%M-%S", default set to iso8601 # # :console_style => string # 'full' -> display full log message on console # 'message' -> display log level + log message on console # default set to 'full' # raise "missing :file option" if not opts[:file] @logfile = File.new(opts[:file], "a") ; @logfile.sync = true @options = opts @color = Term::ANSIColor $stdout.sync = true @hostname = ENV['HOSTNAME'] || 'unknown_hostname' @user = Etc.getlogin || 'unknown_user' @time_format = opts[:time_format] @program_name = opts[:program_name] || 'unknown_program' @console_style = opts[:console_style] || 'full' @enable_verbose = opts[:enable_verbose] @enable_debug = opts[:enable_debug] @enable_uuencode = opts[:enable_uuencode] @enable_nocolor = opts[:enable_nocolor] end |
Instance Attribute Details
#color ⇒ Object (readonly)
Returns the value of attribute color.
14 15 16 |
# File 'lib/simplelogger.rb', line 14 def color @color end |
#console_style ⇒ Object
Returns the value of attribute console_style.
15 16 17 |
# File 'lib/simplelogger.rb', line 15 def console_style @console_style end |
#enable_debug ⇒ Object
Returns the value of attribute enable_debug.
15 16 17 |
# File 'lib/simplelogger.rb', line 15 def enable_debug @enable_debug end |
#enable_nocolor ⇒ Object
Returns the value of attribute enable_nocolor.
15 16 17 |
# File 'lib/simplelogger.rb', line 15 def enable_nocolor @enable_nocolor end |
#enable_uuencode ⇒ Object
Returns the value of attribute enable_uuencode.
15 16 17 |
# File 'lib/simplelogger.rb', line 15 def enable_uuencode @enable_uuencode end |
#enable_verbose ⇒ Object
Returns the value of attribute enable_verbose.
15 16 17 |
# File 'lib/simplelogger.rb', line 15 def enable_verbose @enable_verbose end |
#hostname ⇒ Object (readonly)
Returns the value of attribute hostname.
14 15 16 |
# File 'lib/simplelogger.rb', line 14 def hostname @hostname end |
#logfile ⇒ Object (readonly)
Returns the value of attribute logfile.
14 15 16 |
# File 'lib/simplelogger.rb', line 14 def logfile @logfile end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
14 15 16 |
# File 'lib/simplelogger.rb', line 14 def @options end |
#program_name ⇒ Object
Returns the value of attribute program_name.
15 16 17 |
# File 'lib/simplelogger.rb', line 15 def program_name @program_name end |
#time_format ⇒ Object
Returns the value of attribute time_format.
15 16 17 |
# File 'lib/simplelogger.rb', line 15 def time_format @time_format end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
14 15 16 |
# File 'lib/simplelogger.rb', line 14 def user @user end |
Instance Method Details
#_print(string) ⇒ Object
186 187 188 189 |
# File 'lib/simplelogger.rb', line 186 def _print(string) string = uuencode(string) print string end |
#_puts(string) ⇒ Object
181 182 183 184 |
# File 'lib/simplelogger.rb', line 181 def _puts(string) string = uuencode(string) puts string end |
#close ⇒ Object
58 59 60 |
# File 'lib/simplelogger.rb', line 58 def close logfile.close if logfile.closed? end |
#current_time ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/simplelogger.rb', line 62 def current_time if time_format Time.new.strftime(time_format) else Time.now.iso8601(5) end end |
#debug(string) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/simplelogger.rb', line 116 def debug(string) # Default debug messages are not logged, kept it out of 'log' if enable_debug string = uuencode(string) = "#{current_time} #{hostname} #{user} #{program_name} DEBUG: #{string}" logfile.puts = "DEBUG: #{string}" if console_style == 'message' if enable_nocolor puts else puts color.magenta end end end |
#error(string) ⇒ Object
136 137 138 139 |
# File 'lib/simplelogger.rb', line 136 def error(string) string = uuencode(string) log('ERROR',string) end |
#fail(string) ⇒ Object
161 162 163 164 |
# File 'lib/simplelogger.rb', line 161 def fail(string) string = uuencode(string) log('FAILURE',string) end |
#fatal(string) ⇒ Object
141 142 143 144 |
# File 'lib/simplelogger.rb', line 141 def fatal(string) string = uuencode(string) log('FATAL',string) end |
#info(string) ⇒ Object
111 112 113 114 |
# File 'lib/simplelogger.rb', line 111 def info(string) string = uuencode(string) log('INFO',string) end |
#init(string) ⇒ Object
151 152 153 154 |
# File 'lib/simplelogger.rb', line 151 def init(string) string = uuencode(string) log('INIT',string) end |
#log(level, string) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/simplelogger.rb', line 78 def log(level, string) string = uuencode(string) = "#{current_time} #{hostname} #{user} #{program_name} #{level}: #{string}" logfile.puts = "#{level}: #{string}" if console_style == 'message' if enable_verbose if enable_nocolor puts else case level when 'INFO' puts color.green when 'WARNING' puts color.yellow when 'ERROR' puts color.red when 'FATAL' puts color.red when 'NOTICE' puts color.blue when 'INIT' puts color.bold(color.blue()) when 'SUCCESS' puts color.green when 'FAILURE' puts color.red else puts end end end end |
#notice(string) ⇒ Object
146 147 148 149 |
# File 'lib/simplelogger.rb', line 146 def notice(string) string = uuencode(string) log('NOTICE',string) end |
#stderr(string) ⇒ Object
171 172 173 174 |
# File 'lib/simplelogger.rb', line 171 def stderr(string) string = uuencode(string) $stderr.write string end |
#stdout(string) ⇒ Object
176 177 178 179 |
# File 'lib/simplelogger.rb', line 176 def stdout(string) string = uuencode(string) $stdout.write string end |
#success(string) ⇒ Object
156 157 158 159 |
# File 'lib/simplelogger.rb', line 156 def success(string) string = uuencode(string) log('SUCCESS',string) end |
#underscore(string) ⇒ Object
166 167 168 169 |
# File 'lib/simplelogger.rb', line 166 def underscore(string) string = uuencode(string) puts color.underscore string end |
#uuencode(string) ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/simplelogger.rb', line 70 def uuencode(string) if enable_uuencode [string].pack("u") else string end end |
#warning(string) ⇒ Object
131 132 133 134 |
# File 'lib/simplelogger.rb', line 131 def warning(string) string = uuencode(string) log('WARNING',string) end |