sinatra-log 0.0.1 → 0.1.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 74bad9e3793d9e6ea15b20cb1d885db8e8b9ab16
4
- data.tar.gz: 032e9d08ce73ad442b93e53f1ad2b661bc651dcc
3
+ metadata.gz: d330d9392783361671201bbfd7edd7896a428038
4
+ data.tar.gz: fe9491f7f176995b96ead97d92fb817430d2018c
5
5
  SHA512:
6
- metadata.gz: 384f29484e66a4054fcf75349f73145e6eade5ea53e3be730b5e1ed74a6a80587e39e6ba0f7ddc63573bdcf2a51163597325ddff828a74435a6b17b07c83543f
7
- data.tar.gz: 17dfd01b9926d36c0cce08bd039c52772874ea0873ad29c264c124da40481faf33854120b3d407f6c27e95d7b388755b0aa7fa2234f86feaec4688fe7c7c86de
6
+ metadata.gz: 98a409b0fa7bbc3f053ecb42572ab353c13825df433a4564b33ee1ec67e96da9ef7248e7909beea08d753622e6252a58ed9539f81d8362e2dc7b31fc0bf2c007
7
+ data.tar.gz: 8058cd7f3c28ebf9083cc0a90227ccdc46b9c7e0deff7309ceafe02ee9c196339a14222ee5a55b7dcee02673c7ed3d80faa6d44d7891e8b4af1e0bcda8326015
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # A Sinatra Logger ![Build Status][1]
2
+
3
+ ### History
4
+
5
+ I wrote this wrapper around log4r in 2014 for a small Sinatra project at
6
+ [Lookout][4]. As the project ended, the code was pulled out
7
+ and open sourced by [ismith][3] and included in [lookout-rack-utils][2].
8
+
9
+ ### Changes
10
+
11
+ It is mostly unchanged here, though configuration is done through the logging
12
+ constructor, not through configatron. Graphite logging was also thrown out,
13
+ though that could be re-added if desired. And of course there was a namespace
14
+ change.
15
+
16
+ ### Purpose
17
+
18
+ This was written for usage in Sinatra -- as logging isn't provide outside of
19
+ request context. But it could be used in any Ruby project.
20
+
21
+ [1]: https://api.travis-ci.org/svrana/sinatra-log.svg?branch=master
22
+ [2]: https://github.com/lookout/lookout-rack-utils.git
23
+ [3]: https://github.com/ismith
24
+ [4]: https://lookout.com
data/lib/sinatra/log.rb CHANGED
@@ -3,38 +3,53 @@ require 'log4r'
3
3
  require 'time'
4
4
 
5
5
  module Sinatra
6
- # Logging. Logs to log/<project_name>.log with the format:
6
+ # Logs to specified filename with the format:
7
7
  #
8
8
  # [Log Level]: [Timestamp (ISO-8601)]: [File:linenum]: [Log Message]
9
9
  #
10
- # Use through the helper:
11
- # log.warn 'This is my log message'
10
+ # Access is usually wrapped in a class helper:
12
11
  #
12
+ # class MyProject
13
+ # self.log
14
+ # @logger ||= Sinatra::Log.new(:logger_name => 'myproject',
15
+ # :log_filename => 'myproject/dev.log',
16
+ # :loglevel => 'WARN',
17
+ # :enabled => true,
18
+ # :project_dir => '/var/opt/myproj')
19
+ # end
20
+ # end
13
21
  class Log
14
22
  include Log4r
15
23
 
24
+ REQUIRED_CONFIG_SYMBOLS = [:logger_name, :loglevel, :log_filename,
25
+ :enabled].freeze
26
+
16
27
  # Formatter that include the filename and relative path, and line number in
17
28
  # output of the caller.
18
29
  #
19
30
  # Since all callers go through the methods defined in this class to log, we
20
31
  # look at the second line of the tracer output, removing everything but the
21
32
  # directories after the project directory.
22
- #
23
33
  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__), ".."))
34
+ attr_reader :basedir
35
+
36
+ # @param [String] basedir The base project directory; this directory
37
+ # will be filtered out from each log entry if specified.
38
+ def initialize(basedir = nil)
39
+ super
40
+ @basedir = basedir
31
41
  end
32
42
 
33
43
  # Return a trimmed version of the filename from where a LogEvent occurred
44
+ #
34
45
  # @param [String] tracer A line from the LogEvent#tracer Array
35
46
  # @return [String] Trimmed and parsed version of the file ane line number
36
47
  def event_filename(tracer)
37
- parts = tracer.match(/#{basedir}\/(.*:[0-9]+).*:/)
48
+ if basedir.nil?
49
+ parts = tracer.match(/(.*:[0-9]+).*:/)
50
+ else
51
+ parts = tracer.match(/#{basedir}\/(.*:[0-9]+).*:/)
52
+ end
38
53
 
39
54
  # If we get no matches back, we're probably in a jar file in which case
40
55
  # the format of the tracer is going to be abbreviated
@@ -60,12 +75,12 @@ module Sinatra
60
75
 
61
76
  def initialize(config={})
62
77
  errors = []
63
- [:logger_name, :loglevel, :log_filename, :enabled].each do |key|
78
+ REQUIRED_CONFIG_SYMBOLS.each do |key|
64
79
  if !config.include?(key)
65
80
  errors << "#{key} required, but not specified in config hash"
66
81
  end
67
- raise ArgumentError, "#{errors}" if errors.count > 0
68
82
  end
83
+ raise ArgumentError, "#{errors}" if errors.count > 0
69
84
 
70
85
  logger_name = config[:logger_name].to_s.gsub(/\s+/, '_')
71
86
  @logger = Log4r::Logger.new(logger_name)
@@ -82,7 +97,7 @@ module Sinatra
82
97
  :filename => config[:log_filename],
83
98
  :trunc => false)
84
99
  @logger.trace = true
85
- @outputter.formatter = DefaultFormatter
100
+ @outputter.formatter = DefaultFormatter.new(config[:project_dir])
86
101
  @logger.outputters = @outputter
87
102
  end
88
103
 
@@ -1,5 +1,5 @@
1
1
  module Sinatra
2
2
  module Logger
3
- VERSION = "0.0.1"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
@@ -2,55 +2,15 @@ require 'spec_helper'
2
2
  require 'sinatra/log'
3
3
  require 'timecop'
4
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
5
  describe Sinatra::Log::DefaultFormatter do
45
- subject(:formatter) { described_class.new }
6
+ let(:project_name) { 'some_project' }
7
+ let(:basedir) { "/home/proj/backend/#{project_name}" }
46
8
  let(:logger) do
47
9
  logger = double('Mock Logger')
48
10
  logger.stub(:name).and_return('RSpec Logger')
49
11
  logger.stub(:fullname).and_return('RSpec Logger')
50
12
  logger
51
13
  end
52
- let(:project_name) { 'some_project' }
53
- let(:basedir) { "/home/rspec/#{project_name}" }
54
14
  let(:tracer) do
55
15
  [
56
16
  "#{basedir}/log.rb:63:in `warn'",
@@ -58,23 +18,27 @@ describe Sinatra::Log::DefaultFormatter do
58
18
  ]
59
19
  end
60
20
 
21
+ subject(:formatter) { described_class.new(basedir) }
22
+
61
23
  before :all do
62
24
  # The root logger creates the log levels, so making sure it's been
63
25
  # created
64
26
  Log4r::RootLogger.instance
65
27
  end
66
28
 
67
-
68
- before :each do
69
- formatter.stub(:basedir).and_return(basedir)
70
- end
71
-
72
-
73
29
  describe '#event_filename' do
74
30
  subject(:filename) { formatter.event_filename(tracer[1]) }
75
31
 
76
- context 'with a normal MRI LogEvent' do
77
- it { should eql('spec/log_spec.rb:9') }
32
+ context 'with the project name specified' do
33
+ context 'with a normal MRI LogEvent' do
34
+ it { should eql('spec/log_spec.rb:9') }
35
+ end
36
+ end
37
+
38
+ context 'without the project name specified' do
39
+ let(:formatter) { described_class.new }
40
+
41
+ it { should eql("#{basedir}/spec/log_spec.rb:9") }
78
42
  end
79
43
 
80
44
  # We have slightly different log formats under packaged .jar files
@@ -100,10 +64,7 @@ describe Sinatra::Log::DefaultFormatter do
100
64
  let(:level) { 3 }
101
65
  let(:data) { 'rspec' }
102
66
  let(:timestamp) { Time.now.utc.iso8601 }
103
-
104
- let(:event) do
105
- event = Log4r::LogEvent.new(level, logger, tracer, data)
106
- end
67
+ let(:event) { Log4r::LogEvent.new(level, logger, tracer, data) }
107
68
 
108
69
  it 'should be properly formatted' do
109
70
  expect(formatter.format(event)).to eql("WARN: #{timestamp}: spec/log_spec.rb:9: #{data}\n")
data/spec/log_spec.rb ADDED
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+ require 'sinatra/log'
3
+
4
+ describe Sinatra::Log do
5
+ let(:myconfig) do
6
+ {
7
+ :enabled => true,
8
+ :logger_name => 'mylogger',
9
+ :loglevel => 'DEBUG',
10
+ :log_filename => 'log'
11
+ }
12
+ end
13
+
14
+ subject(:log) { Sinatra::Log.new(myconfig) }
15
+ subject(:log_message) { 'foo' }
16
+
17
+ [:debug, :info, :warn, :error, :fatal].each do |method|
18
+ describe ".#{method}" do
19
+ it 'should invoke the internal logger object with a given block' do
20
+ log.instance_variable_get(:@logger).should_receive(method).with(log_message).and_call_original
21
+ processed = false
22
+ b = Proc.new { processed = true }
23
+ log.send(method, log_message, &b)
24
+ expect(processed).to be(true)
25
+ end
26
+
27
+ it 'should invoke the internal logger object w/o a given block' do
28
+ log.instance_variable_get(:@logger).should_receive(method).with(log_message).and_call_original
29
+ log.send(method, log_message)
30
+ end
31
+ end
32
+ end
33
+
34
+ [:debug?, :info?, :warn?, :error?, :fatal?].each do |method|
35
+ describe ".#{method}" do
36
+ it 'returns true when level is debug' do
37
+ expect(log.send(method)).to eq(true)
38
+ end
39
+ end
40
+ end
41
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-log
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shaw Vrana
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-14 00:00:00.000000000 Z
11
+ date: 2015-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -106,11 +106,13 @@ files:
106
106
  - ".travis.yml"
107
107
  - Gemfile
108
108
  - LICENSE.txt
109
+ - README.md
109
110
  - Rakefile
110
111
  - lib/sinatra/log.rb
111
112
  - lib/sinatra/version.rb
112
113
  - sinatra-log.gemspec
113
- - spec/logger_spec.rb
114
+ - spec/formatter_spec.rb
115
+ - spec/log_spec.rb
114
116
  - spec/spec_helper.bak.rb
115
117
  - spec/spec_helper.rb
116
118
  homepage: http://github.com/svrana/sinatra-log
@@ -138,6 +140,7 @@ signing_key:
138
140
  specification_version: 4
139
141
  summary: ''
140
142
  test_files:
141
- - spec/logger_spec.rb
143
+ - spec/formatter_spec.rb
144
+ - spec/log_spec.rb
142
145
  - spec/spec_helper.bak.rb
143
146
  - spec/spec_helper.rb