better-logger 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in better-logger.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Sam Rose
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,119 @@
1
+ # Better::Logger
2
+
3
+ Better Logger is designed to be a replacement for the standard library logging
4
+ gem. There are a couple of things about the standard library gem that I don't
5
+ quite like:
6
+
7
+ - It puts errors in the same place as debug and info messages. I'd prefer my
8
+ logger to put errors in STDERR and everything else in STDOUT.
9
+
10
+ - Lots of people declare their loggers in different ways, there's no
11
+ convention around logging. I'd much prefer to be able to specify a
12
+ configuration block and then have a globally accessible logger.
13
+
14
+ - The default format of log output in the Ruby standard logger isn't very
15
+ useful in my opinion. I would prefer a default that has colours and
16
+ information about where in the code the log message came from.
17
+
18
+ This gem attempts to address all of these things. You may prefer the standard
19
+ Ruby implementation, and that's totally fine, I built this for me originally and
20
+ wanted to share it :)
21
+
22
+ ## Installation
23
+
24
+ Add this line to your application's Gemfile:
25
+
26
+ gem 'better-logger'
27
+
28
+ And then execute:
29
+
30
+ $ bundle
31
+
32
+ Or install it yourself as:
33
+
34
+ $ gem install better-logger
35
+
36
+ ## Usage
37
+
38
+ First thing you need to do is configure your logger:
39
+
40
+ ``` ruby
41
+ require 'better-logger'
42
+
43
+ Better::Logger.config do |conf|
44
+ conf.color = true
45
+ conf.log_to = STDOUT
46
+ conf.error_to = STDERR
47
+ conf.log_level = :info
48
+ end
49
+ ```
50
+
51
+ After this configuration block you will have access to a `log` object from
52
+ everywhere in your code. Its usage should be familiar:
53
+
54
+ ``` ruby
55
+ # The following log methods are listed in order of their severity from least to
56
+ # most severe.
57
+
58
+ log.debug "This is a debug message. It goes to conf.log_to"
59
+ log.info "This is an info message. It goes to conf.log_to"
60
+ log.warn "This is a warn message. It goes to conf.log_to"
61
+ log.error "This is an error message. It does to conf.error_to"
62
+ log.fatal "This is also an error but more sever. It also goes to conf.error_to"
63
+ ```
64
+
65
+ ### Multiple logs
66
+
67
+ It's possible to have multiple logs in the same piece of code by passing a
68
+ symbol into the configure blocks:
69
+
70
+ ``` ruby
71
+ require 'better-logger'
72
+
73
+ Better::Logger.config :log1 do |conf|
74
+ conf.color = true
75
+ conf.log_to = STDOUT
76
+ conf.error_to = STDERR
77
+ conf.log_level = :info
78
+ end
79
+
80
+ Better::Logger.config :log2 do |conf|
81
+ conf.color = true
82
+ conf.log_to = "log_output.log"
83
+ conf.error_to = "log_error.log"
84
+ conf.log_level = :info
85
+ end
86
+
87
+ log1.info "Going to STDOUT."
88
+ log2.info "Going to a file called 'log_output.log' in the current directory."
89
+ ```
90
+
91
+ ### Modifying logs after creation
92
+
93
+ You can access and modify the configuration of a log after its creation simply
94
+ by modifying its `config` object:
95
+
96
+ ``` ruby
97
+ require 'better-logger'
98
+
99
+ Better::Logger.config do |conf|
100
+ conf.color = true
101
+ conf.log_to = "log_output.log"
102
+ conf.error_to = "log_error.log"
103
+ conf.log_level = :info
104
+ end
105
+
106
+ log.info "Logging to a file called 'log_output.log'"
107
+
108
+ log.config.log_to = STDOUT
109
+
110
+ log.info "Logging to STDOUT."
111
+ ```
112
+
113
+ ## Contributing
114
+
115
+ 1. Fork it
116
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
117
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
118
+ 4. Push to the branch (`git push origin my-new-feature`)
119
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ task :default => [:test]
5
+
6
+ desc "Run all tests"
7
+ RSpec::Core::RakeTask.new(:test) do |t|
8
+ t.rspec_opts = '-cfs'
9
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'better-logger/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "better-logger"
8
+ gem.version = Better::Logger::VERSION
9
+ gem.authors = ["Sam Rose"]
10
+ gem.email = ["samwho@lbak.co.uk"]
11
+ gem.description = %q{A better logging default for Ruby.}
12
+ gem.summary = %q{A little tired of unconventional logging techniques in projects, this gem aims to introduce a logging convention.}
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
+ # Dependencies
21
+ gem.add_development_dependency "rspec"
22
+ gem.add_dependency "colored"
23
+ end
@@ -0,0 +1,22 @@
1
+ require_relative '../lib/better-logger'
2
+
3
+ Better::Logger.config do |conf|
4
+ conf.color = true
5
+ conf.log_to = STDOUT
6
+ conf.error_to = STDERR
7
+ conf.log_level = :debug
8
+ end
9
+
10
+ class Foo
11
+ def bar
12
+ log.debug "Yes"
13
+ log.info "Np"
14
+ log.warn "Oops!"
15
+ log.error "Test"
16
+ log.fatal "wat"
17
+ end
18
+ end
19
+
20
+ Foo.new.bar
21
+
22
+ log.info "Done."
@@ -0,0 +1,11 @@
1
+ class Foo
2
+ include Better::Logger
3
+
4
+ def initialize
5
+ debug "Initialize entered."
6
+ end
7
+
8
+ def bar
9
+ error "Oh noes! Something went wrong!"
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ lib = File.dirname(__FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ def require_all path
5
+ Dir[File.join(File.dirname(__FILE__), path, '*.rb')].each { |f| require f }
6
+ end
7
+
8
+ require 'colored'
9
+
10
+ require 'better-logger/base'
11
+ require_all 'better-logger'
12
+
13
+ # Make the logging methods available from everywhere
14
+ include Better::Logger::Loggers
@@ -0,0 +1,26 @@
1
+ module Better
2
+ module Logger
3
+ LEVELS = {
4
+ debug: 0,
5
+ info: 1,
6
+ warn: 2,
7
+ error: 3,
8
+ fatal: 4,
9
+ silent: 100,
10
+ }
11
+
12
+ def self.config log_name = :log, &block
13
+ conf_object = Config.new
14
+ yield conf_object
15
+
16
+ # The "Loggers" module gets included into the main namespace when
17
+ # better-logger is required. By defining a method in it, we define a
18
+ # method in the main namespace. This allows the loggers to be accessed
19
+ # from anywhere.
20
+ Loggers._log_hash[log_name] = Logger.new(conf_object)
21
+ Loggers.send :define_method, log_name do
22
+ Loggers._log_hash[log_name]
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,72 @@
1
+ module Better
2
+ module Logger
3
+ class Config
4
+ attr_writer :color, :formatter, :datetime_format, :log_level, :time_format
5
+
6
+ def color
7
+ @color = true if @color.nil?
8
+ @color
9
+ end
10
+
11
+ def color?
12
+ color
13
+ end
14
+
15
+ def log_to
16
+ @log_to ||= STDOUT
17
+ end
18
+
19
+ def log_to= new_log_to
20
+ if new_log_to.is_a? String
21
+ @log_to = File.open(new_log_to, 'a')
22
+ else
23
+ @log_to = new_log_to
24
+ end
25
+ end
26
+
27
+ def error_to
28
+ @error_to ||= STDERR
29
+ end
30
+
31
+ def error_to= new_error_to
32
+ if new_error_to.is_a? String
33
+ @error_to = File.open(new_error_to, 'a')
34
+ else
35
+ @error_to = new_error_to
36
+ end
37
+ end
38
+
39
+ def log_level
40
+ @log_level ||= :info
41
+ end
42
+
43
+ def time_format
44
+ @time_format ||= "%Y/%m/%d %H:%M:%S"
45
+ end
46
+
47
+ def formatter
48
+ @formatter ||= lambda do |message, level|
49
+ _level = level.to_s.ljust(5)
50
+ _time = Time.now.strftime(config.time_format)
51
+
52
+ if config.color?
53
+ case level
54
+ when :debug
55
+ _level = _level.magenta
56
+ when :info
57
+ _level = _level.cyan
58
+ when :warn
59
+ _level = _level.yellow
60
+ when :error
61
+ _level = _level.red
62
+ when :fatal
63
+ _level = _level.red
64
+ end
65
+ end
66
+
67
+ "[#{_time}][#{_level}][#{caller[3]}] #{message}"
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,37 @@
1
+ module Better
2
+ module Logger
3
+ class Logger
4
+ attr_accessor :config
5
+
6
+ def initialize config
7
+ @config = config
8
+ end
9
+
10
+ def debug msg; _log config.log_to, __method__, msg; end;
11
+ def info msg; _log config.log_to, __method__, msg; end;
12
+ def warn msg; _log config.log_to, __method__, msg; end;
13
+ def error msg; _log config.error_to, __method__, msg; end;
14
+ def fatal msg; _log config.error_to, __method__, msg; end;
15
+
16
+ # Pseudo-private methods
17
+
18
+ def _format msg, level = config.log_level
19
+ if config.formatter
20
+ instance_exec msg, level.to_sym, &config.formatter
21
+ else
22
+ msg
23
+ end
24
+ end
25
+
26
+ def _should_log? level
27
+ # :info <= :debug
28
+ # 1 <= 0
29
+ LEVELS[config.log_level] <= LEVELS[level.to_sym]
30
+ end
31
+
32
+ def _log io, level, msg
33
+ io.puts _format(msg, level) if _should_log? level
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,9 @@
1
+ module Better
2
+ module Logger
3
+ module Loggers
4
+ def self._log_hash
5
+ @@log_hash ||= {}
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module Better
2
+ module Logger
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,191 @@
1
+ require 'spec_helper'
2
+
3
+ describe Better::Logger do
4
+ let(:fake_stdout) { Tempfile.new 'fake_stdout' }
5
+ let(:fake_stderr) { Tempfile.new 'fake_stderr' }
6
+ let(:fake_stdout_content) { fake_stdout.rewind; fake_stdout.read }
7
+ let(:fake_stderr_content) { fake_stderr.rewind; fake_stderr.read }
8
+
9
+ before :each do
10
+ Better::Logger.config :log do |conf|
11
+ conf.color = false
12
+ conf.log_to = fake_stdout
13
+ conf.error_to = fake_stderr
14
+ conf.log_level = :debug
15
+ end
16
+ end
17
+
18
+ after :each do
19
+ fake_stdout.close; fake_stdout.unlink
20
+ fake_stderr.close; fake_stderr.unlink
21
+ end
22
+
23
+ describe "log#info" do
24
+ subject { fake_stdout_content }
25
+ before { log.info "Testing!" }
26
+ it { should include "Testing!" }
27
+ it { should include "[info ]" }
28
+ end
29
+
30
+ describe "log#info multiple calls" do
31
+ subject { fake_stdout_content }
32
+
33
+ before do
34
+ log.info "Info test!"
35
+ log.info "Info test 2!"
36
+ end
37
+
38
+ it { should include "Info test!" }
39
+ it { should include "Info test 2!" }
40
+ end
41
+
42
+ describe "log#error" do
43
+ subject { fake_stderr_content }
44
+ before { log.error "error test!" }
45
+ it { should include "error test!" }
46
+ it { should include "[error]" }
47
+ end
48
+
49
+ describe "log#error multiple calls" do
50
+ subject { fake_stderr_content }
51
+
52
+ before do
53
+ log.error "error test!"
54
+ log.error "error test 2!"
55
+ end
56
+
57
+ it { should include "error test!" }
58
+ it { should include "error test 2!" }
59
+ end
60
+
61
+ describe "log#debug" do
62
+ subject { fake_stdout_content }
63
+ before { log.debug "debug test!" }
64
+ it { should include "debug test!" }
65
+ it { should include "[debug]" }
66
+ end
67
+
68
+ describe "log#debug multiple calls" do
69
+ subject { fake_stderr_content }
70
+
71
+ before do
72
+ log.error "debug test!"
73
+ log.error "debug test 2!"
74
+ end
75
+
76
+ it { should include "debug test!" }
77
+ it { should include "debug test 2!" }
78
+ end
79
+
80
+ describe "With color" do
81
+ before :each do
82
+ Better::Logger.config :log do |conf|
83
+ conf.color = true
84
+ conf.log_to = fake_stdout
85
+ conf.error_to = fake_stderr
86
+ conf.log_level = :debug
87
+ end
88
+ end
89
+
90
+ describe "log#fatal" do
91
+ subject { fake_stderr_content }
92
+ before { log.fatal "fatal test!" }
93
+ it { should include "fatal test!" }
94
+ it { should include "[#{"fatal".red}]" }
95
+ end
96
+ end
97
+
98
+ describe "With a different formatter" do
99
+ before :each do
100
+ Better::Logger.config :log do |conf|
101
+ conf.color = true
102
+ conf.log_to = fake_stdout
103
+ conf.error_to = fake_stderr
104
+ conf.log_level = :debug
105
+
106
+ conf.formatter = lambda do |message, level|
107
+ "#{level.upcase} #{message.upcase}"
108
+ end
109
+ end
110
+ end
111
+
112
+ describe "log#warn" do
113
+ subject { fake_stdout_content }
114
+ before { log.warn "warn test!" }
115
+ it { should == "WARN WARN TEST!\n" }
116
+ end
117
+ end
118
+
119
+ describe "Non-default logging method" do
120
+ before :each do
121
+ Better::Logger.config :testlog do |conf|
122
+ conf.color = true
123
+ conf.log_to = fake_stdout
124
+ conf.error_to = fake_stderr
125
+ conf.log_level = :debug
126
+ end
127
+ end
128
+
129
+ describe "testlog#info" do
130
+ subject { fake_stdout_content }
131
+ before { testlog.info "testlog!" }
132
+ it { should include "testlog!" }
133
+ end
134
+ end
135
+
136
+ describe "Slightly raised log level" do
137
+ before :each do
138
+ Better::Logger.config :testlog do |conf|
139
+ conf.color = true
140
+ conf.log_to = fake_stdout
141
+ conf.error_to = fake_stderr
142
+ conf.log_level = :info
143
+ end
144
+ end
145
+
146
+ describe "log#debug" do
147
+ subject { fake_stdout_content }
148
+ before { testlog.debug "I shouldn't be in the output." }
149
+ it { should be_empty }
150
+ end
151
+ end
152
+
153
+ describe "Silent flag" do
154
+ before :each do
155
+ Better::Logger.config :log do |conf|
156
+ conf.color = true
157
+ conf.log_to = fake_stdout
158
+ conf.error_to = fake_stderr
159
+ conf.log_level = :silent
160
+ end
161
+ end
162
+
163
+ describe "log#fatal" do
164
+ subject { fake_stderr_content }
165
+ before { log.fatal "Should not be output." }
166
+ it { should be_empty }
167
+ end
168
+ end
169
+
170
+ describe "Modifying log after creation" do
171
+ before :each do
172
+ Better::Logger.config :log do |conf|
173
+ conf.color = true
174
+ conf.log_to = fake_stdout
175
+ conf.error_to = fake_stderr
176
+ conf.log_level = :info
177
+ end
178
+ end
179
+
180
+ describe "log#info after log_to changed to fake_stderr" do
181
+ subject { fake_stderr_content }
182
+
183
+ before do
184
+ log.config.log_to = fake_stderr
185
+ log.info "Testing!"
186
+ end
187
+
188
+ it { should include "Testing!" }
189
+ end
190
+ end
191
+ end
@@ -0,0 +1,2 @@
1
+ require_relative '../lib/better-logger'
2
+ require 'tempfile'
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: better-logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sam Rose
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
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: '0'
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: '0'
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: '0'
46
+ description: A better logging default for Ruby.
47
+ email:
48
+ - samwho@lbak.co.uk
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - better-logger.gemspec
59
+ - examples/config.rb
60
+ - examples/include.rb
61
+ - lib/better-logger.rb
62
+ - lib/better-logger/base.rb
63
+ - lib/better-logger/config.rb
64
+ - lib/better-logger/logger.rb
65
+ - lib/better-logger/loggers.rb
66
+ - lib/better-logger/version.rb
67
+ - spec/logger_spec.rb
68
+ - spec/spec_helper.rb
69
+ homepage: ''
70
+ licenses: []
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 1.8.24
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: A little tired of unconventional logging techniques in projects, this gem
93
+ aims to introduce a logging convention.
94
+ test_files:
95
+ - spec/logger_spec.rb
96
+ - spec/spec_helper.rb
97
+ has_rdoc: