semantic_logger 0.6.2 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source :rubygems
2
+
3
+ group :test do
4
+ gem "shoulda"
5
+ gem "mongo"
6
+ gem "bson_ext", :platform => 'ruby'
7
+ end
8
+
9
+ gem "sync_attr"
data/Gemfile.lock ADDED
@@ -0,0 +1,29 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.2.8)
5
+ i18n (~> 0.6)
6
+ multi_json (~> 1.0)
7
+ bson (1.7.0)
8
+ bson_ext (1.7.0)
9
+ bson (~> 1.7.0)
10
+ i18n (0.6.1)
11
+ mongo (1.7.0)
12
+ bson (~> 1.7.0)
13
+ multi_json (1.3.6)
14
+ shoulda (3.3.0)
15
+ shoulda-context (~> 1.0)
16
+ shoulda-matchers (~> 1.4)
17
+ shoulda-context (1.0.0)
18
+ shoulda-matchers (1.4.0)
19
+ activesupport (>= 3.0.0)
20
+ sync_attr (0.1.1)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ bson_ext
27
+ mongo
28
+ shoulda
29
+ sync_attr
data/README.md CHANGED
@@ -1,7 +1,10 @@
1
- semantic-logger-ruby
2
- ====================
1
+ semantic_logger
2
+ ===============
3
3
 
4
- * http://github.com/ClarityServices/semantic-logger-ruby
4
+ Ruby and Rails Logger enhanced with semantic capabilities, high performance, standardized
5
+ logging with multiple appenders
6
+
7
+ * http://github.com/ClarityServices/semantic_logger
5
8
 
6
9
  ### Introduction
7
10
 
@@ -122,7 +125,33 @@ The following output will be written to file:
122
125
  2012-08-30 15:37:29.474 I [48308:ScriptThreadProcess: script/rails] Rails -- Calling external interface (5.2ms)
123
126
 
124
127
  If an exception is raised during the block the exception is logged
125
- at the same log level as the benchmark along with the duration and message
128
+ at the same log level as the benchmark along with the duration and message.
129
+ The exception will flow through to the caller unchanged
130
+
131
+ ```
132
+ Parameters to benchmark_info, benchmark_debug, etc..
133
+
134
+ message
135
+ The message for this log entry
136
+
137
+ params:
138
+ :log_exception
139
+ Control whether or how an exception thrown in the block is
140
+ reported by SemanticLogger. Values:
141
+ :full
142
+ Log the exception class, message, and backtrace
143
+ :partial
144
+ Log the exception class and messag
145
+ The backtrace will not be logged
146
+ :off
147
+ Any unhandled exception from the block will not be logged
148
+
149
+ :min_duration
150
+ Only log if the block takes longer than this duration in ms
151
+
152
+ :payload
153
+ To log a custom payload along with this log entry
154
+ ```
126
155
 
127
156
  #### Logging levels
128
157
 
@@ -384,7 +413,7 @@ specific.
384
413
 
385
414
  ### Install
386
415
 
387
- gem install semantic-logger
416
+ gem install semantic_logger
388
417
 
389
418
  To log to MongoDB
390
419
 
@@ -401,9 +430,9 @@ Want to contribute to Semantic Logger?
401
430
 
402
431
  First clone the repo and run the tests:
403
432
 
404
- git clone git://github.com/ClarityServices/semantic-logger.git
405
- cd semantic-logger
406
- jruby -S rake test
433
+ git clone git://github.com/ClarityServices/semantic_logger.git
434
+ cd semantic_logger
435
+ ruby -S rake test
407
436
 
408
437
  Feel free to ping the mailing list with any issues and we'll try to resolve it.
409
438
 
@@ -412,20 +441,19 @@ Contributing
412
441
 
413
442
  Once you've made your great commits:
414
443
 
415
- 1. [Fork](http://help.github.com/forking/) semantic-logger
444
+ 1. [Fork](http://help.github.com/forking/) semantic_logger
416
445
  2. Create a topic branch - `git checkout -b my_branch`
417
446
  3. Push to your branch - `git push origin my_branch`
418
- 4. Create an [Issue](http://github.com/ClarityServices/semantic-logger/issues) with a link to your branch
447
+ 4. Create an [Issue](http://github.com/ClarityServices/semantic_logger/issues) with a link to your branch
419
448
  5. That's it!
420
449
 
421
450
  Meta
422
451
  ----
423
452
 
424
- * Code: `git clone git://github.com/ClarityServices/semantic-logger.git`
425
- * Home: <https://github.com/ClarityServices/semantic-logger>
426
- * Docs: TODO <http://ClarityServices.github.com/semantic-logger/>
427
- * Bugs: <http://github.com/reidmorrison/semantic-logger/issues>
428
- * Gems: <http://rubygems.org/gems/semantic-logger>
453
+ * Code: `git clone git://github.com/ClarityServices/semantic_logger.git`
454
+ * Home: <https://github.com/ClarityServices/semantic_logger>
455
+ * Bugs: <http://github.com/reidmorrison/semantic_logger/issues>
456
+ * Gems: <http://rubygems.org/gems/semantic_logger>
429
457
 
430
458
  This project uses [Semantic Versioning](http://semver.org/).
431
459
 
@@ -1,6 +1,3 @@
1
- # Include sync_attr dependency
2
- require 'sync_attr'
3
-
4
1
  module SemanticLogger
5
2
  autoload :Base, 'semantic_logger/base'
6
3
  autoload :Logger, 'semantic_logger/logger'
@@ -33,13 +33,13 @@ module SemanticLogger
33
33
  exception = log.payload
34
34
  message << " -- " << "#{exception.class}: #{exception.message}\n#{(exception.backtrace || []).join("\n")}"
35
35
  else
36
- message << " -- " << log.payload.inspect
36
+ message << " -- " << self.class.inspect_payload(log.payload)
37
37
  end
38
38
  end
39
39
 
40
- str = "#{SemanticLogger::Base.formatted_time(log.time)} #{log.level.to_s[0..0].upcase} [#{$$}:#{log.thread_name}] #{tags}#{log.name} -- #{message}"
41
- str << " (#{'%.1f' % log.duration}ms)" if log.duration
42
- str
40
+ duration_str = log.duration ? "(#{'%.1f' % log.duration}ms) " : ''
41
+
42
+ "#{SemanticLogger::Base.formatted_time(log.time)} #{log.level.to_s[0..0].upcase} [#{$$}:#{log.thread_name}] #{tags}#{duration_str}#{log.name} -- #{message}"
43
43
  end
44
44
  end
45
45
 
@@ -113,25 +113,33 @@ module SemanticLogger
113
113
  @level_index <= #{index}
114
114
  end
115
115
 
116
- # Log the duration of the supplied block
117
- # If an exception occurs in the block the exception is logged using the
118
- # same log level. The exception will flow through to the caller unchanged
119
- def benchmark_#{level}(message, payload = nil)
116
+ def benchmark_#{level}(message, params = nil)
120
117
  raise "Mandatory block missing" unless block_given?
118
+ log_exception = params.nil? ? :full : params[:log_exception]
119
+ min_duration = params.nil? ? 0.0 : (params[:min_duration] || 0.0)
120
+ payload = params.nil? ? nil : params[:payload]
121
121
  if @level_index <= #{index}
122
122
  start = Time.now
123
123
  begin
124
124
  result = yield
125
125
  end_time = Time.now
126
- # Add scoped payload
127
- if self.payload
128
- payload = payload.nil? ? self.payload : self.payload.merge(payload)
126
+ duration = 1000.0 * (end_time - start)
127
+
128
+ # Only log if the block took longer than 'min_duration' to complete
129
+ if duration >= min_duration
130
+ # Add scoped payload
131
+ if self.payload
132
+ payload = payload.nil? ? self.payload : self.payload.merge(payload)
133
+ end
134
+ log Log.new(:#{level}, self.class.thread_name, name, message, payload, end_time, duration, tags, #{index})
129
135
  end
130
- log Log.new(:#{level}, self.class.thread_name, name, message, payload, end_time, 1000.0 * (end_time - start), tags, #{index})
131
136
  result
132
137
  rescue Exception => exc
133
- # TODO Need to be able to have both an exception and a Payload
134
- log Log.new(:#{level}, self.class.thread_name, name, message, exc, Time.now, 1000.0 * (Time.now - start), tags, #{index})
138
+ if log_exception == :full
139
+ log Log.new(:#{level}, self.class.thread_name, name, message, exc, Time.now, 1000.0 * (Time.now - start), tags, #{index})
140
+ elsif log_exception == :partial
141
+ log Log.new(:#{level}, self.class.thread_name, name, "\#{message} -- \#{exception.class}: \#{exception.message}", payload, end_time, 1000.0 * (end_time - start), tags, #{index})
142
+ end
135
143
  raise exc
136
144
  end
137
145
  else
@@ -262,6 +270,17 @@ module SemanticLogger
262
270
  end
263
271
  end
264
272
 
273
+ if RUBY_VERSION.to_f >= 1.9
274
+ # With Ruby 1.9 calling .to_s on a hash now returns { 'a' => 1 }
275
+ def self.inspect_payload(payload)
276
+ payload.to_s
277
+ end
278
+ else
279
+ def self.inspect_payload(payload)
280
+ payload.inspect
281
+ end
282
+ end
283
+
265
284
  # Internal method to return the log level as an internal index
266
285
  # Also supports mapping the ::Logger levels to SemanticLogger levels
267
286
  def self.map_level_to_index(level)
@@ -1,3 +1,6 @@
1
+ require 'thread'
2
+ require 'sync_attr'
3
+
1
4
  # Logger is the interface used by
2
5
  #
3
6
  # Logger maintains the logging name to be used for all log entries generated
@@ -63,13 +66,17 @@ module SemanticLogger
63
66
  # logger = SemanticLogger::Logger.new('MyClass')
64
67
  #
65
68
  # Parameters:
66
- # application: A class, module or a string with the application/class name
67
- # to be used in the logger
68
- # options:
69
- # :level The initial log level to start with for this logger instance
70
- def initialize(klass, level=self.class.default_level)
69
+ # application
70
+ # A class, module or a string with the application/class name
71
+ # to be used in the logger
72
+ #
73
+ # level
74
+ # The initial log level to start with for this logger instance
75
+ # Default: SemanticLogger::Logger.default_level
76
+ #
77
+ def initialize(klass, level=nil)
71
78
  @name = klass.is_a?(String) ? klass : klass.name
72
- self.level = level
79
+ self.level = level || self.class.default_level
73
80
  end
74
81
 
75
82
  # Returns [Integer] the number of log entries that have not been written
@@ -1,3 +1,3 @@
1
1
  module SemanticLogger #:nodoc
2
- VERSION = "0.6.2"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -0,0 +1 @@
1
+ platform.active=Ruby_0
@@ -0,0 +1,4 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project-private xmlns="http://www.netbeans.org/ns/project-private/1">
3
+ <editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/1"/>
4
+ </project-private>
@@ -0,0 +1,4 @@
1
+ clean=
2
+ clobber=
3
+ gem=
4
+ test=
@@ -0,0 +1,7 @@
1
+ javac.classpath=
2
+ main.file=main.rb
3
+ platform.active=JRuby
4
+ source.encoding=UTF-8
5
+ spec.src.dir=spec
6
+ src.dir=lib
7
+ test.src.dir=test
@@ -0,0 +1,16 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project xmlns="http://www.netbeans.org/ns/project/1">
3
+ <type>org.netbeans.modules.ruby.rubyproject</type>
4
+ <configuration>
5
+ <data xmlns="http://www.netbeans.org/ns/ruby-project/1">
6
+ <name>semantic_logger</name>
7
+ <source-roots>
8
+ <root id="src.dir"/>
9
+ </source-roots>
10
+ <test-roots>
11
+ <root id="test.src.dir"/>
12
+ <root id="spec.src.dir"/>
13
+ </test-roots>
14
+ </data>
15
+ </configuration>
16
+ </project>
@@ -6,14 +6,14 @@ require 'test/unit'
6
6
  require 'shoulda'
7
7
  require 'logger'
8
8
  require 'semantic_logger'
9
- require 'test/mock_logger'
9
+ require 'stringio'
10
10
 
11
11
  # Unit Test for SemanticLogger::Appender::File
12
12
  #
13
13
  class AppenderFileTest < Test::Unit::TestCase
14
14
  context SemanticLogger::Appender::File do
15
15
  setup do
16
- @time = Time.parse("2012-08-02 09:48:32.482")
16
+ @time = Time.new
17
17
  @io = StringIO.new
18
18
  @appender = SemanticLogger::Appender::File.new(@io)
19
19
  @hash = { :session_id => 'HSSKLEU@JDK767', :tracking_number => 12345 }
@@ -1,19 +1,20 @@
1
1
  # Allow test to be run in-place without requiring a gem install
2
2
  $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
3
+ $LOAD_PATH.unshift File.dirname(__FILE__)
3
4
 
4
5
  require 'rubygems'
5
6
  require 'test/unit'
6
7
  require 'shoulda'
7
8
  require 'logger'
8
9
  require 'semantic_logger'
9
- require 'test/mock_logger'
10
+ require 'mock_logger'
10
11
 
11
12
  # Unit Test for SemanticLogger::Appender::Wrapper
12
13
  #
13
14
  class AppenderWrapperTest < Test::Unit::TestCase
14
15
  context SemanticLogger::Appender::Wrapper do
15
16
  setup do
16
- @time = Time.parse("2012-08-02 09:48:32.482")
17
+ @time = Time.new
17
18
  @mock_logger = MockLogger.new
18
19
  @appender = SemanticLogger::Appender::Wrapper.new(@mock_logger)
19
20
  @hash = { :session_id => 'HSSKLEU@JDK767', :tracking_number => 12345 }
data/test/logger_test.rb CHANGED
@@ -6,7 +6,6 @@ require 'test/unit'
6
6
  require 'shoulda'
7
7
  require 'logger'
8
8
  require 'semantic_logger'
9
- require 'test/mock_logger'
10
9
 
11
10
  # Unit Test for SemanticLogger::Logger
12
11
  class LoggerTest < Test::Unit::TestCase
@@ -71,24 +70,41 @@ class LoggerTest < Test::Unit::TestCase
71
70
  end
72
71
 
73
72
  context "Ruby Logger" do
74
-
75
73
  # Ensure that any log level can be logged
76
74
  Logger::Severity.constants.each do |level|
77
75
  should "log Ruby logger #{level} info" do
78
76
  @logger.level = Logger::Severity.const_get(level)
79
- #assert_equal level.downcase.to_sym, @logger.level
80
- if level == 'UNKNOWN'
77
+ if level.to_s == 'UNKNOWN'
81
78
  assert_equal Logger::Severity.const_get('ERROR')+1, @logger.send(:level_index)
82
79
  else
83
80
  assert_equal Logger::Severity.const_get(level)+1, @logger.send(:level_index)
84
81
  end
85
- # @logger.send(level, 'hello world', @hash) { "Calculations" }
86
- # SemanticLogger::Logger.flush
87
- # assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ \w \[\d+:.+\] LoggerTest -- hello world -- Calculations -- #{@hash_str}/, @mock_logger.message
88
82
  end
89
83
  end
90
84
  end
91
85
 
86
+ context "benchmark" do
87
+ # Ensure that any log level can be benchmarked and logged
88
+ SemanticLogger::LEVELS.each do |level|
89
+ should "log #{level} info" do
90
+ assert_equal "result", @logger.send("benchmark_#{level}".to_sym, 'hello world') { "result" }
91
+ SemanticLogger::Logger.flush
92
+ assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ \w \[\d+:.+\] \(\d+\.\dms\) LoggerTest -- hello world/, @mock_logger.message
93
+ end
94
+
95
+ should "log #{level} info with payload" do
96
+ assert_equal "result", @logger.send("benchmark_#{level}".to_sym, 'hello world', :payload => @hash) { "result" }
97
+ SemanticLogger::Logger.flush
98
+ assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ \w \[\d+:.+\] \(\d+\.\dms\) LoggerTest -- hello world -- #{@hash_str}/, @mock_logger.message
99
+ end
100
+
101
+ should "not log #{level} info when block is faster than :min_duration" do
102
+ assert_equal "result", @logger.send("benchmark_#{level}".to_sym, 'hello world', :min_duration => 0.5) { "result" }
103
+ SemanticLogger::Logger.flush
104
+ assert_nil @mock_logger.message
105
+ end
106
+ end
107
+ end
92
108
 
93
109
  end
94
110
  end
metadata CHANGED
@@ -1,58 +1,59 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: semantic_logger
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 6
8
- - 2
9
- version: 0.6.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.7.0
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Reid Morrison
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2012-09-18 00:00:00 -04:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-10-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: sync_attr
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- requirements:
25
- - - ">="
26
- - !ruby/object:Gem::Version
27
- segments:
28
- - 0
29
- version: "0"
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
30
22
  type: :runtime
31
- version_requirements: *id001
32
- - !ruby/object:Gem::Dependency
33
- name: shoulda
34
23
  prerelease: false
35
- requirement: &id002 !ruby/object:Gem::Requirement
36
- requirements:
37
- - - ">="
38
- - !ruby/object:Gem::Version
39
- segments:
40
- - 0
41
- version: "0"
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: shoulda
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
42
38
  type: :development
43
- version_requirements: *id002
44
- description: Machine readable document oriented logging with support for MongoDB and text files
45
- email:
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: Machine readable document oriented logging with support for MongoDB and
47
+ text files
48
+ email:
46
49
  - reidmo@gmail.com
47
50
  executables: []
48
-
49
51
  extensions: []
50
-
51
52
  extra_rdoc_files: []
52
-
53
- files:
54
- - bson_client.log
53
+ files:
55
54
  - FUTURE.rb
55
+ - Gemfile
56
+ - Gemfile.lock
56
57
  - init.txt
57
58
  - lib/semantic_logger/appender/file.rb
58
59
  - lib/semantic_logger/appender/mongodb.rb
@@ -63,6 +64,12 @@ files:
63
64
  - lib/semantic_logger/version.rb
64
65
  - lib/semantic_logger.rb
65
66
  - LICENSE.txt
67
+ - nbproject/private/config.properties
68
+ - nbproject/private/private.properties
69
+ - nbproject/private/private.xml
70
+ - nbproject/private/rake-d.txt
71
+ - nbproject/project.properties
72
+ - nbproject/project.xml
66
73
  - Rakefile
67
74
  - README.md
68
75
  - test/appender_file_test.rb
@@ -70,35 +77,31 @@ files:
70
77
  - test/appender_wrapper_test.rb
71
78
  - test/logger_test.rb
72
79
  - test/mock_logger.rb
73
- has_rdoc: true
74
80
  homepage: https://github.com/ClarityServices/semantic_logger
75
81
  licenses: []
76
-
77
82
  post_install_message:
78
83
  rdoc_options: []
79
-
80
- require_paths:
84
+ require_paths:
81
85
  - lib
82
- required_ruby_version: !ruby/object:Gem::Requirement
83
- requirements:
84
- - - ">="
85
- - !ruby/object:Gem::Version
86
- segments:
87
- - 0
88
- version: "0"
89
- required_rubygems_version: !ruby/object:Gem::Requirement
90
- requirements:
91
- - - ">="
92
- - !ruby/object:Gem::Version
93
- segments:
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ segments:
94
93
  - 0
95
- version: "0"
94
+ hash: 3405593264048637508
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
96
101
  requirements: []
97
-
98
102
  rubyforge_project:
99
- rubygems_version: 1.3.6
103
+ rubygems_version: 1.8.24
100
104
  signing_key:
101
105
  specification_version: 3
102
106
  summary: Semantic Logger for Ruby, and Ruby on Rails
103
107
  test_files: []
104
-