green_syslogger 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest CHANGED
@@ -1,6 +1,13 @@
1
1
  Manifest
2
2
  README.md
3
3
  Rakefile
4
- green_syslogger.gemspec
4
+ experimental_test/benchmarks.txt
5
+ experimental_test/green_syslogger_benchmarks_test.rb
6
+ experimental_test/green_syslogger_mutithread_test.rb
7
+ experimental_test/ruby_prof_buffered_logger.txt
8
+ experimental_test/ruby_prof_green_syslogger.txt
9
+ experimental_test/ruby_prof_syslog_logger.txt
10
+ experimental_test/testing_singleton_methods_with_variable.rb
5
11
  lib/green_syslogger.rb
6
12
  test/green_syslogger_test.rb
13
+ test/test_helper.rb
data/README.md CHANGED
@@ -22,10 +22,12 @@ Custom Rails Logger
22
22
  logger = GreenSyslogger.new
23
23
  logger.debug( 'debug message' )
24
24
 
25
- To install it configure it on the <RAILS_ENVIRONMENT>.rb:
26
- config.logger = GreenSyslogger.new(<default tag>, <facility>, <level>)
25
+ To configure it on the <RAILS_ENVIRONMENT>.rb:
26
+
27
+ config.logger = GreenSyslogger.new([<default tag>], [<facility>], [<level>])
27
28
 
28
29
  By default
30
+
29
31
  GreenSyslogger.new('rails', 'local2', :debug)
30
32
 
31
33
  Example for Rails configuration:
@@ -40,9 +42,9 @@ To use it:
40
42
  logger.debug("my debug message")
41
43
  logger.error("my error message")
42
44
 
43
- To user another tag:
45
+ To use another tag:
44
46
 
45
- logger.custom("my custom message", <tag>, <level>)
47
+ logger.custom("my custom message", [<tag>], [<level>])
46
48
 
47
49
  By default:
48
50
 
@@ -54,12 +56,18 @@ In the `GreenSyslogger.custom` there are two reopen connections just to be allow
54
56
 
55
57
  There is not `GreenSyslogger.silencer`. Maybe is a good idea to implemented.
56
58
 
59
+ Benchmarking to compare with other logger libraries and to see with is the cost of the `reopen` calls.
60
+
57
61
  ## Change log
58
62
 
59
63
  ### v 0.2.0
60
64
 
61
65
  * Compatible with `config.logger.auto_flushing = false` so every log of a simple request will be written on an atomic way
62
66
 
67
+ ### v 0.2.1
68
+
69
+ * Avoiding Thread blocking
70
+ * Optimization
63
71
 
64
72
  ## Credits
65
73
 
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('green_syslogger', '0.2.0') do |p|
5
+ Echoe.new('green_syslogger', '0.2.1') do |p|
6
6
  p.description = "Syslogger that makes your life greener"
7
7
  p.url = "https://github.com/SponsorPay/GreenSyslogger"
8
8
  p.author = "Fernando Guillen <http://fernandoguillen.info> & Juan Jose Vidal <@j2vidal>"
@@ -0,0 +1,40 @@
1
+ # normal
2
+ green_syslogger_buffered: 2.640000 0.890000 3.530000 ( 3.668854)
3
+
4
+ # with copy_buffer
5
+ green_syslogger_buffered: 2.620000 0.890000 3.510000 ( 3.607195)
6
+
7
+ # regex comprobation before .gsub!
8
+ green_syslogger_buffered: 2.530000 0.890000 3.420000 ( 3.460330)
9
+
10
+ # with the DATE_FORMAT constant
11
+ green_syslogger_buffered: 2.150000 0.870000 3.020000 ( 3.766759)
12
+ (slower!)
13
+
14
+ # without the 'auto_flush' method call
15
+ green_syslogger_buffered: 2.130000 0.860000 2.990000 ( 3.796795)
16
+ (no changes!!)
17
+
18
+ # MAX_BUFFER=5
19
+ green_syslogger_buffered: 2.220000 0.870000 3.090000 ( 3.590922)
20
+ (faster!)
21
+
22
+ # MAX_BUFFER=1
23
+ green_syslogger_buffered: 2.560000 0.910000 3.470000 ( 4.080794)
24
+ (slower!)
25
+
26
+ # more strings to Constants
27
+ green_syslogger_buffered: 2.140000 0.860000 3.000000 ( 3.641767)
28
+ (faster!)
29
+
30
+ # without .strftime
31
+ green_syslogger_buffered: 2.190000 0.860000 3.050000 ( 3.683527)
32
+ (no changes!)
33
+
34
+ # without print Time
35
+ green_syslogger_buffered: 1.850000 0.880000 2.730000 ( 3.428737)
36
+ (faster!)
37
+
38
+ # without the message = #message
39
+ green_syslogger_buffered: 1.780000 0.890000 2.670000 ( 3.470216)
40
+ (no changes!)
@@ -0,0 +1,61 @@
1
+ require "#{File.dirname(__FILE__)}/../test/test_helper"
2
+
3
+ class GreenSysloggerBenchmarksTest < Test::Unit::TestCase
4
+ # def test_all_loggers
5
+ # logger = Logger.new( "/tmp/log.txt" )
6
+ # syslog_logger = SyslogLogger.new
7
+ # buffered_logger = ActiveSupport::BufferedLogger.new( "/tmp/log2.txt" )
8
+ # buffered_logger.auto_flushing = false
9
+ #
10
+ # n = 50000
11
+ #
12
+ # Benchmark.bm(7) do |x|
13
+ # x.report("logger:") { n.times do ; logger.debug('message'); end }
14
+ # x.report("syslog_logger:") { n.times do ; syslog_logger.debug('message'); end }
15
+ # x.report("buffered_logger:") { n.times do ; buffered_logger.debug('message'); end }
16
+ # end
17
+ #
18
+ # SyslogLogger::SYSLOG.close
19
+ #
20
+ # green_syslogger = GreenSyslogger.new
21
+ # Benchmark.bm(7) do |x|
22
+ # x.report("gren_syslogger:") { n.times do ; green_syslogger.debug('message'); end }
23
+ # end
24
+ #
25
+ # green_syslogger.auto_flushing = false
26
+ # Benchmark.bm(7) do |x|
27
+ # x.report("green_syslogger_buffered:") { n.times do ; green_syslogger.debug('message'); end }
28
+ # end
29
+ #
30
+ # Benchmark.bm(7) do |x|
31
+ # x.report("gren_syslogger_custom:") { n.times do ; green_syslogger.custom('message'); end }
32
+ # end
33
+ #
34
+ # green_syslogger.close
35
+ # end
36
+
37
+ def test_only_green_syslogger
38
+ n = 50000
39
+ green_syslogger = GreenSyslogger.new
40
+ green_syslogger.auto_flushing = false
41
+
42
+ Benchmark.bm(7) do |x|
43
+ x.report("green_syslogger_buffered:") { n.times do ; green_syslogger.debug('message'); end }
44
+ end
45
+
46
+ green_syslogger.close
47
+ end
48
+
49
+ # def test_only_bufffered_syslogger
50
+ # n = 50000
51
+ # logger = BufferedSyslogger.new
52
+ # logger.level = Logger::DEBUG
53
+ #
54
+ # Benchmark.bm(7) do |x|
55
+ # x.report("buffered_syslogger:") { n.times do ; logger.debug('message'); end }
56
+ # end
57
+ #
58
+ # logger.close
59
+ # end
60
+
61
+ end
@@ -0,0 +1,74 @@
1
+ require "#{File.dirname(__FILE__)}/../test/test_helper"
2
+
3
+ class GreenSysloggerMultithreadTest < Test::Unit::TestCase
4
+ def test_multithread
5
+ flushing_times = 5
6
+ flusing_at = 5
7
+
8
+ t1 =
9
+ Thread.new do
10
+ green_syslogger = GreenSyslogger.new
11
+ green_syslogger.auto_flushing = false
12
+
13
+ (1..flushing_times).each do |n|
14
+ (1..flusing_at).each do |nn|
15
+ green_syslogger.debug("thread 1 #{n}-#{nn}")
16
+ end
17
+ green_syslogger.flush
18
+ sleep(0.5)
19
+ end
20
+ end
21
+
22
+ t2 =
23
+ Thread.new do
24
+ green_syslogger = GreenSyslogger.new
25
+ green_syslogger.auto_flushing = false
26
+ (1..flushing_times).each do |n|
27
+ (1..flusing_at).each do |nn|
28
+ green_syslogger.debug("thread 2 #{n}-#{nn}")
29
+ end
30
+ green_syslogger.flush
31
+ sleep(1)
32
+ end
33
+ end
34
+
35
+ while( t1.alive? || t2.alive? ) do
36
+ sleep(1)
37
+ end
38
+ end
39
+
40
+ # def test_multithread_syslog_logger
41
+ # flushing_times = 10
42
+ # flusing_at = 10
43
+ #
44
+ # Thread.new do
45
+ # puts "init thread 1---"
46
+ # green_syslogger = SyslogLogger.new
47
+ #
48
+ # (1..flushing_times).each do |n|
49
+ # (1..flusing_at).each do |nn|
50
+ # green_syslogger.debug("thread 1 #{n}-#{nn}")
51
+ # end
52
+ # # green_syslogger.flush
53
+ # puts "flushed thread 1"
54
+ # sleep(1)
55
+ # end
56
+ # end
57
+ #
58
+ # Thread.new do
59
+ # green_syslogger = SyslogLogger.new
60
+ # (1..flushing_times).each do |n|
61
+ # (1..flusing_at).each do |nn|
62
+ # puts "XXX: thread 2 debug"
63
+ # green_syslogger.debug("thread 2 #{n}-#{nn}")
64
+ # end
65
+ # puts "flushing thread 2"
66
+ # # green_syslogger.flush
67
+ # puts "flushed thread 2"
68
+ # sleep(1)
69
+ # end
70
+ # end
71
+ #
72
+ # sleep(100)
73
+ # end
74
+ end
@@ -0,0 +1,56 @@
1
+ $ ruby-prof green_syslogger_benchmarks.rb
2
+ user system total real
3
+ buffered_logger: 1.420000 1.640000 3.060000 ( 3.073966)
4
+ Thread ID: 2148237740
5
+ Total: 3.096940
6
+
7
+ %self total self wait child calls name
8
+ 29.93 2.75 0.93 0.00 1.83 50000 ActiveSupport::BufferedLogger#add
9
+ 13.99 0.86 0.43 0.00 0.43 100100 ActiveSupport::BufferedLogger#buffer
10
+ 10.02 1.12 0.31 0.00 0.81 50000 ActiveSupport::BufferedLogger#auto_flush
11
+ 7.00 0.32 0.22 0.00 0.10 100218 Hash#[]
12
+ 5.76 2.93 0.18 0.00 2.75 50000 ActiveSupport::BufferedLogger#debug
13
+ 4.19 3.06 0.13 0.00 2.93 1 Integer#times
14
+ 3.62 0.28 0.11 0.00 0.17 84 Array#each
15
+ 3.40 0.11 0.11 0.00 0.00 100150 <Class::Thread>#current
16
+ 3.34 0.10 0.10 0.00 0.00 100200 Kernel#hash
17
+ 3.29 0.16 0.10 0.00 0.06 50000 StringIO#<<
18
+ 1.85 0.06 0.06 0.00 0.00 50012 Fixnum#>
19
+ 1.83 0.06 0.06 0.00 0.00 50001 Array#<<
20
+ 1.81 0.06 0.06 0.00 0.00 50000 StringIO#write
21
+ 1.80 0.06 0.06 0.00 0.00 50047 String#to_s
22
+ 1.80 0.06 0.06 0.00 0.00 50000 String#[]
23
+ 1.76 0.05 0.05 0.00 0.00 50024 Array#size
24
+ 1.75 0.05 0.05 0.00 0.00 50032 Fixnum#==
25
+ 1.66 0.05 0.05 0.00 0.00 50000 Fixnum#>=
26
+ 0.31 0.01 0.01 0.00 0.01 18 Kernel#gem_original_require
27
+ 0.15 0.00 0.00 0.00 0.00 33 <Class::Dir>#[]
28
+ 0.13 0.01 0.00 0.00 0.00 44 Array#select
29
+ 0.06 0.00 0.00 0.00 0.00 53 IO#write
30
+ 0.06 0.00 0.00 0.00 0.00 1572 String#=~
31
+ 0.04 0.27 0.00 0.00 0.27 52 Mutex#synchronize
32
+ 0.02 3.07 0.00 0.00 3.07 1 <Class::GreenSysloggerBenchmarks>#do
33
+ 0.02 0.01 0.00 0.00 0.01 33 Gem::GemPathSearcher#matching_files
34
+ 0.02 0.00 0.00 0.00 0.00 94 String#gsub!
35
+ 0.02 0.00 0.00 0.00 0.00 29 ActiveSupport::Inflector#underscore
36
+ 0.02 0.00 0.00 0.00 0.00 30 ActiveSupport::Autoload#autoload
37
+ 0.01 0.00 0.00 0.00 0.00 12 Integer#upto
38
+ 0.01 0.01 0.00 0.00 0.01 6 <Module::Gem>#activate
39
+ 0.01 0.00 0.00 0.00 0.00 82 Class#new
40
+ 0.01 0.00 0.00 0.00 0.00 250 Kernel#instance_variables
41
+ 0.01 0.00 0.00 0.00 0.00 10 Module#class_eval
42
+ 0.01 0.00 0.00 0.00 0.00 223 Module#method_added
43
+ 0.01 0.00 0.00 0.00 0.00 258 Array#include?
44
+ 0.01 0.00 0.00 0.00 0.00 50 ActiveSupport::BufferedLogger#clear_buffer
45
+ 0.01 0.00 0.00 0.00 0.00 12 Gem::Version#<=>
46
+ 0.01 3.10 0.00 0.00 3.10 1 Kernel#load
47
+ 0.01 0.01 0.00 0.00 0.01 6 Gem::SourceIndex#search
48
+ 0.01 0.00 0.00 0.00 0.00 96 Hash#[]=
49
+ 0.01 0.00 0.00 0.00 0.00 6 Kernel#eval
50
+ 0.01 0.00 0.00 0.00 0.00 50 StringIO#initialize
51
+ 0.01 0.27 0.00 0.00 0.27 50 ActiveSupport::BufferedLogger#flush
52
+ 0.01 0.00 0.00 0.00 0.00 76 Gem::Version#segments
53
+ 0.01 0.00 0.00 0.00 0.00 8 Gem::Dependency#initialize
54
+ 0.01 0.00 0.00 0.00 0.00 50 Hash#delete
55
+ 0.01 0.00 0.00 0.00 0.00 140 Module#===
56
+ 0.00 0.01 0.00 0.00 0.01 33 Gem::Ge
@@ -0,0 +1,79 @@
1
+ $ ruby-prof green_syslogger_benchmarks.rb
2
+ user system total real
3
+ gren_syslogger_buffered: 4.180000 3.630000 7.810000 ( 7.928079)
4
+ Thread ID: 2148237740
5
+ Total: 7.846172
6
+
7
+ %self total self wait child calls name
8
+ 27.15 2.13 2.13 0.00 0.00 50000 <Module::<#Syslog: opened=true, ident="rails", options=1, facility=144, mask=255>>#debug
9
+ 15.62 4.19 1.23 0.00 2.96 131 Array#each
10
+ 10.75 7.43 0.84 0.00 6.58 50000 GreenSyslogger#add
11
+ 5.46 0.85 0.43 0.00 0.43 100100 GreenSyslogger#buffer
12
+ 4.91 0.49 0.39 0.00 0.10 250214 Hash#[]
13
+ 4.87 0.38 0.38 0.00 0.00 350046 Array#[]
14
+ 4.81 0.65 0.38 0.00 0.27 50000 GreenSyslogger#level?
15
+ 4.22 5.04 0.33 0.00 4.71 50000 GreenSyslogger#auto_flush
16
+ 3.54 0.28 0.28 0.00 0.00 50000 Time#strftime
17
+ 3.34 7.69 0.26 0.00 7.43 50000 GreenSyslogger#debug
18
+ 2.06 0.30 0.16 0.00 0.14 50002 <Class::Time>#now
19
+ 1.84 2.27 0.14 0.00 2.13 50000 Kernel#send
20
+ 1.55 7.81 0.12 0.00 7.69 1 Integer#times
21
+ 1.40 0.11 0.11 0.00 0.00 50094 String#gsub!
22
+ 1.35 0.11 0.11 0.00 0.00 100200 <Class::Thread>#current
23
+ 1.33 0.10 0.10 0.00 0.00 100000 Fixnum#>=
24
+ 1.32 0.10 0.10 0.00 0.00 100300 Kernel#hash
25
+ 1.05 0.08 0.08 0.00 0.00 50002 <Class::Time>#allocate
26
+ 0.77 0.06 0.06 0.00 0.00 50092 String#==
27
+ 0.75 0.06 0.06 0.00 0.00 50002 Time#initialize
28
+ 0.73 0.06 0.06 0.00 0.00 50001 Array#<<
29
+ 0.69 0.05 0.05 0.00 0.00 50024 Array#size
30
+ 0.13 0.01 0.01 0.00 0.01 16 Kernel#gem_original_require
31
+ 0.08 0.01 0.01 0.00 0.00 33 <Class::Dir>#[]
32
+ 0.05 0.01 0.00 0.00 0.00 44 Array#select
33
+ 0.02 0.00 0.00 0.00 0.00 1572 String#=~
34
+ 0.01 4.17 0.00 0.00 4.17 102 Mutex#synchronize
35
+ 0.01 4.18 0.00 0.00 4.18 100 GreenSyslogger#flush
36
+ 0.01 0.00 0.00 0.00 0.00 100 GreenSyslogger#clear_buffer
37
+ 0.01 0.01 0.00 0.00 0.01 33 Gem::GemPathSearcher#matching_files
38
+ 0.01 0.00 0.00 0.00 0.00 29 ActiveSupport::Inflector#underscore
39
+ 0.01 0.00 0.00 0.00 0.00 30 ActiveSupport::Autoload#autoload
40
+ 0.01 7.85 0.00 0.00 7.85 1 Kernel#load
41
+ 0.01 0.00 0.00 0.00 0.00 146 Hash#[]=
42
+ 0.01 0.00 0.00 0.00 0.00 100 Hash#delete
43
+ 0.01 0.00 0.00 0.00 0.00 12 Integer#upto
44
+ 0.01 0.01 0.00 0.00 0.01 6 <Module::Gem>#activate
45
+ 0.00 0.00 0.00 0.00 0.00 250 Kernel#instance_variables
46
+ 0.00 0.00 0.00 0.00 0.00 258 Array#include?
47
+ 0.00 0.00 0.00 0.00 0.00 12 Gem::Version#<=>
48
+ 0.00 0.00 0.00 0.00 0.00 192 Module#method_added
49
+ 0.00 0.01 0.00 0.00 0.01 6 Gem::SourceIndex#search
50
+ 0.00 0.00 0.00 0.00 0.00 107 Hash#default
51
+ 0.00 0.00 0.00 0.00 0.00 6 Kernel#eval
52
+ 0.00 0.00 0.00 0.00 0.00 76 Gem::Version#segments
53
+ 0.00 0.00 0.00 0.00 0.00 8 Gem::Dependency#initialize
54
+ 0.00 0.00 0.00 0.00 0.00 6 Hash#values
55
+ 0.00 0.00 0.00 0.00 0.00 140 Module#===
56
+ 0.00 0.03 0.00 0.00 0.03 14 Kernel#require
57
+ 0.00 0.01 0.00 0.00 0.01 33 Gem::GemPathSearcher#matching_file?
58
+ 0.00 0.00 0.00 0.00 0.00 27 Class#new
59
+ 0.00 0.00 0.00 0.00 0.00 12 Module#define_method
60
+ 0.00 0.00 0.00 0.00 0.00 35 Kernel#dup
61
+ 0.00 0.00 0.00 0.00 0.00 53 <Class::File>#join
62
+ 0.00 0.00 0.00 0.00 0.00 7 Module#attr_accessor
63
+ 0.00 0.00 0.00 0.00 0.00 3 Hash#each_key
64
+ 0.00 0.00 0.00 0.00 0.00 11 Array#map
65
+ 0.00 0.00 0.00 0.00 0.00 10 Gem::Specification#full_gem_path
66
+ 0.00 0.00 0.00 0.00 0.00 6 <Class::SyslogLogger>#make_methods
67
+ 0.00 0.00 0.00 0.00 0.00 6 Gem::Requirement#initialize
68
+ 0.00 0.00 0.00 0.00 0.00 66 Symbol#to_s
69
+ 0.00 0.00 0.00 0.00 0.00 1 <Module::<#Syslog: opened=true, ident="rails", options=1, facility=144, mask=255>>#open
70
+ 0.00 0.00 0.00 0.00 0.00 10 Gem::Specification#full_name
71
+ 0.00 0.00 0.00 0.00 0.00 5 Array#index
72
+ 0.00 0.00 0.00 0.00 0.00 13 Module#attr_reader
73
+ 0.00 0.00 0.00 0.00 0.00 33 <Module::Gem>#suffix_pattern
74
+ 0.00 0.00 0.00 0.00 0.00 6 Gem::Version#initialize
75
+ 0.00 0.00 0.00 0.00 0.00 10 Gem::Specification#installation_path
76
+ 0.00 0.00 0.00 0.00 0.00 6 <Class::Gem::Requirement>#parse
77
+ 0.00 0.00 0.00 0.00 0.00 1 ActiveSupport::Autoload#eager_autoload
78
+ 0.00 0.00 0.00 0.00 0.00 10 <Class::File>#directory?
79
+ 0.00 0.00 0.00 0.00 0.00 51 Arr
@@ -0,0 +1,63 @@
1
+ $ ruby-prof green_syslogger_benchmarks.rb
2
+ user system total real
3
+ syslog_logger: 2.230000 1.850000 4.080000 ( 4.161498)
4
+ Thread ID: 2148237740
5
+ Total: 4.119740
6
+
7
+ %self total self wait child calls name
8
+ 51.96 2.14 2.14 0.00 0.00 50000 <Module::<#Syslog: opened=true, ident="rails", options=3, facility=8, mask=255>>#debug
9
+ 15.05 1.18 0.62 0.00 0.56 50000 SyslogLogger#clean
10
+ 12.78 3.90 0.53 0.00 3.37 50000 SyslogLogger#debug
11
+ 4.47 4.08 0.18 0.00 3.90 1 Integer#times
12
+ 4.31 0.29 0.18 0.00 0.11 50035 Kernel#dup
13
+ 3.42 0.14 0.14 0.00 0.00 100094 String#gsub!
14
+ 1.58 0.07 0.07 0.00 0.00 50006 String#strip!
15
+ 1.42 0.06 0.06 0.00 0.00 50030 String#initialize_copy
16
+ 1.41 0.06 0.06 0.00 0.00 50047 String#to_s
17
+ 1.37 0.06 0.06 0.00 0.00 50030 <Class::String>#allocate
18
+ 1.37 0.06 0.06 0.00 0.00 50000 Fixnum#<
19
+ 0.26 0.01 0.01 0.00 0.01 18 Kernel#gem_original_require
20
+ 0.12 0.00 0.00 0.00 0.00 33 <Class::Dir>#[]
21
+ 0.10 0.01 0.00 0.00 0.00 44 Array#select
22
+ 0.04 0.00 0.00 0.00 0.00 1572 String#=~
23
+ 0.03 0.02 0.00 0.00 0.01 34 Array#each
24
+ 0.02 4.09 0.00 0.00 4.09 1 <Class::GreenSysloggerBenchmarks>#do
25
+ 0.01 0.01 0.00 0.00 0.01 33 Gem::GemPathSearcher#matching_files
26
+ 0.01 0.00 0.00 0.00 0.00 10 Module#class_eval
27
+ 0.01 0.00 0.00 0.00 0.00 29 ActiveSupport::Inflector#underscore
28
+ 0.01 0.00 0.00 0.00 0.00 30 ActiveSupport::Autoload#autoload
29
+ 0.01 0.00 0.00 0.00 0.00 12 Integer#upto
30
+ 0.01 0.01 0.00 0.00 0.01 6 <Module::Gem>#activate
31
+ 0.01 0.00 0.00 0.00 0.00 250 Kernel#instance_variables
32
+ 0.01 0.00 0.00 0.00 0.00 223 Module#method_added
33
+ 0.01 0.00 0.00 0.00 0.00 258 Array#include?
34
+ 0.01 0.00 0.00 0.00 0.00 12 Gem::Version#<=>
35
+ 0.01 4.12 0.00 0.00 4.12 1 Kernel#load
36
+ 0.01 0.01 0.00 0.00 0.01 6 Gem::SourceIndex#search
37
+ 0.01 0.00 0.00 0.00 0.00 6 Kernel#eval
38
+ 0.00 0.00 0.00 0.00 0.00 76 Gem::Version#segments
39
+ 0.00 0.00 0.00 0.00 0.00 32 Class#new
40
+ 0.00 0.00 0.00 0.00 0.00 8 Gem::Dependency#initialize
41
+ 0.00 0.00 0.00 0.00 0.00 140 Module#===
42
+ 0.00 0.00 0.00 0.00 0.00 14 Module#attr_reader
43
+ 0.00 0.00 0.00 0.00 0.00 118 Hash#[]
44
+ 0.00 0.03 0.00 0.00 0.03 16 Kernel#require
45
+ 0.00 0.01 0.00 0.00 0.01 33 Gem::GemPathSearcher#matching_file?
46
+ 0.00 0.00 0.00 0.00 0.00 6 Hash#values
47
+ 0.00 0.00 0.00 0.00 0.00 12 Module#define_method
48
+ 0.00 0.00 0.00 0.00 0.00 53 <Class::File>#join
49
+ 0.00 0.00 0.00 0.00 0.00 8 Module#attr_accessor
50
+ 0.00 0.00 0.00 0.00 0.00 78 Symbol#to_s
51
+ 0.00 0.00 0.00 0.00 0.00 3 Hash#each_key
52
+ 0.00 0.00 0.00 0.00 0.00 92 String#==
53
+ 0.00 0.00 0.00 0.00 0.00 11 Array#map
54
+ 0.00 0.00 0.00 0.00 0.00 6 Gem::Requirement#initialize
55
+ 0.00 0.00 0.00 0.00 0.00 10 Gem::Specification#full_gem_path
56
+ 0.00 0.00 0.00 0.00 0.00 6 <Class::SyslogLogger>#make_methods
57
+ 0.00 0.00 0.00 0.00 0.00 1 <Module::<#Syslog: opened=true, ident="rails", options=3, facility=8, mask=255>>#open
58
+ 0.00 0.00 0.00 0.00 0.00 10 Gem::Specification#full_name
59
+ 0.00 0.00 0.00 0.00 0.00 10 Gem::Specification#installation_path
60
+ 0.00 0.00 0.00 0.00 0.00 33 <Module::Gem>#suffix_pattern
61
+ 0.00 0.00 0.00 0.00 0.00 5 Array#index
62
+ 0.00 0.00 0.00 0.00 0.00 6 Gem::Version#initialize
63
+ 0.00 0.00 0.00 0.00 0.00 6 <Class
@@ -0,0 +1,33 @@
1
+ class TestingSingletonMethodsWithVariable
2
+ METHODS = %w(a b c d)
3
+
4
+ def initialize(favorite_method)
5
+ class << self
6
+ METHODS.each do |method_name|
7
+ if( favorite_method == method_name )
8
+ define_method method_name do
9
+ puts "#{method_name} its my favorite method"
10
+ end
11
+ else
12
+ define_method method_name do
13
+ puts "#{method_name} its not my favorite method"
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ t = TestingSingletonMethodsWithVariable.new('b')
22
+ t.a
23
+ t.b
24
+ t.c
25
+ t.d
26
+
27
+
28
+ # $ ruby test/testing_singleton_methods_with_variable.rb
29
+ # test/testing_singleton_methods_with_variable.rb:7:in `initialize': undefined local variable or method `favorite_method' for #<Class:#<TestingSingletonMethodsWithVariable:0x1001a77b8>> (NameError)
30
+ # from test/testing_singleton_methods_with_variable.rb:6:in `each'
31
+ # from test/testing_singleton_methods_with_variable.rb:6:in `initialize'
32
+ # from test/testing_singleton_methods_with_variable.rb:21:in `new'
33
+ # from test/testing_singleton_methods_with_variable.rb:21
@@ -2,22 +2,22 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{green_syslogger}
5
- s.version = "0.2.0"
5
+ s.version = "0.2.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Fernando Guillen <http://fernandoguillen.info> & Juan Jose Vidal <@j2vidal>"]
9
- s.date = %q{2010-11-12}
9
+ s.date = %q{2010-11-17}
10
10
  s.description = %q{Syslogger that makes your life greener}
11
11
  s.email = %q{fguillen.mail@gmail.com}
12
12
  s.extra_rdoc_files = ["README.md", "lib/green_syslogger.rb"]
13
- s.files = ["Manifest", "README.md", "Rakefile", "green_syslogger.gemspec", "lib/green_syslogger.rb", "test/green_syslogger_test.rb"]
13
+ s.files = ["Manifest", "README.md", "Rakefile", "experimental_test/benchmarks.txt", "experimental_test/green_syslogger_benchmarks_test.rb", "experimental_test/green_syslogger_mutithread_test.rb", "experimental_test/ruby_prof_buffered_logger.txt", "experimental_test/ruby_prof_green_syslogger.txt", "experimental_test/ruby_prof_syslog_logger.txt", "experimental_test/testing_singleton_methods_with_variable.rb", "lib/green_syslogger.rb", "test/green_syslogger_test.rb", "test/test_helper.rb", "green_syslogger.gemspec"]
14
14
  s.homepage = %q{https://github.com/SponsorPay/GreenSyslogger}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Green_syslogger", "--main", "README.md"]
16
16
  s.require_paths = ["lib"]
17
17
  s.rubyforge_project = %q{green_syslogger}
18
18
  s.rubygems_version = %q{1.3.7}
19
19
  s.summary = %q{Syslogger that makes your life greener}
20
- s.test_files = ["test/green_syslogger_test.rb"]
20
+ s.test_files = ["test/green_syslogger_test.rb", "test/test_helper.rb"]
21
21
 
22
22
  if s.respond_to? :specification_version then
23
23
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -9,31 +9,37 @@ require 'syslog'
9
9
  # * Posibility to use custom 'tag' only in a concrete point so you can configure Syslog to filter this concrete message to another file
10
10
  # * Compatible with `config.logger.auto_flushing = false` so every log of a simple request will be written on an atomic way
11
11
  #
12
- # To install it configure it on the <RAILS_ENVIRONMENT>.rb:
13
- # config.logger = GreenSyslogger.new(<default tag>, <facility>, <level>)
14
- #
12
+ # require 'green_syslogger'
13
+ # logger = GreenSyslogger.new
14
+ # logger.debug( 'debug message' )
15
+ #
16
+ # To configure it on the <RAILS_ENVIRONMENT>.rb:
17
+ #
18
+ # config.logger = GreenSyslogger.new([<default tag>], [<facility>], [<level>])
19
+ #
15
20
  # By default
21
+ #
16
22
  # GreenSyslogger.new('rails', 'local2', :debug)
17
- #
23
+ #
18
24
  # Example for Rails configuration:
19
- #
20
- # config.log_level = :info
21
- # config.logger = GreenSyslogger.new('myapp', 'local1', config.log_level)
22
- # config.colorize_logging = false
23
- # config.logger.auto_flushing = false
24
- #
25
+ #
26
+ # config.log_level = :info
27
+ # config.logger = GreenSyslogger.new('myapp', 'local1', config.log_level)
28
+ # config.colorize_logging = false
29
+ # config.logger.auto_flushing = false
30
+ #
25
31
  # To use it:
26
- #
27
- # logger.debug("my debug message")
28
- # logger.error("my error message")
29
- #
30
- # To user another tag:
31
- #
32
- # logger.custom("my custom message", <tag>, <level>)
33
- #
32
+ #
33
+ # logger.debug("my debug message")
34
+ # logger.error("my error message")
35
+ #
36
+ # To use another tag:
37
+ #
38
+ # logger.custom("my custom message", [<tag>], [<level>])
39
+ #
34
40
  # By default:
35
- #
36
- # logger.custom("my custom message", 'custom', :info)
41
+ #
42
+ # logger.custom("my custom message", 'custom', :info)
37
43
  #
38
44
  class GreenSyslogger
39
45
  attr_accessor :level
@@ -49,6 +55,14 @@ class GreenSyslogger
49
55
  }
50
56
 
51
57
  DEFAULT_MAX_BUFFER_SIZE = 500
58
+ DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
59
+ SEPARATOR = '---'
60
+ RAILS_REQUEST_INIT = "\n\nStarted"
61
+ RAILS_REQUEST_INIT_CLEANED = "Started"
62
+ RAILS_REQUEST_INIT_REGEX = /\n\nStarted/
63
+
64
+ @@syslog = nil
65
+ @@guard = nil
52
66
 
53
67
  def initialize(tag = 'rails', facility = 'local2', level = :debug)
54
68
  @tag = tag
@@ -56,12 +70,19 @@ class GreenSyslogger
56
70
  @level = level
57
71
  @max_buffer_size = 1
58
72
  @buffer = {}
59
- @guard = Mutex.new
60
- @syslog = Syslog.open(@tag, Syslog::LOG_PID, Syslog.const_get("LOG_#{@facility.upcase}"))
73
+
74
+ if( !@@guard )
75
+ @@guard = Mutex.new
76
+ end
77
+
78
+ if( !@@syslog || !@@syslog.opened? )
79
+ @@syslog = Syslog.open(@tag, Syslog::LOG_PID, Syslog.const_get("LOG_#{@facility.upcase}"))
80
+ end
61
81
  end
62
82
 
63
83
  def close
64
- @syslog.close
84
+ flush
85
+ @@syslog.close
65
86
  end
66
87
 
67
88
  # This method is a kind of a back door, using it you can
@@ -70,12 +91,12 @@ class GreenSyslogger
70
91
  # level default to 'info'
71
92
  # tag default 'ids-custom'
72
93
  def custom(message, tag = 'custom', level = :info)
73
- add(tag, level, message)
94
+ add(tag, level, message) if(level?(level))
74
95
  end
75
96
 
76
97
  LEVELS_MAP.each_key do |level|
77
98
  define_method level do |message|
78
- add(@tag, level, message)
99
+ add(@tag, level, message) if(level?(level))
79
100
  end
80
101
 
81
102
  define_method "#{level}?" do
@@ -94,26 +115,24 @@ class GreenSyslogger
94
115
  end
95
116
 
96
117
  def flush
97
- @guard.synchronize do
98
- buffer.each do |buff|
99
- date = buff[0]
100
- tag = buff[1]
101
- level = buff[2]
102
- message = buff[3]
103
-
104
- message = "[#{date.strftime('%Y-%m-%d %H:%M:%S')}] #{message}"
118
+ @@guard.synchronize do
119
+ unless buffer.empty?
120
+ copy_buffer = buffer
121
+ copy_buffer.each do |date, tag, level, message|
122
+ message = "[#{date.strftime(DATE_FORMAT)}] #{message}"
105
123
 
106
- if(tag == @tag)
107
- @syslog.send(LEVELS_MAP[level][0], message)
108
- else
109
- @syslog = Syslog.reopen(tag, Syslog::LOG_PID, Syslog.const_get("LOG_#{@facility.upcase}"))
110
- @syslog.send(LEVELS_MAP[level][0], message)
111
- @syslog = Syslog.reopen(@tag, Syslog::LOG_PID, Syslog.const_get("LOG_#{@facility.upcase}"))
124
+ if(tag == @tag)
125
+ @@syslog.send(LEVELS_MAP[level][0], message)
126
+ else
127
+ @@syslog = Syslog.reopen(tag, Syslog::LOG_PID, Syslog.const_get("LOG_#{@facility.upcase}"))
128
+ @@syslog.send(LEVELS_MAP[level][0], message)
129
+ @@syslog = Syslog.reopen(@tag, Syslog::LOG_PID, Syslog.const_get("LOG_#{@facility.upcase}"))
130
+ end
112
131
  end
113
132
  end
133
+
134
+ @buffer.delete(Thread.current) # clear buffer
114
135
  end
115
-
116
- clear_buffer
117
136
  end
118
137
 
119
138
 
@@ -123,29 +142,24 @@ class GreenSyslogger
123
142
  LEVELS_MAP[level][1] >= LEVELS_MAP[@level][1]
124
143
  end
125
144
 
126
- def add(tag, level, message)
127
- if(level?(level))
128
- # The init of the request
129
- if( message.gsub!("\n\nStarted", 'Started') )
130
- add(tag, level, '---')
131
- end
132
-
133
- buffer << [Time.now, tag, level, message]
134
- auto_flush
145
+ def add(tag, level, message)
146
+ # The init of the Rails request
147
+ if( message.gsub!(RAILS_REQUEST_INIT, RAILS_REQUEST_INIT_CLEANED) )
148
+ buffer << [Time.now, tag, level, SEPARATOR]
135
149
  end
150
+
151
+ buffer << [Time.now, tag, level, message]
152
+
153
+ auto_flush
136
154
  end
137
-
155
+
138
156
  def auto_flush
139
157
  self.flush if buffer.size >= @max_buffer_size
140
158
  end
141
-
159
+
142
160
  def buffer
143
161
  @buffer[Thread.current] ||= []
144
162
  end
145
-
146
- def clear_buffer
147
- @buffer.delete(Thread.current)
148
- end
149
163
 
150
164
  # This is really only for debbugging
151
165
  def log_to_file(message)
@@ -1,9 +1,4 @@
1
- require 'rubygems'
2
- require 'test/unit'
3
- require 'mocha'
4
- require 'delorean'
5
- require File.expand_path(File.dirname(__FILE__) + '/../lib/green_syslogger')
6
-
1
+ require "#{File.dirname(__FILE__)}/test_helper"
7
2
 
8
3
  class GreenSysloggerTest < Test::Unit::TestCase
9
4
 
@@ -17,7 +12,8 @@ class GreenSysloggerTest < Test::Unit::TestCase
17
12
  end
18
13
 
19
14
  def test_initialize_by_default
20
- Syslog.expects(:open).with('rails', Syslog::LOG_PID, Syslog::LOG_LOCAL2).returns( @logger.instance_eval('@syslog') )
15
+ GreenSyslogger.class_eval('@@syslog').expects(:opened?).returns(false)
16
+ Syslog.expects(:open).with('rails', Syslog::LOG_PID, Syslog::LOG_LOCAL2).returns( GreenSyslogger.class_eval('@@syslog') )
21
17
  @logger = GreenSyslogger.new
22
18
 
23
19
  assert_equal('rails', @logger.instance_eval("@tag"))
@@ -27,7 +23,8 @@ class GreenSysloggerTest < Test::Unit::TestCase
27
23
  end
28
24
 
29
25
  def test_initialize_with_params
30
- Syslog.expects(:open).with('my tag', Syslog::LOG_PID, Syslog::LOG_LOCAL3).returns( @logger.instance_eval('@syslog') )
26
+ GreenSyslogger.class_eval('@@syslog').expects(:opened?).returns(false)
27
+ Syslog.expects(:open).with('my tag', Syslog::LOG_PID, Syslog::LOG_LOCAL3).returns( GreenSyslogger.class_eval('@@syslog') )
31
28
  @logger = GreenSyslogger.new('my tag', 'local3', :info)
32
29
 
33
30
  assert_equal('my tag', @logger.instance_eval("@tag"))
@@ -97,22 +94,31 @@ class GreenSysloggerTest < Test::Unit::TestCase
97
94
  end
98
95
 
99
96
  def test_flush
100
- @syslog = @logger.instance_eval('@syslog')
101
- Syslog.expects(:reopen).returns(@syslog).times(2)
102
- @syslog.expects(:debug).times(2)
103
- @syslog.expects(:info).times(2)
97
+ syslog = GreenSyslogger.class_eval('@@syslog')
98
+
99
+ order = sequence("order")
100
+
101
+ syslog.expects(:debug).with('[2010-10-10 10:10:10] wadus debug').in_sequence(order)
102
+ syslog.expects(:info).with('[2010-10-10 10:10:10] wadus info').in_sequence(order)
103
+ Syslog.expects(:reopen).with('new tag', Syslog::LOG_PID, Syslog::LOG_LOCAL2).returns(syslog).in_sequence(order)
104
+ syslog.expects(:info).with('[2010-10-10 10:10:10] wadus custom tag info').in_sequence(order)
105
+ Syslog.expects(:reopen).with('rails', Syslog::LOG_PID, Syslog::LOG_LOCAL2).returns(syslog).in_sequence(order)
106
+ syslog.expects(:debug).with('[2010-10-10 10:10:10] wadus debug again').in_sequence(order)
104
107
 
105
108
  @logger.auto_flushing = false
106
- @logger.debug('wadus debug')
107
- @logger.info('wadus info')
108
- @logger.custom('wadus custom tag info', 'new tag', :info)
109
- @logger.debug('wadus info again')
110
109
 
110
+ Delorean.time_travel_to('2010-10-10 10:10:10') do
111
+ @logger.debug('wadus debug')
112
+ @logger.info('wadus info')
113
+ @logger.custom('wadus custom tag info', 'new tag', :info)
114
+ @logger.debug('wadus debug again')
115
+ end
116
+
111
117
  @logger.flush
112
118
  end
113
119
 
114
120
  def test_especial_case_init_of_request
115
- @logger.expects(:auto_flush).twice
121
+ @logger.instance_eval('buffer').expects(:<<).twice
116
122
  @logger.info("\n\nStarted")
117
123
  end
118
124
  end
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'mocha'
4
+ require 'delorean'
5
+ require 'logger'
6
+ require 'syslog_logger'
7
+ require 'active_support'
8
+ require 'fileutils'
9
+ require 'benchmark'
10
+ require 'stringio'
11
+ require 'buffered_syslogger'
12
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/green_syslogger')
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: green_syslogger
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 1
10
+ version: 0.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Fernando Guillen <http://fernandoguillen.info> & Juan Jose Vidal <@j2vidal>
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-12 00:00:00 +01:00
18
+ date: 2010-11-17 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -32,9 +32,17 @@ files:
32
32
  - Manifest
33
33
  - README.md
34
34
  - Rakefile
35
- - green_syslogger.gemspec
35
+ - experimental_test/benchmarks.txt
36
+ - experimental_test/green_syslogger_benchmarks_test.rb
37
+ - experimental_test/green_syslogger_mutithread_test.rb
38
+ - experimental_test/ruby_prof_buffered_logger.txt
39
+ - experimental_test/ruby_prof_green_syslogger.txt
40
+ - experimental_test/ruby_prof_syslog_logger.txt
41
+ - experimental_test/testing_singleton_methods_with_variable.rb
36
42
  - lib/green_syslogger.rb
37
43
  - test/green_syslogger_test.rb
44
+ - test/test_helper.rb
45
+ - green_syslogger.gemspec
38
46
  has_rdoc: true
39
47
  homepage: https://github.com/SponsorPay/GreenSyslogger
40
48
  licenses: []
@@ -77,3 +85,4 @@ specification_version: 3
77
85
  summary: Syslogger that makes your life greener
78
86
  test_files:
79
87
  - test/green_syslogger_test.rb
88
+ - test/test_helper.rb