Class: TTY::Logger
Overview
A class providing logging system
Constant Summary collapse
- ALL =
0
- INFO =
1
- DEBUG =
2
- WARN =
3
- ERROR =
4
- FATAL =
5
- OFF =
6
- MAX_LEVELS =
7
Instance Attribute Summary collapse
-
#level ⇒ Object
Returns the value of attribute level.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#timestamp_format ⇒ Object
readonly
Returns the value of attribute timestamp_format.
Class Method Summary collapse
-
.valid_level?(level) ⇒ Boolean
Verify valid level.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Logger
constructor
Initialize a Logger.
-
#log(message) ⇒ Object
Print formatted log to output.
-
#timestamp ⇒ Time
Create a timestamp.
- #validate_level(level) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Logger
Initialize a Logger
35 36 37 38 39 40 41 42 |
# File 'lib/tty/logger.rb', line 35 def initialize( = {}) @namespace = .fetch(:namespace) do fail ArgumentError, 'Logger must have namespace', caller end @output = .fetch(:output) { $stderr } @level = .fetch(:level) { ALL } @timestamp_format = .fetch(:timestamp_format) { '%Y-%m-%d %T' } end |
Instance Attribute Details
#level ⇒ Object
Returns the value of attribute level.
20 21 22 |
# File 'lib/tty/logger.rb', line 20 def level @level end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
18 19 20 |
# File 'lib/tty/logger.rb', line 18 def namespace @namespace end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
22 23 24 |
# File 'lib/tty/logger.rb', line 22 def output @output end |
#timestamp_format ⇒ Object (readonly)
Returns the value of attribute timestamp_format.
24 25 26 |
# File 'lib/tty/logger.rb', line 24 def @timestamp_format end |
Class Method Details
Instance Method Details
#log(message) ⇒ Object
Print formatted log to output
81 82 83 |
# File 'lib/tty/logger.rb', line 81 def log() output.print + ' - ' + end |
#timestamp ⇒ Time
Create a timestamp
54 55 56 |
# File 'lib/tty/logger.rb', line 54 def Time.now.strftime() end |
#validate_level(level) ⇒ Object
58 59 60 61 |
# File 'lib/tty/logger.rb', line 58 def validate_level(level) return if valid_level?(level) fail ArgumentError, "Log level must be 0..#{MAX_LEVELS}", caller end |