awesome_logger 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +14 -0
- data/README.md +34 -0
- data/Rakefile +3 -0
- data/awesome_logger.gemspec +22 -0
- data/lib/awesome_logger.rb +51 -0
- data/lib/awesome_logger/formatter.rb +5 -0
- data/lib/awesome_logger/formatters/console.rb +26 -0
- data/lib/awesome_logger/formatters/json.rb +7 -0
- data/lib/awesome_logger/version.rb +3 -0
- data/spec/awesome_logger_spec.rb +58 -0
- data/spec/console_formatter_spec.rb +27 -0
- data/spec/spec_helper.rb +4 -0
- metadata +95 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
Copyright (c) 2012 Robin Liao
|
2
|
+
|
3
|
+
This program is free software: you can redistribute it and/or modify
|
4
|
+
it under the terms of the GNU General Public License as published by
|
5
|
+
the Free Software Foundation, either version 3 of the License, or
|
6
|
+
(at your option) any later version.
|
7
|
+
|
8
|
+
This program is distributed in the hope that it will be useful,
|
9
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
11
|
+
GNU General Public License for more details.
|
12
|
+
|
13
|
+
You should have received a copy of the GNU General Public License
|
14
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# AwesomeLogger
|
2
|
+
|
3
|
+
A Logger API compliant logger that will keep track of things like log levels and time stamps for you. Use case is more for small chunks of activity logging that belongs together, usually for dumping into JSON and writing out to a file, printing on the terminal, or even writing to a database for tracking.
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'awesome_logger'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install awesome_logger
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
logger = AwesomeLogger.new
|
23
|
+
|
24
|
+
logger.log "Something happened"
|
25
|
+
logger.fatal "Something terrible happened"
|
26
|
+
|
27
|
+
|
28
|
+
## Contributing
|
29
|
+
|
30
|
+
1. Fork it
|
31
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
32
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
33
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
34
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'awesome_logger/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "awesome_logger"
|
8
|
+
gem.version = AwesomeLogger::VERSION
|
9
|
+
gem.authors = ["Robin Liao"]
|
10
|
+
gem.email = ["randuin@gmail.com"]
|
11
|
+
gem.description = %q{Awesome logging}
|
12
|
+
gem.summary = %q{Awesome logging}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_runtime_dependency 'json'
|
21
|
+
gem.add_development_dependency 'rspec'
|
22
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require "awesome_logger/version"
|
2
|
+
require "awesome_logger/formatter"
|
3
|
+
require "awesome_logger/formatters/console"
|
4
|
+
require "awesome_logger/formatters/json"
|
5
|
+
require "active_support/inflector"
|
6
|
+
require 'json'
|
7
|
+
|
8
|
+
class AwesomeLogger
|
9
|
+
LOGGER_LEVELS = [ :fatal, :error, :warn, :info, :debug ]
|
10
|
+
|
11
|
+
attr_accessor :logs
|
12
|
+
attr_reader :formatter
|
13
|
+
|
14
|
+
LOGGER_LEVELS.each do |level|
|
15
|
+
define_method level do |message|
|
16
|
+
log message, level
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize
|
21
|
+
@logs = []
|
22
|
+
end
|
23
|
+
|
24
|
+
def formatter= symbol
|
25
|
+
@formatter = AwesomeLogger.get_formatter_by_symbol symbol
|
26
|
+
end
|
27
|
+
|
28
|
+
def log(msg, level = :info)
|
29
|
+
if msg.is_a?(AwesomeLogger)
|
30
|
+
@logs += msg.logs
|
31
|
+
else
|
32
|
+
@logs << { :time => Time.now, :message => msg, :level => level }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
alias_method :<<, :log
|
37
|
+
|
38
|
+
def flush_to_logger logger
|
39
|
+
@formatter.flush_to_logger logger, @logs
|
40
|
+
end
|
41
|
+
|
42
|
+
def output
|
43
|
+
self.formatter.to_output @logs
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.get_formatter_by_symbol symbol
|
47
|
+
const_get symbol.to_s.camelize.to_sym
|
48
|
+
rescue NameError
|
49
|
+
nil
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class AwesomeLogger
|
2
|
+
class Console < Formatter
|
3
|
+
def self.to_output logs
|
4
|
+
logs.map do |l|
|
5
|
+
wrap_line l
|
6
|
+
end.join("\n")
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.wrap_line l
|
10
|
+
"[#{ Console.red l[:time]}]\t[#{ Console.green l[:level]}]\t#{l[:message]}"
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.flush_to_logger logger, logs
|
14
|
+
logs.each do |l|
|
15
|
+
logger.send l[:level], wrap_line(l)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.colorize text, color_code
|
20
|
+
"#{color_code}#{text}\e[0m"
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.red text; colorize text, "\e[31m"; end
|
24
|
+
def self.green text; colorize text, "\e[32m"; end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'awesome_logger'
|
3
|
+
|
4
|
+
describe AwesomeLogger do
|
5
|
+
before :each do
|
6
|
+
@logger = AwesomeLogger.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should initialize with proper logs instance variable' do
|
10
|
+
@logger.logs.should be_a Array
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should set the formatter correctly' do
|
14
|
+
@logger.formatter = :console
|
15
|
+
@logger.formatter.should be AwesomeLogger::Console
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should have proper logger api methods' do
|
19
|
+
@logger.should respond_to :warn
|
20
|
+
@logger.should respond_to :fatal
|
21
|
+
@logger.should respond_to :error
|
22
|
+
@logger.should respond_to :info
|
23
|
+
@logger.should respond_to :debug
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should log message with proper log level' do
|
27
|
+
@logger.info 'hi'
|
28
|
+
@logger.logs.last[:level].should be :info
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should add logs with info as the default level' do
|
32
|
+
@logger.log 'hi'
|
33
|
+
@logger.logs.last[:level].should eq :info
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should have the aliased << method for loggign' do
|
37
|
+
@logger << 'aliased'
|
38
|
+
@logger.logs.last[:message].should eq 'aliased'
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should concact another logger onto the current one' do
|
42
|
+
new_logger = AwesomeLogger.new
|
43
|
+
new_logger.log 'another message'
|
44
|
+
@logger << new_logger
|
45
|
+
|
46
|
+
@logger.logs.last[:message].should eq 'another message'
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should grab the correct formatter module via symbol" do
|
50
|
+
formatter = AwesomeLogger.get_formatter_by_symbol :json
|
51
|
+
formatter.should be AwesomeLogger::Json
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should return nil when it cannot find the formatter" do
|
55
|
+
formatter = AwesomeLogger.get_formatter_by_symbol :blah
|
56
|
+
formatter.should be nil
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'awesome_logger'
|
3
|
+
|
4
|
+
describe AwesomeLogger::Console do
|
5
|
+
before :each do
|
6
|
+
@logger = AwesomeLogger.new
|
7
|
+
@logger.formatter = :console
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should always reset colorized text' do
|
11
|
+
@logger.log AwesomeLogger::Console.red "hi"
|
12
|
+
@logger.logs.last[:message].should match /\e\[0m$/
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should prepend the correct color code' do
|
16
|
+
@logger.log AwesomeLogger::Console.red "hi"
|
17
|
+
@logger.logs.last[:message].should match /^\e\[31m/
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should output to the console properly' do
|
21
|
+
@logger.log AwesomeLogger::Console.red "hi"
|
22
|
+
@logger.log AwesomeLogger::Console.red "hi2"
|
23
|
+
|
24
|
+
@logger.output.split("\n").should have(2).things
|
25
|
+
@logger.output.should match /\[.+\]\s+\[.+\]\s+.+/
|
26
|
+
end
|
27
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: awesome_logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Robin Liao
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: json
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Awesome logging
|
47
|
+
email:
|
48
|
+
- randuin@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- awesome_logger.gemspec
|
59
|
+
- lib/awesome_logger.rb
|
60
|
+
- lib/awesome_logger/formatter.rb
|
61
|
+
- lib/awesome_logger/formatters/console.rb
|
62
|
+
- lib/awesome_logger/formatters/json.rb
|
63
|
+
- lib/awesome_logger/version.rb
|
64
|
+
- spec/awesome_logger_spec.rb
|
65
|
+
- spec/console_formatter_spec.rb
|
66
|
+
- spec/spec_helper.rb
|
67
|
+
homepage: ''
|
68
|
+
licenses: []
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements: []
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 1.8.24
|
88
|
+
signing_key:
|
89
|
+
specification_version: 3
|
90
|
+
summary: Awesome logging
|
91
|
+
test_files:
|
92
|
+
- spec/awesome_logger_spec.rb
|
93
|
+
- spec/console_formatter_spec.rb
|
94
|
+
- spec/spec_helper.rb
|
95
|
+
has_rdoc:
|