filum 1.0.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- NDM1NGQzMzc1YzgzN2RhNWEyMjkwYmM3MGRkOTAyM2E1OWExNmQzMQ==
5
- data.tar.gz: !binary |-
6
- MmIyNzE3ZTg4MmNjZmI4ODliYmQwZGViYWRkZjUzMDlkM2U4OTk5OQ==
2
+ SHA1:
3
+ metadata.gz: 426959005772065dfb25532dc64f4e9a947ac84b
4
+ data.tar.gz: 54089585bc896384ad9125ac370073433e842f5e
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- YjUyYWY5NjM1ZGQzMjg3MzBkZTNiODI0N2MyODZhOWMzMGY1OTFkY2E1ZWU5
10
- OGRiZTg4NTMwOGZmMTdhNWUwMGJlZGZjZGI5ZjE4Nzk0YTQ0M2U2ODYzOWFk
11
- ODJhNjIxZDFlMDM0YTI3NTIyY2E5NzQyYjhkMTc2OWZmZDQ1Njc=
12
- data.tar.gz: !binary |-
13
- ZmQzNTlkZGE0OWY3OGZlMjMzN2ZhY2FhMmU2MzMzZmUyZGJmZjRjZmI1NTY5
14
- ZjZhZTE1MjQ1NDA3YTFkYzI2NTU0YzQ1NDA2NTY3Y2RlZDAxMGRlYjRhYjNi
15
- OTZlOTdjZTJhMGYxZjhkY2UwNzI2YWI4NWY4OTEzNjI4ZmFlNjE=
6
+ metadata.gz: ba784e9ac29471e59f74186630f125042abb96a85d6bba4af164bc5953f5fec245ccf084fd3b39d8ce9ba9074f811dd22d153761c019507ca737b24b4760d7d1
7
+ data.tar.gz: e79a649b039a704ed1fc6cb7884c089c750a1d5908754d6544bfed40cb724744b8b36ffe820b552b4a78b1599a56cbc4c2f33ce79c59f34120669ae7fa334242
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ log/
@@ -1,3 +1,6 @@
1
+ 2.0.0 / 2014-02-22
2
+ [FEATURE] Remove config and add Filum.setup
3
+
1
4
  1.0.3 / 2013-12-17
2
5
  [FEATURE] Tidy up formatting to have positional padding
3
6
 
@@ -1,29 +1,20 @@
1
1
  require "filum/version"
2
2
  require "filum/filum_error"
3
- require "filum/configuration"
4
3
  require "filum/log_formatter"
5
4
  require "filum/logger"
6
5
 
7
6
  module Filum
8
7
 
9
- # Filum configuration settings.
8
+ # Filum setup method
10
9
  #
11
- # Settings should be set in an initializer or using some
12
- # other method that insures they are set before any
13
- # Filum code is used. They can be set as followed:
10
+ # This must be called before Filum.logger is used.
14
11
  #
15
- # Filum.config.logfile = "/var/log/mylogfile.log"
12
+ # Options can be
13
+ # * <tt>:config.context_id_length</tt> Defaults to 6
14
+ # * <tt>:filename_length</tt> Defaults to 20
16
15
  #
17
- # The following settings are allowed:
18
- #
19
- # * <tt>:logfile</tt> - The logfile
20
- def self.config
21
- @config ||= Configuration.new
22
- if block_given?
23
- yield @config
24
- else
25
- @config
26
- end
16
+ def self.setup(logfile, options = {})
17
+ @logger = Filum::Logger.new(logfile, options)
27
18
  end
28
19
 
29
20
  # Filum logger.
@@ -33,15 +24,7 @@ module Filum
33
24
  # Filum.logger.info "Log this"
34
25
  #
35
26
  def self.logger
36
- logfile = Filum.config.logfile
37
- dir = File.dirname(logfile)
38
- unless File.directory?(dir)
39
- FileUtils.mkdir_p(dir)
40
- end
41
-
42
- @logger ||= Filum::Logger.new(logfile, shift_age='daily')
43
- @logger.level = Logger::INFO
27
+ raise FilumError.new("Filum is not setup. Please call Filum#setup") unless @logger
44
28
  @logger
45
29
  end
46
-
47
30
  end
@@ -2,13 +2,18 @@ require 'logger'
2
2
 
3
3
  module Filum
4
4
  class LogFormatter < Logger::Formatter
5
+ def initialize(options = {})
6
+ @context_id_length = options.fetch(:context_id_length, 6)
7
+ @filename_length = options.fetch(:filename_length, 20)
8
+ end
9
+
5
10
  def call(severity, timestamp, progname, msg)
6
11
  "#{timestamp} #{formatted_thread_id} [#{formatted_context_id}] #{severity.to_s.ljust(5)} | #{formatted_calling_file_and_line} | #{msg}\n"
7
12
  end
8
13
 
9
14
  private
10
15
  def formatted_context_id
11
- context_id.ljust(Filum.config.context_id_length)
16
+ context_id.ljust(@context_id_length)
12
17
  end
13
18
 
14
19
  def context_id
@@ -16,14 +21,14 @@ module Filum
16
21
  end
17
22
 
18
23
  def formatted_calling_file_and_line
19
- filename_length = Filum.config.filename_length
24
+ filename_length = @filename_length
20
25
  truncated_filename_length = filename_length - 3
21
26
 
22
27
  _, file, line = calling_code.match(/([\w\.]+)\:(\d+)\:in /).to_a
23
- file = "#{file[0,truncated_filename_length]}..." if file.length >= filename_length
28
+ file = "#{file[0,truncated_filename_length]}..." if file.length >= filename_length
24
29
  "#{file}:#{line.ljust(3)}".ljust(filename_length + 4)
25
30
  end
26
-
31
+
27
32
  def formatted_thread_id
28
33
  "t-#{thread_id}".ljust(12)
29
34
  end
@@ -3,13 +3,26 @@ require 'logger'
3
3
  module Filum
4
4
  class Logger < ::Logger
5
5
 
6
- def initialize(*args)
7
- super
8
- self.formatter = Filum::LogFormatter.new
6
+ attr_reader :logfile
7
+ def initialize(logfile, options = {})
8
+ @logfile = logfile
9
+ create_log_directory
10
+
11
+ super(logfile, shift_age='daily')
12
+ self.formatter = Filum::LogFormatter.new(options)
13
+ self.level = Logger::INFO
9
14
  end
10
15
 
11
16
  def context_id=(context_id)
12
17
  Thread.current[:context_id] = context_id
13
18
  end
19
+
20
+ private
21
+ def create_log_directory
22
+ dir = File.dirname(@logfile)
23
+ unless File.directory?(dir)
24
+ FileUtils.mkdir_p(dir)
25
+ end
26
+ end
14
27
  end
15
28
  end
@@ -1,3 +1,3 @@
1
1
  module Filum
2
- VERSION = "1.0.3"
2
+ VERSION = "2.0.0"
3
3
  end
@@ -5,10 +5,7 @@ module Filum
5
5
  class IntegrationTest < Minitest::Test
6
6
  def setup
7
7
  super
8
-
9
- Filum.config do |config|
10
- config.logfile = "/tmp/log/filum_test_#{Time.now.to_i}.log"
11
- end
8
+ Filum.setup("/tmp/log/filum_test_#{Time.now.to_i}.log")
12
9
  end
13
10
  end
14
- end
11
+ end
@@ -38,7 +38,7 @@ module Filum
38
38
  Filum.logger.info "Foobar #{random_string}"
39
39
  assert_logged(/Foobar #{random_string}$/)
40
40
  end
41
-
41
+
42
42
  def test_caller_line_is_correct
43
43
  expected_file = 'logger_test.rb'
44
44
  expected_line = __LINE__+1
@@ -48,7 +48,7 @@ module Filum
48
48
 
49
49
  private
50
50
  def assert_logged(regex)
51
- assert File.readlines(Filum.config.logfile).grep(regex).size == 1
51
+ assert File.readlines(Filum.logger.logfile).grep(regex).size == 1
52
52
  end
53
53
  end
54
54
  end
@@ -12,6 +12,6 @@ require "filum"
12
12
  class Minitest::Test
13
13
  def setup
14
14
  super
15
- Filum.instance_variable_set(:@config, nil)
15
+ Filum.instance_variable_set(:@logger, nil)
16
16
  end
17
17
  end
@@ -0,0 +1,24 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ module Filum
4
+ class FilumTest < Minitest::Test
5
+ def test_setup_proxies_correctly
6
+ logfile = "foobar"
7
+ options = {foo: 'bar'}
8
+ Filum::Logger.expects(:new).with(logfile, options)
9
+ Filum.setup(logfile, options)
10
+ end
11
+
12
+ def test_logger_returns_logger
13
+ logfile = "./log/test.log"
14
+ logger = Filum.setup(logfile)
15
+ assert_equal logger, Filum.logger
16
+ end
17
+
18
+ def test_call_to_loquor_without_setup_raises
19
+ assert_raises(FilumError) do
20
+ Filum.logger
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,9 +1,9 @@
1
1
  require File.expand_path('../../test_helper', __FILE__)
2
2
 
3
3
  module Filum
4
-
4
+
5
5
  class LogFormatterTest < Minitest::Test
6
-
6
+
7
7
  def test_string_format_contains_correct_when_padding_stripped_out
8
8
  formatter = Filum::LogFormatter.new
9
9
  severity = "SEV123"
@@ -19,7 +19,7 @@ module Filum
19
19
  desired = "#{timestamp} t-#{object_id} [#{context_id}] #{severity} | #{file_and_line} | #{msg}\n"
20
20
  assert_equal desired, output.gsub(/[ ]+/, ' ')
21
21
  end
22
-
22
+
23
23
  def test_severity_should_pad_to_5_chars
24
24
  formatter = Filum::LogFormatter.new
25
25
  severity = "SEV"
@@ -29,17 +29,17 @@ module Filum
29
29
  actual_severity_field = output.match(/SEV\s+\|/)[0]
30
30
  assert_equal "SEV |", actual_severity_field
31
31
  end
32
-
32
+
33
33
  def test_can_format_with_nil_severity
34
34
  formatter = Filum::LogFormatter.new
35
35
  formatter.call(nil, "timestamp", nil, "msg")
36
36
  end
37
-
37
+
38
38
  def test_can_format_with_nil_timestamp
39
39
  formatter = Filum::LogFormatter.new
40
40
  formatter.call("SEV", nil, nil, "msg")
41
41
  end
42
-
42
+
43
43
  def test_can_format_with_nil_msg
44
44
  formatter = Filum::LogFormatter.new
45
45
  formatter.call("SEV", "timestamp", nil, nil)
@@ -52,10 +52,9 @@ module Filum
52
52
  end
53
53
 
54
54
  def test_formatted_context_id_uses_config
55
- Filum.config.context_id_length = 20
56
55
  context_id = "12345"
57
56
  Thread.current[:context_id] = context_id
58
- formatter = Filum::LogFormatter.new
57
+ formatter = Filum::LogFormatter.new(context_id_length: 20)
59
58
  output = formatter.send(:formatted_context_id)
60
59
  assert_equal "#{context_id} ", output
61
60
  end
@@ -91,10 +90,9 @@ module Filum
91
90
  end
92
91
 
93
92
  def test_formatted_calling_file_and_line_uses_config
94
- Filum.config.filename_length = 40
95
93
  filename = "abcdefghij"
96
94
  line = "/Users/iHiD/Projects/meducation/filum/lib/filum/#{filename}:30:in `formatted_calling_file_and_line'"
97
- formatter = Filum::LogFormatter.new
95
+ formatter = Filum::LogFormatter.new(filename_length: 40)
98
96
  formatter.stubs(calling_code: line)
99
97
  output = formatter.send(:formatted_calling_file_and_line)
100
98
  assert_equal "#{filename}:30 #{" " * 30}", output
@@ -117,26 +115,26 @@ module Filum
117
115
  output = formatter.send(:formatted_calling_file_and_line)
118
116
  assert_equal "foobar.txt:30 ", output
119
117
  end
120
-
118
+
121
119
  def test_call_calls_formatted_thread_id
122
120
  formatter = Filum::LogFormatter.new
123
121
  formatter.expects(:formatted_thread_id)
124
122
  formatter.call("", "", "", "")
125
123
  end
126
-
124
+
127
125
  def test_formatted_thread_id_should_pad
128
126
  formatter = Filum::LogFormatter.new
129
127
  formatter.stubs(thread_id: "0123456")
130
128
  output = formatter.send(:formatted_thread_id)
131
129
  assert_equal "t-0123456 ", output
132
130
  end
133
-
131
+
134
132
  def test_formatted_thread_id_should_overflow_not_trim
135
133
  formatter = Filum::LogFormatter.new
136
134
  formatter.stubs(thread_id: "01234567890")
137
135
  output = formatter.send(:formatted_thread_id)
138
136
  assert_equal "t-01234567890", output
139
137
  end
140
-
138
+
141
139
  end
142
- end
140
+ end
@@ -0,0 +1,22 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ module Filum
4
+ class FilumTest < Minitest::Test
5
+ def test_initializer_stores_logfile
6
+ logfile = "./log/test.log"
7
+ logger = Filum::Logger.new(logfile)
8
+ assert_equal logfile, logger.logfile
9
+ end
10
+
11
+ def test_initializer_proxies_to_formatter_correctly
12
+ options = {foo: 'bar'}
13
+ Filum::LogFormatter.expects(:new).with(options)
14
+ Filum::Logger.new("./log/test.log", options)
15
+ end
16
+
17
+ def test_initializer_sets_level
18
+ logger = Filum::Logger.new("./log/test.log")
19
+ assert_equal Logger::INFO, logger.level
20
+ end
21
+ end
22
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filum
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - MalcyL
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-17 00:00:00.000000000 Z
11
+ date: 2014-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -28,42 +28,42 @@ dependencies:
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ! '>='
31
+ - - '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ! '>='
38
+ - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: mocha
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ! '>='
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ! '>='
52
+ - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: yard
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ! '>='
59
+ - - '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ! '>='
66
+ - - '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
@@ -95,7 +95,6 @@ files:
95
95
  - Rakefile
96
96
  - filum.gemspec
97
97
  - lib/filum.rb
98
- - lib/filum/configuration.rb
99
98
  - lib/filum/filum_error.rb
100
99
  - lib/filum/log_formatter.rb
101
100
  - lib/filum/logger.rb
@@ -103,8 +102,9 @@ files:
103
102
  - test/integration/integration_test.rb
104
103
  - test/integration/logger_test.rb
105
104
  - test/test_helper.rb
106
- - test/unit/configuration_test.rb
105
+ - test/unit/filum_test.rb
107
106
  - test/unit/log_formatter_test.rb
107
+ - test/unit/logger_test.rb
108
108
  homepage: ''
109
109
  licenses:
110
110
  - AGPL3
@@ -115,12 +115,12 @@ require_paths:
115
115
  - lib
116
116
  required_ruby_version: !ruby/object:Gem::Requirement
117
117
  requirements:
118
- - - ! '>='
118
+ - - '>='
119
119
  - !ruby/object:Gem::Version
120
120
  version: '0'
121
121
  required_rubygems_version: !ruby/object:Gem::Requirement
122
122
  requirements:
123
- - - ! '>='
123
+ - - '>='
124
124
  - !ruby/object:Gem::Version
125
125
  version: '0'
126
126
  requirements: []
@@ -133,6 +133,7 @@ test_files:
133
133
  - test/integration/integration_test.rb
134
134
  - test/integration/logger_test.rb
135
135
  - test/test_helper.rb
136
- - test/unit/configuration_test.rb
136
+ - test/unit/filum_test.rb
137
137
  - test/unit/log_formatter_test.rb
138
+ - test/unit/logger_test.rb
138
139
  has_rdoc:
@@ -1,18 +0,0 @@
1
- module Filum
2
-
3
- class FilumConfigurationError < FilumError
4
- end
5
-
6
- class Configuration
7
-
8
- SETTINGS = [ :logfile, :context_id_length, :filename_length ]
9
- attr_accessor *SETTINGS
10
-
11
- def initialize
12
- self.context_id_length = 6
13
- self.filename_length = 20
14
- end
15
-
16
- end
17
- end
18
-
@@ -1,29 +0,0 @@
1
- require File.expand_path('../../test_helper', __FILE__)
2
-
3
- module Filum
4
- class ConfigurationTest < Minitest::Test
5
-
6
- def setup
7
- Filum.instance_variable_set("@config", nil)
8
- end
9
-
10
- def test_obtaining_singletion
11
- refute Filum.config.nil?
12
- end
13
-
14
- def test_block_syntax
15
- test_logfile = "/tmp/filum.log"
16
- Filum.config do |config|
17
- config.logfile = test_logfile
18
- end
19
- assert_equal test_logfile, Filum.config.logfile
20
- end
21
-
22
- def test_logfile
23
- test_logfile = "/tmp/filum.log"
24
- Filum.config.logfile = test_logfile
25
- assert_equal test_logfile, Filum.config.logfile
26
- end
27
- end
28
- end
29
-