filum 0.0.2 → 0.1.0

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: cc7fea2cacec8aaaa40b2017b9af8fc54c2aa224
4
- data.tar.gz: d93d4e4bca152a0569e2d88be143c508a7fedaa1
3
+ metadata.gz: 83e02d7327a627863f95dbdbb6ace5b57e889223
4
+ data.tar.gz: 0ff4c2261899b313fa192e947ce5acf71de8884f
5
5
  SHA512:
6
- metadata.gz: 23153c103796478843318c97a3bb5753ffca5b824f62c42106e9f518b43a4c614cf5f976699d3d043736ca7e8b5ca764150e983724b197f36271543e55f50daa
7
- data.tar.gz: 7e6d5ce623f159cadefcdc63ffcd11e8e5629dcbf92d70a75125a95d377eeb339bf56b186c933287a989514a4246bba394df4b716d4dfa5dfec661f10567e426
6
+ metadata.gz: 2acc99b793fd25de85715a9011a142ebba8d71641125e6a46181d03e9ee17358bb60c7caf9b1014f58ad93e3e5bfc8c10ce16510c3df3a1611bdd04a4f976ef9
7
+ data.tar.gz: 3dd343497653597debccddecae5751cd585af9f52b50fa682eeda9dbe43425518fa21362b051acdb6b09698f5726e5327340646da86dd1cbaf84393a91e3c4f7
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ 0.1.0 / 2013-12-11
2
+ [FEATURE] Use formatter for logging
3
+
4
+ 0.0.3 / Unreleased
5
+ [FEATURE] Improving log format
@@ -5,11 +5,14 @@ module Filum
5
5
 
6
6
  class Configuration
7
7
 
8
- SETTINGS = [
9
- :logfile
10
- ]
8
+ SETTINGS = [ :logfile, :context_id_length, :filename_length ]
11
9
  attr_accessor *SETTINGS
12
10
 
11
+ def initialize
12
+ self.context_id_length = 6
13
+ self.filename_length = 20
14
+ end
15
+
13
16
  end
14
17
  end
15
18
 
data/lib/filum/logger.rb CHANGED
@@ -2,28 +2,42 @@ require 'logger'
2
2
 
3
3
  module Filum
4
4
  class Logger < ::Logger
5
+
6
+ def initialize(*args)
7
+ super
8
+ self.formatter = Filum::Logger::Formatter.new
9
+ end
10
+
5
11
  def context_id=(context_id)
6
12
  Thread.current[:context_id] = context_id
7
13
  end
8
14
 
9
- def info(str)
10
- super("#{DateTime.now} thread_id-#{Thread.current.object_id} [#{Thread.current[:context_id]}] INFO | #{str} | #{caller[0]}")
11
- end
15
+ class Formatter < Logger::Formatter
16
+ def call(severity, timestamp, progname, msg)
17
+ "#{timestamp} thread_id-#{Thread.current.object_id} [#{formatted_context_id}] #{severity} | #{formatted_calling_file_and_line} | #{msg}\n"
18
+ end
12
19
 
13
- def fatal(str)
14
- super("#{DateTime.now} thread_id-#{Thread.current.object_id} [#{Thread.current[:context_id]}] FATAL | #{str} | #{caller[0]}")
15
- end
20
+ private
21
+ def formatted_context_id
22
+ context_id.ljust(Filum.config.context_id_length)
23
+ end
16
24
 
17
- def error(str)
18
- super("#{DateTime.now} thread_id-#{Thread.current.object_id} [#{Thread.current[:context_id]}] ERROR | #{str} | #{caller[0]}")
19
- end
25
+ def context_id
26
+ Thread.current[:context_id].to_s
27
+ end
20
28
 
21
- def warn(str)
22
- super("#{DateTime.now} thread_id-#{Thread.current.object_id} [#{Thread.current[:context_id]}] WARN | #{str} | #{caller[0]}")
23
- end
29
+ def formatted_calling_file_and_line
30
+ filename_length = Filum.config.filename_length
31
+ truncated_filename_length = filename_length - 3
32
+
33
+ _, file, line = calling_code.match(/([\w\.]+)\:(\d+)\:in /).to_a
34
+ file = "#{file[0,truncated_filename_length]}..." if file.length >= filename_length
35
+ "#{file}:#{line.ljust(3)}".ljust(filename_length + 4)
36
+ end
24
37
 
25
- def debug(str)
26
- super("#{DateTime.now} thread_id-#{Thread.current.object_id} [#{Thread.current[:context_id]}] DEBUG | #{str} | #{caller[0]}")
38
+ def calling_code
39
+ caller[4]
40
+ end
27
41
  end
28
42
  end
29
- end
43
+ end
data/lib/filum/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Filum
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -11,31 +11,127 @@ module Filum
11
11
  class LoggerTest < IntegrationTest
12
12
  def test_one_logline
13
13
  test_thread = Thread.new do
14
- Filum.logger.context_id = " [12345]"
14
+ Filum.logger.context_id = "123456"
15
15
  Worker.process
16
16
  end
17
17
  test_thread.join
18
- assert_logged(/\[12345\]/)
18
+ assert_logged(/\[123456\]/)
19
19
  end
20
20
 
21
21
  def test_multiple_threads
22
22
  test_thread1 = Thread.new do
23
- Filum.logger.context_id = " [23456]"
23
+ Filum.logger.context_id = "23456a"
24
24
  Worker.process
25
25
  end
26
26
  test_thread2 = Thread.new do
27
- Filum.logger.context_id = " [34567]"
27
+ Filum.logger.context_id = "34567a"
28
28
  Worker.process
29
29
  end
30
30
  test_thread1.join
31
31
  test_thread2.join
32
- assert_logged(/\[23456\]/)
33
- assert_logged(/\[34567\]/)
32
+ assert_logged(/\[23456a\]/)
33
+ assert_logged(/\[34567a\]/)
34
34
  end
35
35
 
36
+ def test_info_string_is_correct
37
+ Filum.logger.info "Foobar"
38
+ assert_logged(/Foobar$/)
39
+ end
36
40
 
41
+ private
37
42
  def assert_logged(regex)
38
43
  assert File.readlines(Filum.config.logfile).grep(regex).size == 1
39
44
  end
40
45
  end
46
+
47
+ class LogFormatterTest < IntegrationTest
48
+ def test_string_format
49
+ formatter = Filum::Logger::Formatter.new
50
+ severity = "SEV123"
51
+ timestamp = "Timestamp123"
52
+ msg = "My Message"
53
+ context_id = "context_id"
54
+ object_id = Thread.current.object_id
55
+ file_and_line = "file_and_line"
56
+ formatter.stubs(calling_file_and_line: file_and_line)
57
+ Thread.current[:context_id] = context_id
58
+
59
+ output = formatter.call(severity, timestamp, nil, msg)
60
+ desired = "#{timestamp} thread_id-#{object_id} [#{context_id}] #{severity} | #{file_and_line} | #{msg}\n"
61
+ assert_equal desired.strip.hex, output.strip.hex
62
+ end
63
+
64
+ def test_call_calls_formatted_context_id
65
+ formatter = Filum::Logger::Formatter.new
66
+ formatter.expects(:formatted_context_id)
67
+ formatter.call("", "", "", "")
68
+ end
69
+
70
+ def test_formatted_context_id_uses_config
71
+ Filum.config.context_id_length = 20
72
+ context_id = "12345"
73
+ Thread.current[:context_id] = context_id
74
+ formatter = Filum::Logger::Formatter.new
75
+ output = formatter.send(:formatted_context_id)
76
+ assert_equal "#{context_id} ", output
77
+ end
78
+
79
+ def test_call_calls_formatted_calling_file_and_line
80
+ formatter = Filum::Logger::Formatter.new
81
+ formatter.expects(:formatted_calling_file_and_line)
82
+ formatter.call("", "", "", "")
83
+ end
84
+
85
+ def test_call_should_not_truncate_context_id
86
+ context_id = "context_id"
87
+ Thread.current[:context_id] = context_id
88
+ formatter = Filum::Logger::Formatter.new
89
+ output = formatter.send(:formatted_context_id)
90
+ assert_equal context_id, output
91
+ end
92
+
93
+ def test_call_should_return_fixed_width_context_id
94
+ context_id = "1234"
95
+ Thread.current[:context_id] = context_id
96
+ formatter = Filum::Logger::Formatter.new
97
+ output = formatter.send(:formatted_context_id)
98
+ assert_equal "#{context_id} ", output
99
+ end
100
+
101
+ def test_calling_file_and_line_parses_correctly
102
+ formatter = Filum::Logger::Formatter.new
103
+ line = "/Users/iHiD/Projects/meducation/filum/lib/filum/logger.rb:30:in `formatted_calling_file_and_line'"
104
+ formatter.stubs(calling_code: line)
105
+ output = formatter.send(:formatted_calling_file_and_line)
106
+ assert output =~ /logger\.rb:30\s*/
107
+ end
108
+
109
+ def test_formatted_calling_file_and_line_uses_config
110
+ Filum.config.filename_length = 40
111
+ filename = "abcdefghij"
112
+ line = "/Users/iHiD/Projects/meducation/filum/lib/filum/#{filename}:30:in `formatted_calling_file_and_line'"
113
+ formatter = Filum::Logger::Formatter.new
114
+ formatter.stubs(calling_code: line)
115
+ output = formatter.send(:formatted_calling_file_and_line)
116
+ assert_equal "#{filename}:30 #{" " * 30}", output
117
+ end
118
+
119
+ def test_formatted_calling_file_and_line_should_truncate
120
+ filename = "abcdefghijklmnopqrstuvwxyz1234"
121
+ line = "/Users/iHiD/Projects/meducation/filum/lib/filum/#{filename}:30:in `formatted_calling_file_and_line'"
122
+ formatter = Filum::Logger::Formatter.new
123
+ formatter.stubs(calling_code: line)
124
+ output = formatter.send(:formatted_calling_file_and_line)
125
+ assert_equal "abcdefghijklmnopq...:30 ", output
126
+ end
127
+
128
+ def test_formatted_calling_file_and_line_should_pad
129
+ filename = "foobar.txt"
130
+ line = "/Users/iHiD/Projects/meducation/filum/lib/filum/#{filename}:30:in `formatted_calling_file_and_line'"
131
+ formatter = Filum::Logger::Formatter.new
132
+ formatter.stubs(calling_code: line)
133
+ output = formatter.send(:formatted_calling_file_and_line)
134
+ assert_equal "foobar.txt:30 ", output
135
+ end
136
+ end
41
137
  end
data/test/test_helper.rb CHANGED
@@ -9,3 +9,9 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
9
9
 
10
10
  require "filum"
11
11
 
12
+ class Minitest::Test
13
+ def setup
14
+ super
15
+ Filum.instance_variable_set(:@config, nil)
16
+ end
17
+ 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: 0.0.2
4
+ version: 0.1.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-11-22 00:00:00.000000000 Z
11
+ date: 2013-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -88,6 +88,7 @@ extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
90
  - .gitignore
91
+ - CHANGELOG.md
91
92
  - Gemfile
92
93
  - LICENSE.md
93
94
  - README.md
@@ -122,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
123
  version: '0'
123
124
  requirements: []
124
125
  rubyforge_project:
125
- rubygems_version: 2.0.3
126
+ rubygems_version: 2.1.9
126
127
  signing_key:
127
128
  specification_version: 4
128
129
  summary: Identifies the context of log lines using a context id stored in Thread local