cologger 0.1.0

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.
Files changed (4) hide show
  1. data/LICENSE +20 -0
  2. data/README.md +65 -0
  3. data/lib/cologger.rb +76 -0
  4. metadata +81 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Peng Sun
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ Software), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ #Cologger
2
+ *Add some color to your logs!*
3
+
4
+ ## Installation
5
+ This utils is packed as a gem, so, simply run `gem install cologger` to install it.
6
+
7
+ Current version is 0.1.0
8
+
9
+ ## Usage
10
+ Using cologger is quite easy && straight forward.
11
+
12
+ Start by creating a new logger
13
+
14
+ ```
15
+ logger = Cologger.new
16
+ ```
17
+
18
+ Next, set the log level you want. By default, cologger outputs all the logs to your console, which means setting the level to `LEVEL_DEBUG`. For more info about level, check out the `Log levels` section below.
19
+
20
+ ```
21
+ logger.log_level = Cologger.LEVEL_DEBUG
22
+ ```
23
+
24
+ After that, you can start logging. For example,
25
+
26
+ ```
27
+ logger.fatal "universe", "the world is crashing"
28
+ logger.error "brain", "what is going on here?"
29
+ logger.warn "road", "watch your step sir"
30
+ logger.info "FYI", "the big bang theory is funny"
31
+ logger.debug "program", "anything still working?"
32
+ ```
33
+
34
+ And, here's what you got:
35
+
36
+ ![colorful log](Screenshot.png)
37
+
38
+ ## Log
39
+ The first parameter of any log method, like the `universe` in the `fatal` log above, is taken as a tag, the other strings, concated by a space character, is treated as the log content.
40
+
41
+ ## Log levels
42
+ Same as ruby `logger`, there are 5 levels. Namely,
43
+
44
+ - FATAL, an unhandleable error that results in a program crash
45
+ - ERROR, a handleable error condition
46
+ - WARN, a warning
47
+ - INFO, generic (useful) information about system operation
48
+ - DEBUG, low-level information for developers
49
+
50
+ The levels are ordered desendingly according to thier level of importance.
51
+
52
+ ## TODO
53
+ - Support file as output dev
54
+ - Customize log format
55
+ - Customize log level color
56
+
57
+
58
+ ## Dependency
59
+
60
+ This gem depends on the cute [`colored' gem](https://github.com/defunkt/colored) by [defunk](https://github.com/defunkt).
61
+
62
+ Thanks sir!
63
+
64
+ ## Change log
65
+ - 0.1.0, initial version.
data/lib/cologger.rb ADDED
@@ -0,0 +1,76 @@
1
+ require 'colored'
2
+ require 'date'
3
+
4
+ # Extending ruby's logger by adding some color in it!
5
+ #
6
+ class Cologger
7
+
8
+ # an unhandleable error that results in a program crash
9
+ LEVEL_FATAL = 0
10
+
11
+ # a handleable error condition
12
+ LEVEL_ERROR = 1
13
+
14
+ # a warning
15
+ LEVEL_WARN = 2
16
+
17
+ # generic (useful) information about system operation
18
+ LEVEL_INFO = 3
19
+
20
+ # low-level information for developers
21
+ LEVEL_DEBUG = 4
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
+ attr_accessor :log_level
39
+
40
+ def initialize
41
+ @log_level = LEVEL_DEBUG # default to log everything
42
+ end
43
+
44
+ def method_missing(method, *args, &block)
45
+ if respond_to? method
46
+ log(method, *args)
47
+ end
48
+ end
49
+
50
+ def respond_to? (method, include_all=false)
51
+ Cologger.constants.include? sym_for(method, "LEVEL")
52
+ end
53
+
54
+ private
55
+ def sym_for method, prefix = nil
56
+ str = "#{method.to_s.upcase}"
57
+ str = "#{prefix}_#{str}" if prefix
58
+ str.to_sym
59
+ end
60
+
61
+ def log(method, *args)
62
+ level = Cologger.const_get(sym_for method, "LEVEL")
63
+ color = Cologger.const_get(sym_for method, "COLOR")
64
+
65
+ return unless level <= @log_level
66
+
67
+ tag, *content_array = args
68
+ content = content_array.join(" ")
69
+ log = "#{sym_for method} "
70
+ log += "[#{DateTime.now.iso8601}] "
71
+ log += "[#{tag}] #{content}"
72
+
73
+ puts log.send(color) # TODO fixme!
74
+ end
75
+
76
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cologger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Peng Sun
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: colored
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.2'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.2'
30
+ - !ruby/object:Gem::Dependency
31
+ name: colored
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.2'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.2'
46
+ description: ! " Simple log util that adds some colors to your logs.\n\n For more
47
+ information, please visit 'https://github.com/void-main/Cologger'\n"
48
+ email: voidmain1313113@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files:
52
+ - README.md
53
+ files:
54
+ - README.md
55
+ - LICENSE
56
+ - lib/cologger.rb
57
+ homepage: http://github.com/void-main/Cologger
58
+ licenses: []
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 1.8.24
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Log with color!
81
+ test_files: []