cologger 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +47 -2
- data/lib/cologger.rb +48 -15
- metadata +1 -1
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
## Installation
|
5
5
|
This utils is packed as a gem, so, simply run `gem install cologger` to install it.
|
6
6
|
|
7
|
-
Current version is 0.1.
|
7
|
+
Current version is 0.1.3
|
8
8
|
|
9
9
|
## Usage
|
10
10
|
Using cologger is quite easy && straight forward.
|
@@ -49,10 +49,54 @@ Same as ruby `logger`, there are 5 levels. Namely,
|
|
49
49
|
|
50
50
|
The levels are ordered desendingly according to thier level of importance.
|
51
51
|
|
52
|
+
## Color schema
|
53
|
+
If you don't like our default color schema, which is `{ fatal: "red", error: "magenta", warn: "yellow", info: "green", debug: "cyan" }`, you can customize your own schema.
|
54
|
+
|
55
|
+
To do so, you can add the following code before start logging:
|
56
|
+
|
57
|
+
```
|
58
|
+
new_color_hash = {
|
59
|
+
fatal: "green",
|
60
|
+
error: "NotAColor"
|
61
|
+
}
|
62
|
+
```
|
63
|
+
|
64
|
+
After that, apply this schema to your logger like this:
|
65
|
+
|
66
|
+
```
|
67
|
+
logger.update_colors new_color_hash
|
68
|
+
```
|
69
|
+
|
70
|
+
Now, if you do logging, here's what you'll get:
|
71
|
+
|
72
|
+

|
73
|
+
|
74
|
+
Notice that, when encountered an unknown color, the logger will complain about it in `red` color, and ignore this item. The same is true for the key, which is log level.
|
75
|
+
|
76
|
+
### Possible colors
|
77
|
+
Since Cologger is built upon the cute `colored` gem, the colors we support is listed below:
|
78
|
+
|
79
|
+
- black
|
80
|
+
- red
|
81
|
+
- green
|
82
|
+
- yellow
|
83
|
+
- blue
|
84
|
+
- magenta
|
85
|
+
- cyan
|
86
|
+
- white
|
87
|
+
|
88
|
+
### Logging levels
|
89
|
+
In order to make your life easier, we choose to use simple word for the levels, the levels are listed below:
|
90
|
+
|
91
|
+
- fatal
|
92
|
+
- error
|
93
|
+
- warn
|
94
|
+
- info
|
95
|
+
- debug
|
96
|
+
|
52
97
|
## TODO
|
53
98
|
- Support file as output dev
|
54
99
|
- Customize log format
|
55
|
-
- Customize log level color
|
56
100
|
|
57
101
|
|
58
102
|
## Dependency
|
@@ -62,6 +106,7 @@ This gem depends on the cute [`colored' gem](https://github.com/defunkt/colored)
|
|
62
106
|
Thanks sir!
|
63
107
|
|
64
108
|
## Change log
|
109
|
+
- 0.1.3, you can use `update_colors` method to change the color schema
|
65
110
|
- 0.1.2, remove `tag` according to [@zhf](https://github.com/zhf)'s advice. User can add tag to content by themself when needed.
|
66
111
|
- 0.1.1, use `\t` to align timestamp.
|
67
112
|
- 0.1.0, initial version.
|
data/lib/cologger.rb
CHANGED
@@ -20,27 +20,34 @@ class Cologger
|
|
20
20
|
# low-level information for developers
|
21
21
|
LEVEL_DEBUG = 4
|
22
22
|
|
23
|
-
# fatal in red
|
24
|
-
COLOR_FATAL = 'red'
|
25
|
-
|
26
|
-
# error in magenta
|
27
|
-
COLOR_ERROR = 'magenta'
|
28
|
-
|
29
|
-
# warn in yellow
|
30
|
-
COLOR_WARN = 'yellow'
|
31
|
-
|
32
|
-
# info in green
|
33
|
-
COLOR_INFO = 'green'
|
34
|
-
|
35
|
-
# debug in cyan
|
36
|
-
COLOR_DEBUG = 'cyan'
|
37
|
-
|
38
23
|
attr_accessor :log_level
|
39
24
|
|
25
|
+
# Set alias for const methods, to override the behavior
|
26
|
+
class << self
|
27
|
+
alias :old_const_get :const_get
|
28
|
+
alias :old_const_set :const_set
|
29
|
+
end
|
30
|
+
|
31
|
+
# public methods
|
40
32
|
def initialize
|
41
33
|
@log_level = LEVEL_DEBUG # default to log everything
|
42
34
|
end
|
43
35
|
|
36
|
+
# Pass in a hash that contains the LEVEL to color hash
|
37
|
+
def update_colors color_hash
|
38
|
+
color_hash.each_pair do |sym, color|
|
39
|
+
if respond_to? sym.to_sym
|
40
|
+
if String::COLORS.has_key? color
|
41
|
+
Cologger.const_set((sym_for sym.to_sym, "COLOR"), color)
|
42
|
+
else
|
43
|
+
puts "Unknown color #{color} for #{sym}, ignord".red
|
44
|
+
end
|
45
|
+
else
|
46
|
+
puts "Unknown log level #{sym}, ignored".red
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
44
51
|
def method_missing(method, *args, &block)
|
45
52
|
if respond_to? method
|
46
53
|
log(method, *args)
|
@@ -51,7 +58,33 @@ class Cologger
|
|
51
58
|
Cologger.constants.include? sym_for(method, "LEVEL")
|
52
59
|
end
|
53
60
|
|
61
|
+
def self.const_get const, inherit=true
|
62
|
+
if const =~ /COLOR_(.+)/
|
63
|
+
level = $1.downcase.to_sym
|
64
|
+
@@color_hash[level]
|
65
|
+
else
|
66
|
+
self.old_const_get const, inherit
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.const_set const, value
|
71
|
+
if const =~ /COLOR_(.+)/
|
72
|
+
level = $1.downcase.to_sym
|
73
|
+
@@color_hash[level] = value
|
74
|
+
else
|
75
|
+
self.old_const_set const, value
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
54
79
|
private
|
80
|
+
@@color_hash = {
|
81
|
+
fatal: "red",
|
82
|
+
error: "magenta",
|
83
|
+
warn: "yellow",
|
84
|
+
info: "green",
|
85
|
+
debug: "cyan"
|
86
|
+
}
|
87
|
+
|
55
88
|
def sym_for method, prefix = nil
|
56
89
|
str = "#{method.to_s.upcase}"
|
57
90
|
str = "#{prefix}_#{str}" if prefix
|