rubysl-logger 2.0.0 → 2.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: 4289a82a3a4e798295bcd9db969140f9cc14fcc9
4
- data.tar.gz: 8fb80a06ecc540a8775fe75fc03c43c0b9a3d051
3
+ metadata.gz: a676ecaf9297d88b65c786ac45d755e611786135
4
+ data.tar.gz: 7262d8960283a6371c3ec363340a70c51fb9745c
5
5
  SHA512:
6
- metadata.gz: a2419fb218cf686cfb4553e437dd7b1156c206736071205780c94243b9e4be3a05302b969e882827f0e23c789b7f72ecf79d3f47a4f081977d1bafd5dabba1e8
7
- data.tar.gz: 13660278cc0dba622a8dc7e3157d0bdb8cedc0a64b8da04ccbfb77f3072c2b24786907a6759e7df074b8ee37ea55454b988df40be7f71bb3b305bc45519656f4
6
+ metadata.gz: ab00e49ed4d0e086b3d339a0e3cdddec8521928262c1aff5ea2a1f756f4acf8821e0f6ff82ba642852f123a443d2597804bd9057fdbf4265a6e7defb2d879070
7
+ data.tar.gz: 691f3510d81d1fd31cb74a3ab5b4a6799f2abf59565107d1e7cb2a4fe6a65f89b795c964302778ad4c3c44aa87563f8e08f70e7df79b53820bc39298441726ef
@@ -1,7 +1,14 @@
1
1
  language: ruby
2
2
  env:
3
3
  - RUBYLIB=lib
4
- script: bundle exec mspec
4
+ - RUBYLIB=
5
+ script: mspec spec
5
6
  rvm:
6
- - 1.9.3
7
- - rbx-nightly-19mode
7
+ - 2.0.0
8
+ - rbx-2.2.1
9
+ matrix:
10
+ exclude:
11
+ - rvm: 2.0.0
12
+ env: RUBYLIB=lib
13
+ - rvm: rbx-2.2.1
14
+ env: RUBYLIB=
@@ -18,18 +18,19 @@ require 'monitor'
18
18
  #
19
19
  # The messages have associated levels, such as +INFO+ or +ERROR+ that indicate
20
20
  # their importance. You can then give the Logger a level, and only messages
21
- # at that level of higher will be printed.
21
+ # at that level or higher will be printed.
22
22
  #
23
23
  # The levels are:
24
24
  #
25
- # +FATAL+:: an unhandleable error that results in a program crash
26
- # +ERROR+:: a handleable error condition
27
- # +WARN+:: a warning
28
- # +INFO+:: generic (useful) information about system operation
29
- # +DEBUG+:: low-level information for developers
25
+ # +UNKNOWN+:: An unknown message that should always be logged.
26
+ # +FATAL+:: An unhandleable error that results in a program crash.
27
+ # +ERROR+:: A handleable error condition.
28
+ # +WARN+:: A warning.
29
+ # +INFO+:: Generic (useful) information about system operation.
30
+ # +DEBUG+:: Low-level information for developers.
30
31
  #
31
32
  # For instance, in a production system, you may have your Logger set to
32
- # +INFO+ or even +WARN+
33
+ # +INFO+ or even +WARN+.
33
34
  # When you are developing the system, however, you probably
34
35
  # want to know about the program's internal state, and would set the Logger to
35
36
  # +DEBUG+.
@@ -51,24 +52,29 @@ require 'monitor'
51
52
  #
52
53
  # === Example
53
54
  #
54
- # This creates a logger to the standard output stream, with a level of +WARN+
55
+ # This creates a Logger that outputs to the standard output stream, with a
56
+ # level of +WARN+:
55
57
  #
56
- # log = Logger.new(STDOUT)
57
- # log.level = Logger::WARN
58
+ # require 'logger'
58
59
  #
59
- # log.debug("Created logger")
60
- # log.info("Program started")
61
- # log.warn("Nothing to do!")
60
+ # logger = Logger.new(STDOUT)
61
+ # logger.level = Logger::WARN
62
+ #
63
+ # logger.debug("Created logger")
64
+ # logger.info("Program started")
65
+ # logger.warn("Nothing to do!")
66
+ #
67
+ # path = "a_non_existent_file"
62
68
  #
63
69
  # begin
64
- # File.each_line(path) do |line|
70
+ # File.foreach(path) do |line|
65
71
  # unless line =~ /^(\w+) = (.*)$/
66
- # log.error("Line in wrong format: #{line}")
72
+ # logger.error("Line in wrong format: #{line.chomp}")
67
73
  # end
68
74
  # end
69
75
  # rescue => err
70
- # log.fatal("Caught exception; exiting")
71
- # log.fatal(err)
76
+ # logger.fatal("Caught exception; exiting")
77
+ # logger.fatal(err)
72
78
  # end
73
79
  #
74
80
  # Because the Logger's level is set to +WARN+, only the warning, error, and
@@ -102,16 +108,16 @@ require 'monitor'
102
108
  # 3. Create a logger for the specified file.
103
109
  #
104
110
  # file = File.open('foo.log', File::WRONLY | File::APPEND)
105
- # # To create new (and to remove old) logfile, add File::CREAT like;
106
- # # file = open('foo.log', File::WRONLY | File::APPEND | File::CREAT)
111
+ # # To create new (and to remove old) logfile, add File::CREAT like:
112
+ # # file = File.open('foo.log', File::WRONLY | File::APPEND | File::CREAT)
107
113
  # logger = Logger.new(file)
108
114
  #
109
- # 4. Create a logger which ages logfile once it reaches a certain size. Leave
110
- # 10 "old log files" and each file is about 1,024,000 bytes.
115
+ # 4. Create a logger which ages the logfile once it reaches a certain size.
116
+ # Leave 10 "old" log files where each file is about 1,024,000 bytes.
111
117
  #
112
118
  # logger = Logger.new('foo.log', 10, 1024000)
113
119
  #
114
- # 5. Create a logger which ages logfile daily/weekly/monthly.
120
+ # 5. Create a logger which ages the logfile daily/weekly/monthly.
115
121
  #
116
122
  # logger = Logger.new('foo.log', 'daily')
117
123
  # logger = Logger.new('foo.log', 'weekly')
@@ -124,13 +130,13 @@ require 'monitor'
124
130
  # +debug+. +add+ is used below to log a message of an arbitrary (perhaps
125
131
  # dynamic) level.
126
132
  #
127
- # 1. Message in block.
133
+ # 1. Message in a block.
128
134
  #
129
135
  # logger.fatal { "Argument 'foo' not given." }
130
136
  #
131
137
  # 2. Message as a string.
132
138
  #
133
- # logger.error "Argument #{ @foo } mismatch."
139
+ # logger.error "Argument #{@foo} mismatch."
134
140
  #
135
141
  # 3. With progname.
136
142
  #
@@ -168,8 +174,7 @@ require 'monitor'
168
174
  #
169
175
  # logger.level = Logger::INFO
170
176
  #
171
- # DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
172
- #
177
+ # # DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
173
178
  #
174
179
  # == Format
175
180
  #
@@ -177,22 +182,22 @@ require 'monitor'
177
182
  # default. The default format and a sample are shown below:
178
183
  #
179
184
  # Log format:
180
- # SeverityID, [Date Time mSec #pid] SeverityLabel -- ProgName: message
185
+ # SeverityID, [DateTime #pid] SeverityLabel -- ProgName: message
181
186
  #
182
187
  # Log sample:
183
- # I, [Wed Mar 03 02:34:24 JST 1999 895701 #19074] INFO -- Main: info.
188
+ # I, [1999-03-03T02:34:24.895701 #19074] INFO -- Main: info.
184
189
  #
185
- # You may change the date and time format via #datetime_format=
190
+ # You may change the date and time format via #datetime_format=.
186
191
  #
187
- # logger.datetime_format = "%Y-%m-%d %H:%M:%S"
192
+ # logger.datetime_format = '%Y-%m-%d %H:%M:%S'
188
193
  # # e.g. "2004-01-03 00:54:26"
189
194
  #
190
- # Or, you may change the overall format with #formatter= method.
195
+ # Or, you may change the overall format via the #formatter= method.
191
196
  #
192
197
  # logger.formatter = proc do |severity, datetime, progname, msg|
193
198
  # "#{datetime}: #{msg}\n"
194
199
  # end
195
- # # e.g. "Thu Sep 22 08:51:08 GMT+9:00 2005: hello world"
200
+ # # e.g. "2005-09-22 08:51:08 +0900: hello world"
196
201
  #
197
202
  class Logger
198
203
  VERSION = "1.2.7"
@@ -213,17 +218,17 @@ class Logger
213
218
 
214
219
  # Logging severity.
215
220
  module Severity
216
- # Low-level information, mostly for developers
221
+ # Low-level information, mostly for developers.
217
222
  DEBUG = 0
218
- # generic, useful information about system operation
223
+ # Generic (useful) information about system operation.
219
224
  INFO = 1
220
- # a warning
225
+ # A warning.
221
226
  WARN = 2
222
- # a handleable error condition
227
+ # A handleable error condition.
223
228
  ERROR = 3
224
- # an unhandleable error that results in a program crash
229
+ # An unhandleable error that results in a program crash.
225
230
  FATAL = 4
226
- # an unknown message that should always be logged
231
+ # An unknown message that should always be logged.
227
232
  UNKNOWN = 5
228
233
  end
229
234
  include Severity
@@ -231,7 +236,7 @@ class Logger
231
236
  # Logging severity threshold (e.g. <tt>Logger::INFO</tt>).
232
237
  attr_accessor :level
233
238
 
234
- # program name to include in log messages.
239
+ # Program name to include in log messages.
235
240
  attr_accessor :progname
236
241
 
237
242
  # Set date-time format.
@@ -249,9 +254,9 @@ class Logger
249
254
  # Logging formatter, as a +Proc+ that will take four arguments and
250
255
  # return the formatted message. The arguments are:
251
256
  #
252
- # +severity+:: The Severity of the log message
253
- # +time+:: A Time instance representing when the message was logged
254
- # +progname+:: The #progname configured, or passed to the logger method
257
+ # +severity+:: The Severity of the log message.
258
+ # +time+:: A Time instance representing when the message was logged.
259
+ # +progname+:: The #progname configured, or passed to the logger method.
255
260
  # +msg+:: The _Object_ the user passed to the log message; not necessarily a
256
261
  # String.
257
262
  #
@@ -284,8 +289,7 @@ class Logger
284
289
  def fatal?; @level <= FATAL; end
285
290
 
286
291
  #
287
- # === Synopsis
288
- #
292
+ # :call-seq:
289
293
  # Logger.new(name, shift_age = 7, shift_size = 1048576)
290
294
  # Logger.new(name, shift_age = 'weekly')
291
295
  #
@@ -317,8 +321,7 @@ class Logger
317
321
  end
318
322
 
319
323
  #
320
- # === Synopsis
321
- #
324
+ # :call-seq:
322
325
  # Logger#add(severity, message = nil, progname = nil) { ... }
323
326
  #
324
327
  # === Args
@@ -336,10 +339,8 @@ class Logger
336
339
  #
337
340
  # === Return
338
341
  #
339
- # +true+ if successful, +false+ otherwise.
340
- #
341
- # When the given severity is not high enough (for this particular logger), log
342
- # no message, and return +true+.
342
+ # When the given severity is not high enough (for this particular logger),
343
+ # log no message, and return +true+.
343
344
  #
344
345
  # === Description
345
346
  #
@@ -358,7 +359,7 @@ class Logger
358
359
  #
359
360
  # * Logfile is not locked.
360
361
  # * Append open does not need to lock file.
361
- # * If the OS which supports multi I/O, records possibly be mixed.
362
+ # * If the OS supports multi I/O, records possibly may be mixed.
362
363
  #
363
364
  def add(severity, message = nil, progname = nil, &block)
364
365
  severity ||= UNKNOWN
@@ -402,18 +403,17 @@ class Logger
402
403
  #
403
404
  # :call-seq:
404
405
  # info(message)
405
- # info(progname,&block)
406
+ # info(progname, &block)
406
407
  #
407
408
  # Log an +INFO+ message.
408
409
  #
409
- # +message+:: the message to log; does not need to be a String
410
- # +progname+:: in the block form, this is the #progname to use in the
411
- # the log message. The default can be set with #progname=
412
- # <tt>&block</tt>:: evaluates to the message to log. This is not evaluated
413
- # unless the logger's level is sufficient
414
- # to log the message. This allows you to create
415
- # potentially expensive logging messages that are
416
- # only called when the logger is configured to show them.
410
+ # +message+:: The message to log; does not need to be a String.
411
+ # +progname+:: In the block form, this is the #progname to use in the
412
+ # log message. The default can be set with #progname=.
413
+ # +block+:: Evaluates to the message to log. This is not evaluated unless
414
+ # the logger's level is sufficient to log the message. This
415
+ # allows you to create potentially expensive logging messages that
416
+ # are only called when the logger is configured to show them.
417
417
  #
418
418
  # === Examples
419
419
  #
@@ -463,7 +463,7 @@ class Logger
463
463
 
464
464
  #
465
465
  # Log an +UNKNOWN+ message. This will be printed no matter what the logger's
466
- # level.
466
+ # level is.
467
467
  #
468
468
  # See #info for more information.
469
469
  #
@@ -480,7 +480,7 @@ class Logger
480
480
 
481
481
  private
482
482
 
483
- # Severity label for logging. (max 5 char)
483
+ # Severity label for logging (max 5 chars).
484
484
  SEV_LABEL = %w(DEBUG INFO WARN ERROR FATAL ANY)
485
485
 
486
486
  def format_severity(severity)
@@ -492,7 +492,7 @@ private
492
492
  end
493
493
 
494
494
 
495
- # Default formatter for log messages
495
+ # Default formatter for log messages.
496
496
  class Formatter
497
497
  Format = "%s, [%s#%d] %5s -- %s: %s\n"
498
498
 
@@ -588,24 +588,32 @@ private
588
588
  private
589
589
 
590
590
  def open_logfile(filename)
591
- if (FileTest.exist?(filename))
591
+ begin
592
592
  open(filename, (File::WRONLY | File::APPEND))
593
- else
593
+ rescue Errno::ENOENT
594
594
  create_logfile(filename)
595
595
  end
596
596
  end
597
597
 
598
598
  def create_logfile(filename)
599
- logdev = open(filename, (File::WRONLY | File::APPEND | File::CREAT))
600
- logdev.sync = true
601
- add_log_header(logdev)
599
+ begin
600
+ logdev = open(filename, (File::WRONLY | File::APPEND | File::CREAT | File::EXCL))
601
+ logdev.flock(File::LOCK_EX)
602
+ logdev.sync = true
603
+ add_log_header(logdev)
604
+ logdev.flock(File::LOCK_UN)
605
+ rescue Errno::EEXIST
606
+ # file is created by another process
607
+ logdev = open_logfile(filename)
608
+ logdev.sync = true
609
+ end
602
610
  logdev
603
611
  end
604
612
 
605
613
  def add_log_header(file)
606
614
  file.write(
607
615
  "# Logfile created on %s by %s\n" % [Time.now.to_s, Logger::ProgName]
608
- )
616
+ ) if file.size == 0
609
617
  end
610
618
 
611
619
  SiD = 24 * 60 * 60
@@ -614,17 +622,53 @@ private
614
622
  if @shift_age.is_a?(Integer)
615
623
  # Note: always returns false if '0'.
616
624
  if @filename && (@shift_age > 0) && (@dev.stat.size > @shift_size)
617
- shift_log_age
625
+ lock_shift_log { shift_log_age }
618
626
  end
619
627
  else
620
628
  now = Time.now
621
629
  period_end = previous_period_end(now)
622
630
  if @dev.stat.mtime <= period_end
623
- shift_log_period(period_end)
631
+ lock_shift_log { shift_log_period(period_end) }
624
632
  end
625
633
  end
626
634
  end
627
635
 
636
+ if /mswin|mingw/ =~ RUBY_PLATFORM
637
+ def lock_shift_log
638
+ yield
639
+ end
640
+ else
641
+ def lock_shift_log
642
+ retry_limit = 8
643
+ retry_sleep = 0.1
644
+ begin
645
+ File.open(@filename, File::WRONLY | File::APPEND) do |lock|
646
+ lock.flock(File::LOCK_EX) # inter-process locking. will be unlocked at closing file
647
+ if File.identical?(@filename, lock) and File.identical?(lock, @dev)
648
+ yield # log shifting
649
+ else
650
+ # log shifted by another process (i-node before locking and i-node after locking are different)
651
+ @dev.close rescue nil
652
+ @dev = open_logfile(@filename)
653
+ @dev.sync = true
654
+ end
655
+ end
656
+ rescue Errno::ENOENT
657
+ # @filename file would not exist right after #rename and before #create_logfile
658
+ if retry_limit <= 0
659
+ warn("log rotation inter-process lock failed. #{$!}")
660
+ else
661
+ sleep retry_sleep
662
+ retry_limit -= 1
663
+ retry_sleep *= 2
664
+ retry
665
+ end
666
+ end
667
+ rescue
668
+ warn("log rotation inter-process lock failed. #{$!}")
669
+ end
670
+ end
671
+
628
672
  def shift_log_age
629
673
  (@shift_age-3).downto(0) do |i|
630
674
  if FileTest.exist?("#{@filename}.#{i}")
@@ -678,17 +722,17 @@ private
678
722
  #
679
723
  # == Description
680
724
  #
681
- # Application -- Add logging support to your application.
725
+ # Logger::Application --- Add logging support to your application.
682
726
  #
683
727
  # == Usage
684
728
  #
685
729
  # 1. Define your application class as a sub-class of this class.
686
- # 2. Override 'run' method in your class to do many things.
687
- # 3. Instantiate it and invoke 'start'.
730
+ # 2. Override the +run+ method in your class to do many things.
731
+ # 3. Instantiate it and invoke #start.
688
732
  #
689
733
  # == Example
690
734
  #
691
- # class FooApp < Application
735
+ # class FooApp < Logger::Application
692
736
  # def initialize(foo_app, application_specific, arguments)
693
737
  # super('FooApp') # Name of the application.
694
738
  # end
@@ -711,9 +755,8 @@ private
711
755
  attr_reader :appname
712
756
 
713
757
  #
714
- # == Synopsis
715
- #
716
- # Application.new(appname = '')
758
+ # :call-seq:
759
+ # Logger::Application.new(appname = '')
717
760
  #
718
761
  # == Args
719
762
  #
@@ -1,5 +1,5 @@
1
1
  module RubySL
2
2
  module Logger
3
- VERSION = "2.0.0"
3
+ VERSION = "2.1.0"
4
4
  end
5
5
  end
@@ -16,7 +16,10 @@ Gem::Specification.new do |spec|
16
16
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
17
  spec.require_paths = ["lib"]
18
18
 
19
+ spec.required_ruby_version = "~> 2.0"
20
+
19
21
  spec.add_development_dependency "bundler", "~> 1.3"
20
22
  spec.add_development_dependency "rake", "~> 10.0"
21
23
  spec.add_development_dependency "mspec", "~> 1.5"
22
- end
24
+ spec.add_development_dependency "rubysl-prettyprint", "~> 2.0"
25
+ end
metadata CHANGED
@@ -1,57 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubysl-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Shirai
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-04 00:00:00.000000000 Z
11
+ date: 2014-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
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: '10.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: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: mspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '1.5'
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: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubysl-prettyprint
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
55
69
  description: Ruby standard library logger.
56
70
  email:
57
71
  - brixen@gmail.com
@@ -59,8 +73,8 @@ executables: []
59
73
  extensions: []
60
74
  extra_rdoc_files: []
61
75
  files:
62
- - .gitignore
63
- - .travis.yml
76
+ - ".gitignore"
77
+ - ".travis.yml"
64
78
  - Gemfile
65
79
  - LICENSE
66
80
  - README.md
@@ -100,17 +114,17 @@ require_paths:
100
114
  - lib
101
115
  required_ruby_version: !ruby/object:Gem::Requirement
102
116
  requirements:
103
- - - '>='
117
+ - - "~>"
104
118
  - !ruby/object:Gem::Version
105
- version: '0'
119
+ version: '2.0'
106
120
  required_rubygems_version: !ruby/object:Gem::Requirement
107
121
  requirements:
108
- - - '>='
122
+ - - ">="
109
123
  - !ruby/object:Gem::Version
110
124
  version: '0'
111
125
  requirements: []
112
126
  rubyforge_project:
113
- rubygems_version: 2.0.7
127
+ rubygems_version: 2.2.2
114
128
  signing_key:
115
129
  specification_version: 4
116
130
  summary: Ruby standard library logger.