sinatra-log 0.0.0

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: ea5a6692eacff0449ee337af1b84040e18a05b56
4
+ data.tar.gz: c52e620e1fd30d43fa5b7927fbf13b083fe11a0e
5
+ SHA512:
6
+ metadata.gz: 9f542c7f43a6bfca43ded5cf63aebe7e2b4e56c65a9e0fd4322197dc4200773ec69d068356bd4f5cd40a832e7c9a0214e5265618f7229593900a1bf11f4a32b5
7
+ data.tar.gz: bfbbbd41a90a613eeecc14a527dec6838af38433a68411a5ab430273900793b351c60d7384b36fda25ab288a850e5131f8c97fea506d503bd197b3808c2f071e
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ .ruby-*
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+ log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sintra-utils.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Lookout, Inc
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/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |t|
5
+ t.rspec_opts = '--fail-fast --color --order random'
6
+ end
7
+
8
+ task :test => :spec
9
+ task :default => :test
@@ -0,0 +1,103 @@
1
+ require 'rubygems'
2
+ require 'log4r'
3
+ require 'time'
4
+
5
+ module Sinatra
6
+ # Logging. Logs to log/<project_name>.log with the format:
7
+ #
8
+ # [Log Level]: [Timestamp (ISO-8601)]: [File:linenum]: [Log Message]
9
+ #
10
+ # Use through the helper:
11
+ # log.warn 'This is my log message'
12
+ #
13
+ class Log
14
+ include Log4r
15
+
16
+ # Formatter that include the filename and relative path, and line number in
17
+ # output of the caller.
18
+ #
19
+ # Since all callers go through the methods defined in this class to log, we
20
+ # look at the second line of the tracer output, removing everything but the
21
+ # directories after the project directory.
22
+ #
23
+ class DefaultFormatter < Log4r::Formatter
24
+ # Return the project base directory for filtering to help with
25
+ # identifiying the filename and line number when formatting the log
26
+ # message
27
+ #
28
+ # @return [String] Base directory for the project
29
+ def basedir
30
+ @basedir ||= File.expand_path(File.join(File.dirname(__FILE__), ".."))
31
+ end
32
+
33
+ # Return a trimmed version of the filename from where a LogEvent occurred
34
+ # @param [String] tracer A line from the LogEvent#tracer Array
35
+ # @return [String] Trimmed and parsed version of the file ane line number
36
+ def event_filename(tracer)
37
+ parts = tracer.match(/#{basedir}\/(.*:[0-9]+).*:/)
38
+
39
+ # If we get no matches back, we're probably in a jar file in which case
40
+ # the format of the tracer is going to be abbreviated
41
+ if parts.nil?
42
+ parts = tracer.match(/(.*:[0-9]+).*:/)
43
+ end
44
+ return parts[-1] if parts
45
+ end
46
+
47
+ # Receive the LogEvent and pull out the log message and format it for
48
+ # display in the logs
49
+ #
50
+ # @param [Log4r::LogEvent] event
51
+ # @return [String] Formatted log message
52
+ def format(event)
53
+ filename = event_filename(event.tracer[1])
54
+ time = Time.now.utc.iso8601
55
+ return "#{Log4r::LNAMES[event.level]}: #{time}: #{filename}: #{event.data}\n"
56
+ end
57
+ end
58
+
59
+ attr_reader :outputter
60
+
61
+ def initialize(config={})
62
+ errors = []
63
+ [:logger_name, :loglevel, :log_filename, :enabled].each do |key|
64
+ if !config.include?(key)
65
+ errors << "#{key} required, but not specified in config hash"
66
+ end
67
+ raise ArgumentError, "#{errors}" if errors.count > 0
68
+ end
69
+
70
+ logger_name = config[:logger_name].to_s.gsub(/\s+/, '_')
71
+ @logger = Log4r::Logger.new(logger_name)
72
+
73
+ if config[:enabled]
74
+ index = Log4r::LNAMES.index(config[:loglevel])
75
+ # if logger.level is not in LNAMES an exception will be thrown
76
+ @logger.level = index unless index.nil?
77
+ else
78
+ @logger.level = Log4r::OFF
79
+ end
80
+
81
+ @outputter = FileOutputter.new("#{logger_name}fileoutput",
82
+ :filename => config[:log_filename],
83
+ :trunc => false)
84
+ @logger.trace = true
85
+ @outputter.formatter = DefaultFormatter
86
+ @logger.outputters = @outputter
87
+ end
88
+
89
+
90
+ [:debug, :info, :warn, :error, :fatal, :level].each do |method|
91
+ define_method(method) do |*args, &block|
92
+ @logger.send(method, *args, &block)
93
+ end
94
+
95
+ # Returns true iff the current severity level allows for
96
+ # the printing of level messages.
97
+ allow_logging = "#{method}?".to_sym
98
+ define_method(allow_logging) do |*args|
99
+ @logger.send(allow_logging, *args)
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,5 @@
1
+ module Sinatra
2
+ module Logger
3
+ VERSION = "0.0.0"
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sinatra/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sinatra-log"
8
+ spec.version = Sinatra::Logger::VERSION
9
+ spec.authors = ["Shaw Vrana"]
10
+ spec.email = ["shaw@vranix.com1"]
11
+ spec.description = %q{A logger for Sinatra}
12
+ spec.summary = %q{}
13
+ spec.homepage = "http://github.com/svrana/sinatra-log"
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{^(spec)/})
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", "~> 2.0"
24
+ spec.add_development_dependency "rspec-its"
25
+ spec.add_development_dependency "timecop"
26
+
27
+ spec.add_dependency "log4r"
28
+ end
@@ -0,0 +1,113 @@
1
+ require 'spec_helper'
2
+ require 'sinatra/log'
3
+ require 'timecop'
4
+
5
+ describe Sinatra::Log do
6
+ let(:myconfig) do
7
+ {
8
+ :enabled => true,
9
+ :logger_name => 'mylogger',
10
+ :loglevel => 'DEBUG',
11
+ :log_filename => 'log'
12
+ }
13
+ end
14
+
15
+ subject(:log) { Sinatra::Log.new(myconfig) }
16
+ subject(:log_message) { 'foo' }
17
+
18
+ [:debug, :info, :warn, :error, :fatal].each do |method|
19
+ describe ".#{method}" do
20
+ it 'should invoke the internal logger object with a given block' do
21
+ log.instance_variable_get(:@logger).should_receive(method).with(log_message).and_call_original
22
+ processed = false
23
+ b = Proc.new { processed = true }
24
+ log.send(method, log_message, &b)
25
+ expect(processed).to be(true)
26
+ end
27
+
28
+ it 'should invoke the internal logger object w/o a given block' do
29
+ log.instance_variable_get(:@logger).should_receive(method).with(log_message).and_call_original
30
+ log.send(method, log_message)
31
+ end
32
+ end
33
+ end
34
+
35
+ [:debug?, :info?, :warn?, :error?, :fatal?].each do |method|
36
+ describe ".#{method}" do
37
+ it 'returns true when level is debug' do
38
+ expect(log.send(method)).to eq(true)
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ describe Sinatra::Log::DefaultFormatter do
45
+ subject(:formatter) { described_class.new }
46
+ let(:logger) do
47
+ logger = double('Mock Logger')
48
+ logger.stub(:name).and_return('RSpec Logger')
49
+ logger.stub(:fullname).and_return('RSpec Logger')
50
+ logger
51
+ end
52
+ let(:project_name) { 'some_project' }
53
+ let(:basedir) { "/home/rspec/#{project_name}" }
54
+ let(:tracer) do
55
+ [
56
+ "#{basedir}/log.rb:63:in `warn'",
57
+ "#{basedir}/spec/log_spec.rb:9:in `block (2 levels) in <top (required)>'"
58
+ ]
59
+ end
60
+
61
+ before :all do
62
+ # The root logger creates the log levels, so making sure it's been
63
+ # created
64
+ Log4r::RootLogger.instance
65
+ end
66
+
67
+
68
+ before :each do
69
+ formatter.stub(:basedir).and_return(basedir)
70
+ end
71
+
72
+
73
+ describe '#event_filename' do
74
+ subject(:filename) { formatter.event_filename(tracer[1]) }
75
+
76
+ context 'with a normal MRI LogEvent' do
77
+ it { should eql('spec/log_spec.rb:9') }
78
+ end
79
+
80
+ # We have slightly different log formats under packaged .jar files
81
+ context 'with a LogEvent from a packaged .jar' do
82
+ let(:tracer) { [nil, "backend/metrics.rb:52:in `runloop'"] }
83
+ let(:basedir) { 'file:/home/user/source/projects/stuff.jar!/project' }
84
+
85
+ it { should eql('backend/metrics.rb:52') }
86
+ end
87
+ end
88
+
89
+ describe '#format' do
90
+ before :each do
91
+ Timecop.freeze
92
+ end
93
+
94
+ after :each do
95
+ Timecop.return
96
+ end
97
+
98
+ context 'with a valid LogEvent' do
99
+ # Level 3 is the Log4r "warn" level
100
+ let(:level) { 3 }
101
+ let(:data) { 'rspec' }
102
+ let(:timestamp) { Time.now.utc.iso8601 }
103
+
104
+ let(:event) do
105
+ event = Log4r::LogEvent.new(level, logger, tracer, data)
106
+ end
107
+
108
+ it 'should be properly formatted' do
109
+ expect(formatter.format(event)).to eql("WARN: #{timestamp}: spec/log_spec.rb:9: #{data}\n")
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,6 @@
1
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib/'))
2
+
3
+ require 'sinatra/logger'
4
+
5
+ require 'rspec'
6
+ require 'rspec/its'
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-log
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Shaw Vrana
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-14 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: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-its
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: timecop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: log4r
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: A logger for Sinatra
98
+ email:
99
+ - shaw@vranix.com1
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - Rakefile
110
+ - lib/sinatra/log.rb
111
+ - lib/sinatra/version.rb
112
+ - sinatra-log.gemspec
113
+ - spec/logger_spec.rb
114
+ - spec/spec_helper.bak.rb
115
+ - spec/spec_helper.rb
116
+ homepage: http://github.com/svrana/sinatra-log
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.4.6
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: ''
140
+ test_files:
141
+ - spec/logger_spec.rb
142
+ - spec/spec_helper.bak.rb
143
+ - spec/spec_helper.rb