semantic_logger 0.0.2

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.
@@ -0,0 +1,48 @@
1
+ #
2
+ # IGNORE THIS FILE
3
+ #
4
+
5
+ module SemanticLogger
6
+ module Appender
7
+ # Logger appender
8
+ #
9
+ # Maps the SemanticLogger API's to the Rails log, or Ruby Logger
10
+ #
11
+ # The following changes are made:
12
+ # The klass_name is prefixed on all log entries if available.
13
+ #
14
+ # Note: For performance, the original Rails Logger or Ruby Logger is returned,
15
+ # with that instance populated with the new API's to support SymanticLogger
16
+ #
17
+ # Installation:
18
+ # Rails.logger = SemanticLogger::LoggerAppender.new(Rails.logger)
19
+ # Also works with the Ruby Logger
20
+ # logger = Logger.new ....
21
+ # Rails.log = LoggerAppender.new()
22
+ #
23
+ # ActiveResource::BufferedLogger
24
+ # ...
25
+ #
26
+ # Append to the default Rails log
27
+ #
28
+ # Note: The Rails log must be initialized prior to this Appender being started
29
+ # Or, the original Rails log must be passed into the initializer
30
+ #
31
+ class Logger
32
+ @@level_map = {:trace => :debug, :debug=>:debug, :info=>:info, :warn=>:warn, :error=>:error}
33
+
34
+ # These are a problem at the appender level, need to move to API
35
+ # Not consistent with the other appenders levels
36
+ def level
37
+ @logger.level
38
+ end
39
+
40
+ def level=(new_level)
41
+ @logger.level = new_level
42
+ end
43
+
44
+
45
+
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,43 @@
1
+ module SemanticLogger #:nodoc:
2
+ class Railtie < Rails::Railtie #:nodoc:
3
+ # Make the SemanticLogger config available in the Rails application config
4
+ #
5
+ # Example: Add the MongoDB logging appender in the Rails environment
6
+ # initializer in file config/environments/development.rb
7
+ #
8
+ # Claritybase::Application.configure do
9
+ # # Add the MongoDB logger appender only once Rails is initialized
10
+ # config.after_initialize do
11
+ # config.semantic_logger.appenders << SemanticLogger::Appender::Mongo.new(
12
+ # :db => Cache::Work.db
13
+ # )
14
+ # end
15
+ # end
16
+ config.semantic_logger = ::SemanticLogger::Logger
17
+
18
+ # Initialize SemanticLogger. In a Rails environment it will automatically
19
+ # insert itself above the configured rails logger to add support for its
20
+ # additional features
21
+ #
22
+ # Also, if Mongoid is installed it will automatically start logging to Mongoid
23
+ #
24
+ # Loaded after Rails logging is initialized since SemanticLogger will continue
25
+ # to forward logging to the Rails Logger
26
+ initializer :initialize_semantic_logger, :after => :initialize_logger do
27
+ config = Rails.application.config
28
+
29
+ # Add the Rails Logger to the list of appenders
30
+ SemanticLogger::Logger.appenders << SemanticLogger::Appender::Logger.new(Rails.logger)
31
+
32
+ # Set the default log level based on the Rails config
33
+ SemanticLogger::Logger.default_level = Rails.configuration.log_level
34
+
35
+ # Replace the default Rails loggers
36
+ Rails.logger = config.logger = SemanticLogger::Logger.new(Rails)
37
+ if defined?(ActiveRecord)
38
+ ActiveRecord::Base.logger = SemanticLogger::Logger.new(ActiveRecord)
39
+ end
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module SemanticLogger #:nodoc
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,18 @@
1
+ # Temp until in Gemfile
2
+ require 'sync_attr'
3
+
4
+ module SemanticLogger
5
+ autoload :Logger, 'semantic_logger/logger'
6
+
7
+ module Appender
8
+ autoload :Logger, 'semantic_logger/appender/logger'
9
+ # Only load the MongoDB appender if the Mongo Ruby Driver is loaded
10
+ if defined?(Mongo)
11
+ autoload :MongoDB, 'semantic_logger/appender/mongodb'
12
+ end
13
+ end
14
+ end
15
+
16
+ if defined?(Rails)
17
+ require 'semantic_logger/railtie'
18
+ end
@@ -0,0 +1,78 @@
1
+ # Allow test to be run in-place without requiring a gem install
2
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
3
+
4
+ require 'rubygems'
5
+ require 'test/unit'
6
+ require 'shoulda'
7
+ require 'logger'
8
+ require 'semantic_logger/appender/logger'
9
+ require 'test/mock_logger'
10
+
11
+ # Unit Test for SemanticLogger::Appender::Logger
12
+ #
13
+ class AppenderLoggerTest < Test::Unit::TestCase
14
+ context SemanticLogger::Appender::Logger do
15
+ setup do
16
+ @time = Time.parse("2012-08-02 09:48:32.482")
17
+ end
18
+
19
+ context "format messages into text form" do
20
+ setup do
21
+ @hash = { :session_id=>"HSSKLEU@JDK767", :tracking_number=>12345 }
22
+ end
23
+
24
+ should "handle nil level, application, message and hash" do
25
+ msg = SemanticLogger::Appender::Logger.format_message(nil, nil, nil, nil)
26
+ assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ \[\d+:\w+\] -- \n/, msg
27
+ end
28
+
29
+ should "handle nil application, message and hash" do
30
+ msg = SemanticLogger::Appender::Logger.format_message(:debug, nil, nil, nil)
31
+ assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ D \[\d+:\w+\] -- \n/, msg
32
+ end
33
+
34
+ should "handle nil message and hash" do
35
+ msg = SemanticLogger::Appender::Logger.format_message(:debug, 'application', nil, nil)
36
+ assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ D \[\d+:\w+\] application -- \n/, msg
37
+ end
38
+
39
+ should "handle nil hash" do
40
+ msg = SemanticLogger::Appender::Logger.format_message(:debug, 'application', 'hello world', nil)
41
+ assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ D \[\d+:\w+\] application -- hello world\n/, msg
42
+ end
43
+
44
+ should "handle hash" do
45
+ msg = SemanticLogger::Appender::Logger.format_message(:debug, 'application', 'hello world', @hash)
46
+ assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ D \[\d+:\w+\] application -- hello world -- \{:session_id=>\"HSSKLEU@JDK767\", :tracking_number=>12345\}\n/, msg
47
+ end
48
+
49
+ should "handle block" do
50
+ msg = SemanticLogger::Appender::Logger.format_message(:debug, 'application', 'hello world', @hash) { "Calculations" }
51
+ assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ D \[\d+:\w+\] application -- hello world -- \{:session_id=>\"HSSKLEU@JDK767\", :tracking_number=>12345\} -- Calculations\n/, msg
52
+ end
53
+
54
+ should "handle block with no other parameters" do
55
+ msg = SemanticLogger::Appender::Logger.format_message(:debug, nil, nil, nil) { "Calculations" }
56
+ assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ D \[\d+:\w+\] -- -- Calculations\n/, msg
57
+ end
58
+ end
59
+
60
+ context "log to Ruby logger" do
61
+ setup do
62
+ @mock_logger = MockLogger.new
63
+ @appender = SemanticLogger::Appender::Logger.new(@mock_logger)
64
+ @hash = { :tracking_number => 12345, :session_id => 'HSSKLEU@JDK767'}
65
+ end
66
+
67
+ # Ensure that any log level can be logged
68
+ Logger::Severity.constants.each do |level|
69
+ should "log #{level.downcase.to_sym} info" do
70
+ @appender.log(level.downcase.to_sym, 'application', 'hello world', @hash) { "Calculations" }
71
+ assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ \w \[\d+:\w+\] application -- hello world -- \{:session_id=>\"HSSKLEU@JDK767\", :tracking_number=>12345\} -- Calculations\n/, @mock_logger.message
72
+ end
73
+ end
74
+
75
+ end
76
+
77
+ end
78
+ end
@@ -0,0 +1,117 @@
1
+ # Allow test to be run in-place without requiring a gem install
2
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
3
+
4
+ require 'rubygems'
5
+ require 'test/unit'
6
+ require 'shoulda'
7
+ require 'logger'
8
+ require 'mongo'
9
+ require 'sync_attr'
10
+ require 'semantic_logger/logger'
11
+ require 'semantic_logger/appender/mongodb'
12
+
13
+ # Unit Test for SemanticLogger::Appender::MongoDB
14
+ #
15
+ class AppenderMongoDBTest < Test::Unit::TestCase
16
+ context SemanticLogger::Appender::MongoDB do
17
+ setup do
18
+ @db = Mongo::Connection.new['test']
19
+ end
20
+
21
+ context "configuration" do
22
+ #TODO verify configuration setting carry through
23
+ end
24
+
25
+ context "formatter" do
26
+ setup do
27
+ @appender = SemanticLogger::Appender::MongoDB.new(
28
+ :db => @db,
29
+ :collection_size => 10*1024**2, # 10MB
30
+ :host_name => 'test',
31
+ :application => 'test_application'
32
+ )
33
+ @time = Time.parse("2012-08-02 09:48:32.482")
34
+ @hash = { :session_id=>"HSSKLEU@JDK767", :tracking_number=>12345 }
35
+ end
36
+
37
+ context "format messages into text form" do
38
+ should "handle nil level, application, message and hash" do
39
+ document = @appender.formatter.call(nil, nil, nil, nil)
40
+ assert_equal({ :level=>nil, :time=>document[:time], :name=>nil, :pid=>$PID, :host_name=>"test", :thread=>document[:thread], :application=>'test_application'}, document)
41
+ end
42
+
43
+ should "handle nil application, message and hash" do
44
+ document = @appender.formatter.call(:debug, nil, nil, nil)
45
+ assert_equal({ :level=>:debug, :time=>document[:time], :name=>nil, :pid=>$PID, :host_name=>"test", :thread=>document[:thread], :application=>'test_application'}, document)
46
+ end
47
+
48
+ should "handle nil message and hash" do
49
+ document = @appender.formatter.call(:debug, nil, nil, @hash)
50
+ assert_equal({ :level=>:debug, :time=>document[:time], :name=>nil, :pid=>$PID, :host_name=>"test", :thread=>document[:thread], :application=>'test_application', :metadata=>{:session_id=>"HSSKLEU@JDK767", :tracking_number=>12345}}, document)
51
+ end
52
+
53
+ should "handle nil hash" do
54
+ document = @appender.formatter.call(:debug, 'myclass', 'hello world', nil)
55
+ assert_equal({ :level=>:debug, :time=>document[:time], :name=>'myclass', :pid=>$PID, :host_name=>"test", :thread=>document[:thread], :application=>'test_application', :message=>'hello world'}, document)
56
+ end
57
+
58
+ should "handle hash" do
59
+ document = @appender.formatter.call(:debug, 'myclass', 'hello world', @hash)
60
+ assert_equal({ :level=>:debug, :time=>document[:time], :name=>'myclass', :pid=>$PID, :host_name=>"test", :thread=>document[:thread], :application=>'test_application', :message=>'hello world', :metadata=>{:session_id=>"HSSKLEU@JDK767", :tracking_number=>12345}}, document)
61
+ end
62
+
63
+ should "handle string block with no message" do
64
+ document = @appender.formatter.call(:debug, 'myclass', nil, @hash, Proc.new { "Calculations" })
65
+ assert_equal({ :level=>:debug, :time=>document[:time], :name=>'myclass', :pid=>$PID, :host_name=>"test", :thread=>document[:thread], :application=>'test_application', :message=>'Calculations', :metadata=>{:session_id=>"HSSKLEU@JDK767", :tracking_number=>12345}}, document)
66
+ end
67
+
68
+ should "handle string block" do
69
+ document = @appender.formatter.call(:debug, 'myclass', 'hello world', @hash, Proc.new { "Calculations" })
70
+ assert_equal({ :level=>:debug, :time=>document[:time], :name=>'myclass', :pid=>$PID, :host_name=>"test", :thread=>document[:thread], :application=>'test_application', :message=>'hello world Calculations', :metadata=>{:session_id=>"HSSKLEU@JDK767", :tracking_number=>12345}}, document)
71
+ end
72
+
73
+ should "handle hash block" do
74
+ document = @appender.formatter.call(:debug, 'myclass', 'hello world', nil, Proc.new { @hash })
75
+ assert_equal({ :level=>:debug, :time=>document[:time], :name=>'myclass', :pid=>$PID, :host_name=>"test", :thread=>document[:thread], :application=>'test_application', :message=>'hello world', :metadata=>{:session_id=>"HSSKLEU@JDK767", :tracking_number=>12345}}, document)
76
+ end
77
+
78
+ should "handle string block with no other parameters" do
79
+ document = @appender.formatter.call(:debug, 'myclass', 'hello world', @hash, Proc.new { "Calculations" })
80
+ assert_equal({ :level=>:debug, :time=>document[:time], :name=>'myclass', :pid=>$PID, :host_name=>"test", :thread=>document[:thread], :application=>'test_application', :message=>'hello world Calculations', :metadata=>{:session_id=>"HSSKLEU@JDK767", :tracking_number=>12345}}, document)
81
+ end
82
+
83
+ should "handle hash block with no other parameters" do
84
+ document = @appender.formatter.call(:debug, nil, nil, nil, Proc.new { @hash.merge(:message => 'hello world') })
85
+ assert_equal({ :level=>:debug, :time=>document[:time], :name=>nil, :pid=>$PID, :host_name=>"test", :thread=>document[:thread], :application=>'test_application', :message=>'hello world', :metadata=>{:session_id=>"HSSKLEU@JDK767", :tracking_number=>12345}}, document)
86
+ end
87
+ end
88
+ end
89
+
90
+ context "log to Mongo logger" do
91
+ setup do
92
+ @appender = SemanticLogger::Appender::MongoDB.new(
93
+ :db => @db,
94
+ :collection_size => 10*1024**2, # 10MB
95
+ :host_name => 'test',
96
+ :application => 'test_application'
97
+ )
98
+ @hash = { :tracking_number => 12345, :session_id => 'HSSKLEU@JDK767'}
99
+ end
100
+
101
+ teardown do
102
+ @appender.purge_all
103
+ end
104
+
105
+ # Ensure that any log level can be logged
106
+ SemanticLogger::Logger::LEVELS.each do |level|
107
+ should "log #{level} information" do
108
+ @appender.log(level, 'my_class', 'hello world', @hash) { "Calculations" }
109
+ document = @appender.collection.find_one
110
+ assert_equal({"_id"=>document['_id'], "level"=>level, "message"=>"hello world", "thread"=>document['thread'], "time"=>document['time'], 'metadata'=>{'session_id'=>"HSSKLEU@JDK767", 'tracking_number'=>12345}, "name"=>"my_class", "pid"=>document['pid'], "host_name"=>"test", "application"=>"test_application"}, document)
111
+ end
112
+ end
113
+
114
+ end
115
+
116
+ end
117
+ end
@@ -0,0 +1,44 @@
1
+ # Allow test to be run in-place without requiring a gem install
2
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
3
+
4
+ require 'rubygems'
5
+ require 'test/unit'
6
+ require 'shoulda'
7
+ require 'logger'
8
+ require 'semantic_logger'
9
+ require 'test/mock_logger'
10
+
11
+ # Unit Test for SemanticLogger::Logger
12
+ #
13
+ class LoggerTest < Test::Unit::TestCase
14
+ context SemanticLogger::Logger do
15
+
16
+ context "log to Ruby and Rails logger" do
17
+ setup do
18
+ # Use a mock logger that just keeps the last logged entry in an instance variable
19
+ @mock_logger = MockLogger.new
20
+ @appender = SemanticLogger::Appender::Logger.new(@mock_logger)
21
+ SemanticLogger::Logger.appenders << @appender
22
+
23
+ # Use this test's class name as the application name in the log output
24
+ @logger = SemanticLogger::Logger.new('LoggerTest', :level => :trace)
25
+
26
+ @hash = { :tracking_number => 12345, :session_id => 'HSSKLEU@JDK767'}
27
+ end
28
+
29
+ teardown do
30
+ SemanticLogger::Logger.appenders.delete(@appender)
31
+ end
32
+
33
+ # Ensure that any log level can be logged
34
+ SemanticLogger::Logger::LEVELS.each do |level|
35
+ should "log #{level} info" do
36
+ @logger.send(level, 'hello world', @hash) { "Calculations" }
37
+ assert_match /\d+-\d+-\d+ \d+:\d+:\d+.\d+ \w \[\d+:\w+\] LoggerTest -- hello world -- \{:session_id=>\"HSSKLEU@JDK767\", :tracking_number=>12345\} -- Calculations\n/, @mock_logger.message
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,25 @@
1
+ # Looks like a standard Ruby Logger or Rails Logger
2
+ # Except that it stores the last logged entry in the instance variable: message
3
+ class MockLogger
4
+ attr_accessor :message
5
+
6
+ Logger::Severity.constants.each do |level|
7
+ class_eval <<-EOT, __FILE__, __LINE__
8
+ def #{level.downcase}(message = nil, progname = nil, &block)
9
+ if message
10
+ self.message = message
11
+ elsif block
12
+ self.message = block.call
13
+ else
14
+ self.message = progname
15
+ end
16
+ self.message
17
+ end
18
+
19
+ def #{level}?
20
+ @true
21
+ end
22
+ EOT
23
+ end
24
+ end
25
+
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: semantic_logger
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
10
+ platform: ruby
11
+ authors:
12
+ - Reid Morrison
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2012-08-08 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: shoulda
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"
30
+ type: :development
31
+ version_requirements: *id001
32
+ description: Logging with additional machine readable and parseable log data
33
+ email:
34
+ - reidmo@gmail.com
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files: []
40
+
41
+ files:
42
+ - FUTURE.rb
43
+ - init.txt
44
+ - lib/semantic_logger/appender/logger.rb
45
+ - lib/semantic_logger/appender/mongodb.rb
46
+ - lib/semantic_logger/logger.rb
47
+ - lib/semantic_logger/rails.rb
48
+ - lib/semantic_logger/railtie.rb
49
+ - lib/semantic_logger/version.rb
50
+ - lib/semantic_logger.rb
51
+ - LICENSE.txt
52
+ - Rakefile
53
+ - README.md
54
+ - test/appender_logger_test.rb
55
+ - test/appender_mongodb_test.rb
56
+ - test/logger_test.rb
57
+ - test/mock_logger.rb
58
+ has_rdoc: true
59
+ homepage: https://github.com/ClarityServices/semantic_logger
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project:
84
+ rubygems_version: 1.3.6
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Semantic Logger for Ruby, and Ruby on Rails
88
+ test_files: []
89
+