llt-logger 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3c9908c0e92ba93d7fa933e76a41c689a48e0c10
4
+ data.tar.gz: a9f72b6adc27954be79f791e3541a2265df28e80
5
+ SHA512:
6
+ metadata.gz: 46757399ff8d8940accf81899d045b3b9f21e93cd46c96817dbd6f535d2673ced898af316f85eb6c8678eb98ec55f3124e648558424a756cc36409758e5598d3
7
+ data.tar.gz: 8311bfb5dccf5bd9089a9bf3f15b81712f144e6cb9e93b230853ec58761539747d80ed7dbeff96512607de4b747043f5c81aea1d8dab0b25193432df4e0f68a4
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --tty
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in llt-logger.gemspec
4
+ gemspec
5
+ gem 'coveralls', require: false
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 LFDM
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # LLT::Logger
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'llt-logger'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install llt-logger
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/lib/llt/logger.rb ADDED
@@ -0,0 +1,172 @@
1
+ require "llt/logger/version"
2
+ require "colorize"
3
+
4
+ module LLT
5
+ class Logger
6
+ LEVELS = %w{ error info parser cf morph debug }
7
+ # Numeric vals 0 1 2 3 4 5
8
+ DEFAULT_LEVEL = 1
9
+
10
+ @level = ENV["LLT_DEBUG"] ? normalized_level(ENV["LLT_DEBUG"]) : DEFAULT_LEVEL
11
+ @loggers = []
12
+
13
+ class << self
14
+ attr_reader :loggers
15
+
16
+ def level=(lev)
17
+ return @level = nil if lev.nil?
18
+
19
+ l = normalized_level(lev)
20
+ unless valid_level?(l)
21
+ l = @level || DEFAULT_LEVEL # DEFAULT_LEVEL catches invalid levels defined through the env var
22
+ puts "LOG LEVEL ERROR".red + " #{lev} is unknown - falling back to #{l}"
23
+ end
24
+
25
+ @level = l
26
+ end
27
+
28
+ def level(n = nil)
29
+ return nil if @level.nil?
30
+ n ? n <= @level : @level
31
+ end
32
+
33
+ def new(*args)
34
+ new_logger = super
35
+ @loggers << new_logger
36
+ new_logger
37
+ end
38
+
39
+ def clear
40
+ @loggers.clear
41
+ end
42
+
43
+ def count(mapper = :count)
44
+ @loggers.map(&mapper).inject(:+)
45
+ end
46
+
47
+ def errors
48
+ messages.select { |message| message =~ /ERROR!/ }
49
+ end
50
+
51
+ def warnings
52
+ messages.select { |message| message =~ /WARNING!/ }
53
+ end
54
+
55
+ def messages_that_match(regexp)
56
+ messages.select { |message| message =~ regexp }
57
+ end
58
+
59
+ def count_errors
60
+ count(:errors)
61
+ end
62
+
63
+ def count_warnings
64
+ count(:warnings)
65
+ end
66
+
67
+ def normalized_level(lev)
68
+ if lev.is_a? Fixnum
69
+ lev
70
+ else
71
+ LEVELS.index(lev.to_s) || -1 # -1 to fail the valid_level? test
72
+ end
73
+ end
74
+
75
+ private
76
+
77
+ def messages
78
+ @loggers.flat_map(&:logs)
79
+ end
80
+
81
+ def valid_level?(lev)
82
+ lev.between?(0, 5)
83
+ end
84
+ end
85
+
86
+ attr_reader :title, :logs, :errors, :warnings
87
+
88
+ def initialize(title = "", indent = "", default: :info)
89
+ @title = title
90
+ @indent = to_whitespace(indent)
91
+ @default = default
92
+ @logs = []
93
+ @errors = 0
94
+ @warnings = 0
95
+ end
96
+
97
+ def log(*args)
98
+ send(@default, *args)
99
+ # TODO Exception Handling
100
+ end
101
+
102
+ def error(message, indent = "")
103
+ message = "ERROR! #{message}".light_red
104
+ if level(0)
105
+ log_message(message, indent)
106
+ @errors += 1
107
+ end
108
+ end
109
+
110
+ def warning(message, indent = "")
111
+ message = "WARNING! #{message}".yellow
112
+ if level(1)
113
+ log_message(message, indent)
114
+ @warnings += 1
115
+ end
116
+ end
117
+
118
+ def info(message, indent = "")
119
+ log_message(message, indent) if level(1)
120
+ end
121
+
122
+ def parser(message, indent = "")
123
+ log_message(message, indent) if level(2)
124
+ end
125
+
126
+ def cf(message, indent = "")
127
+ log_message(message, indent) if level(3)
128
+ end
129
+
130
+ def morph(message, indent = "")
131
+ log_message(message, indent) if level(4)
132
+ end
133
+
134
+ def debug(message, indent = "")
135
+ log_message(message, indent) if level(5)
136
+ end
137
+
138
+ def bare(message, indent = 0)
139
+ lev = Logger.normalized_level(@default)
140
+ puts "#{to_whitespace(indent)}#{message}" if level(lev)
141
+ end
142
+
143
+ def count
144
+ @logs.count
145
+ end
146
+
147
+ private
148
+
149
+ def log_message(message, indent)
150
+ t = (! @title.empty? ? "#{@title}: " : "")
151
+ indentation = @indent + to_whitespace(indent)
152
+ str = "#{indentation}#{t}#{message}"
153
+
154
+ @logs << str
155
+ puts str
156
+ end
157
+
158
+ def level(lev = nil)
159
+ Logger.level(lev)
160
+ end
161
+
162
+ def to_whitespace(indent)
163
+ if indent.is_a?(Fixnum)
164
+ str = ""
165
+ indent.times { str << " "}
166
+ str
167
+ else
168
+ indent
169
+ end
170
+ end
171
+ end
172
+ end
@@ -0,0 +1,5 @@
1
+ module LLT
2
+ class Logger
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'llt/logger/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "llt-logger"
8
+ spec.version = LLT::Logger::VERSION
9
+ spec.authors = ["LFDM"]
10
+ spec.email = ["1986gh@gmail.com"]
11
+ spec.description = %q{LLT Logger}
12
+ spec.summary = %q{LLT Logger}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "simplecov", "~> 0.7"
25
+ spec.add_dependency "colorize"
26
+ end
@@ -0,0 +1,286 @@
1
+ require 'spec_helper'
2
+ require 'stringio'
3
+
4
+ describe LLT::Logger do
5
+ before :all do
6
+ string_io = StringIO.new
7
+ @stdout = $stdout
8
+ $stdout = string_io
9
+ end
10
+
11
+ it 'should have a version number' do
12
+ LLT::Logger::VERSION.should_not be_nil
13
+ end
14
+
15
+ let(:logger) { LLT::Logger }
16
+ describe ".level" do
17
+ context "with no argument" do
18
+ it "returns the current logger level" do
19
+ logger.level.should_not be_nil
20
+ end
21
+
22
+ it "default level is info" do
23
+ logger.level.should == 1
24
+ end
25
+ end
26
+
27
+ context "with an argument" do
28
+ it "returns true if inside of current logger level" do
29
+ logger.level(1).should be_true
30
+ end
31
+
32
+ it "returns false if outside of current logger level" do
33
+ logger.level = 2 # morph_debug
34
+ logger.level(5).should be_false
35
+ end
36
+ end
37
+
38
+ # don't know how to test that
39
+ #it "can be set through env var LLT_DEBUG" do
40
+ # ENV["LLT_DEBUG"] = "info"
41
+ # logger.level.should == "info"
42
+ #end
43
+ end
44
+
45
+ describe ".level=" do
46
+ it "sets the logger level" do
47
+ logger.level = "info"
48
+ logger.level.should == 1 # info
49
+ end
50
+
51
+ it "needs to be set to a valid level, otherwise an error message is printed and the current level still in use" do
52
+ old_level = logger.level
53
+
54
+ $stdout.should receive(:puts)
55
+ logger.level = "blabla"
56
+
57
+ logger.level.should == old_level
58
+ end
59
+
60
+ it "can be set to nil - Logger will be shut down then" do
61
+ logger.level = nil
62
+ a = logger.new
63
+
64
+ $stdout.should_not receive(:puts)
65
+ a.error("")
66
+ a.debug("")
67
+
68
+ logger.level = :info # set back for other tests
69
+ end
70
+ end
71
+
72
+ describe ".loggers" do
73
+ it "returns an array of loggers" do
74
+ logger.loggers.should be_an_instance_of Array
75
+ end
76
+ end
77
+
78
+ describe ".new" do
79
+ it "registers the newly created logger in Logger.loggers" do
80
+ new_logger = logger.new
81
+ logger.loggers.should include new_logger
82
+ end
83
+
84
+ it "and returns a new logger instance" do
85
+ logger.new.should be_an_instance_of LLT::Logger
86
+ end
87
+ end
88
+
89
+ describe ".clear" do
90
+ it "clears all registered loggers" do
91
+ 5.times { logger.new }
92
+ logger.loggers.should have_at_least(5).items
93
+ logger.clear
94
+ logger.loggers.should be_empty
95
+ end
96
+ end
97
+
98
+ describe ".count" do
99
+ it "counts all log messages" do
100
+ logger.clear
101
+
102
+ a = logger.new
103
+ b = logger.new
104
+ 5.times { a.log("") }
105
+ 5.times { b.error("") }
106
+
107
+ logger.count.should == 10
108
+ end
109
+ end
110
+
111
+ describe ".count_errors" do
112
+ it "counts all errors present" do
113
+ logger.clear
114
+
115
+ a = logger.new
116
+ b = logger.new
117
+ 5.times { a.error("") }
118
+ 5.times { b.error("") }
119
+
120
+ logger.count_errors.should == 10
121
+ end
122
+ end
123
+
124
+ describe ".count_warnings" do
125
+ it "counts all warnings present" do
126
+ logger.clear
127
+
128
+ a = logger.new
129
+ b = logger.new
130
+ 5.times { a.warning("") }
131
+ 5.times { b.warning("") }
132
+
133
+ logger.count_warnings.should == 10
134
+ end
135
+ end
136
+
137
+ describe ".errors" do
138
+ it "returns all logged error messages" do
139
+ logger.clear
140
+
141
+ a = logger.new
142
+ b = logger.new
143
+ 5.times { a.error("") }
144
+ 3.times { b.warning("") }
145
+
146
+ logger.errors.should have(5).items
147
+ end
148
+ end
149
+
150
+ describe ".message_that_match" do
151
+ it "returns all messages that match a given Regexp" do
152
+ logger.clear
153
+
154
+ a = logger.new
155
+ b = logger.new
156
+ 5.times { a.log("arma") }
157
+ 3.times { b.log("multa") }
158
+
159
+ logger.messages_that_match(/arma/).should have(5).items
160
+ end
161
+ end
162
+
163
+ describe ".warnings" do
164
+ it "returns all logged warnings" do
165
+ logger.clear
166
+ a = logger.new
167
+ b = logger.new
168
+
169
+ 5.times { a.error("") }
170
+ 3.times { b.warning("") }
171
+
172
+ logger.warnings.should have(3).items
173
+ end
174
+ end
175
+
176
+ describe "#initialize" do
177
+ it "takes title and indentation as arguments" do
178
+ title = "a"
179
+ nl = logger.new(title, " ")
180
+ nl.title.should == title
181
+ nl.instance_variable_get("@indent").should == " "
182
+ end
183
+
184
+ it "takes :default as keyword argument" do
185
+ nl = logger.new(default: "parser")
186
+ nl.instance_variable_get("@default").should == "parser"
187
+ end
188
+
189
+ it "default defaults to :info" do
190
+ a = logger.new
191
+ a.instance_variable_get("@default").should == :info
192
+ end
193
+
194
+ it "defaults '' and 0" do
195
+ nl = logger.new
196
+ nl.title.should == ""
197
+ nl.instance_variable_get("@indent").should == ""
198
+ end
199
+
200
+ it "indentantion can be set with a Fixnum, converted to n whitespaces" do
201
+ nl = logger.new("title", 3)
202
+ nl.instance_variable_get("@indent").should == " "
203
+ end
204
+ end
205
+
206
+ describe "#log" do
207
+ it "delegates to the set default" do
208
+ a = logger.new(default: "cf")
209
+ b = logger.new(default: "morph")
210
+
211
+ a.should receive(:cf)
212
+ b.should_not receive(:cf)
213
+
214
+ a.log("") and b.log("")
215
+ end
216
+
217
+ it "all log calls take a message and an optional indent level that adds up to the default indentation" do
218
+ a = logger.new("", 2)
219
+ $stdout.should receive(:puts).with(" message")
220
+ a.log("message", 2)
221
+ end
222
+
223
+ it "title is automatically added if present" do
224
+ a = logger.new("Logger", 2)
225
+ $stdout.should receive(:puts).with(" Logger: message")
226
+ a.log("message")
227
+ end
228
+ end
229
+
230
+ describe "#bare" do
231
+ it "logs without title, without default intendation level and doesn't store the message" do
232
+ a = logger.new("Title", 28)
233
+ $stdout.should receive(:puts).with(" message")
234
+ a.bare("message", 1)
235
+ a.logs.should be_empty
236
+ end
237
+
238
+ it "default level matters and decides if anything happens at all" do
239
+ logger.level = 3 # cf
240
+ a = logger.new(default: :debug)
241
+ b = logger.new(default: :info)
242
+
243
+ $stdout.should receive(:puts).exactly(:once)
244
+
245
+ a.bare("")
246
+ b.bare("")
247
+ end
248
+ end
249
+
250
+ describe "#logs" do
251
+ it "logs are stored" do
252
+ a = logger.new
253
+ a.log("message")
254
+ a.logs.should include "message"
255
+ end
256
+ end
257
+
258
+ describe "#count" do
259
+ it "counts number of logs" do
260
+ a = logger.new
261
+ 5.times { a.log("") }
262
+ a.count.should == 5
263
+ end
264
+ end
265
+
266
+ describe "#errors" do
267
+ it "counts the number of errors" do
268
+ a = logger.new
269
+ a.error("")
270
+ a.errors.should == 1
271
+ end
272
+ end
273
+
274
+ describe "#warnings" do
275
+ it "counts the number of warnings" do
276
+ a = logger.new
277
+ a.warning("")
278
+ a.warning("")
279
+ a.warnings.should == 2
280
+ end
281
+ end
282
+
283
+ after :all do
284
+ $stdout = @stdout
285
+ end
286
+ end
@@ -0,0 +1,16 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+
4
+ Coveralls.wear!
5
+
6
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
7
+ SimpleCov::Formatter::HTMLFormatter,
8
+ Coveralls::SimpleCov::Formatter
9
+ ]
10
+
11
+ SimpleCov.start do
12
+ add_filter '/spec/'
13
+ end
14
+
15
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
16
+ require 'llt/logger'
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: llt-logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - LFDM
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: colorize
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: LLT Logger
84
+ email:
85
+ - 1986gh@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/llt/logger.rb
97
+ - lib/llt/logger/version.rb
98
+ - llt-logger.gemspec
99
+ - spec/lib/llt/logger_spec.rb
100
+ - spec/spec_helper.rb
101
+ homepage: ''
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.1.5
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: LLT Logger
125
+ test_files:
126
+ - spec/lib/llt/logger_spec.rb
127
+ - spec/spec_helper.rb