lumberjack 1.2.3 → 1.2.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +31 -8
- data/{MIT_LICENSE → MIT_LICENSE.txt} +0 -0
- data/README.md +11 -8
- data/VERSION +1 -1
- data/lib/lumberjack.rb +38 -28
- data/lib/lumberjack/device.rb +12 -12
- data/lib/lumberjack/device/date_rolling_log_file.rb +7 -7
- data/lib/lumberjack/device/log_file.rb +6 -6
- data/lib/lumberjack/device/multi.rb +6 -6
- data/lib/lumberjack/device/null.rb +1 -1
- data/lib/lumberjack/device/rolling_log_file.rb +43 -19
- data/lib/lumberjack/device/size_rolling_log_file.rb +9 -9
- data/lib/lumberjack/device/writer.rb +12 -12
- data/lib/lumberjack/formatter.rb +15 -15
- data/lib/lumberjack/formatter/date_time_formatter.rb +2 -3
- data/lib/lumberjack/formatter/exception_formatter.rb +0 -2
- data/lib/lumberjack/formatter/id_formatter.rb +2 -2
- data/lib/lumberjack/formatter/pretty_print_formatter.rb +4 -4
- data/lib/lumberjack/formatter/structured_formatter.rb +31 -16
- data/lib/lumberjack/log_entry.rb +8 -10
- data/lib/lumberjack/logger.rb +90 -28
- data/lib/lumberjack/rack.rb +3 -3
- data/lib/lumberjack/rack/request_id.rb +1 -1
- data/lib/lumberjack/rack/unit_of_work.rb +1 -1
- data/lib/lumberjack/severity.rb +1 -2
- data/lib/lumberjack/tag_formatter.rb +1 -3
- data/lib/lumberjack/tagged_logger_support.rb +0 -1
- data/lib/lumberjack/tagged_logging.rb +29 -0
- data/lib/lumberjack/tags.rb +1 -1
- data/lib/lumberjack/template.rb +2 -2
- data/lumberjack.gemspec +12 -16
- metadata +5 -46
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c7912cf1bfe32dcaa911a6a65a0b755fb3051eebc2a7e4a65ff5d422f2f61ad7
|
4
|
+
data.tar.gz: b4077d2ef97be7169ed0e7bca7d107ec3f42372dde45cae1e7cdcedae1fb4e61
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a092775aec0c7670eda8ff4959a7cc928a57b0934ecc8a1190efe395b437bc0a681bdbd2f3455b98f3cf757904f5f39b31c96e24e9c8bcd7a481b806027e92c8
|
7
|
+
data.tar.gz: 885f76da2738f3f6236a363d9a7dea68f83a80a03e123a3c52c578984f963e959de6fd60d0fd222d9a9933c22ed959181538abb4118163c8fd6528292e88673c
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,26 @@
|
|
1
|
+
## 1.2.8
|
2
|
+
|
3
|
+
* Add `Logger#untagged` to remove previously set logging tags from a block.
|
4
|
+
* Return result of the block when a block is passed to `Logger#tag`.
|
5
|
+
|
6
|
+
## 1.2.7
|
7
|
+
|
8
|
+
* Allow passing frozen hashes to `Logger#tag`. Tags passed to this method are now duplicated so the logger maintains it's own copy of the hash.
|
9
|
+
|
10
|
+
## 1.2.6
|
11
|
+
|
12
|
+
* Fix `Logger#tag` so it only ads to the current block's logger tags instead of the global tags if called inside a `Logger#tag` block.
|
13
|
+
* Add Logger#remove_tag
|
14
|
+
|
15
|
+
## 1.2.5
|
16
|
+
|
17
|
+
* Fix logic with recursive reference guard in StructuredFormatter so it only suppresses Enumerable references.
|
18
|
+
* Add support for bang methods (error!) for setting the log level.
|
19
|
+
|
20
|
+
## 1.2.4
|
21
|
+
|
22
|
+
* Enhance `ActiveSupport::TaggedLogging` support so code that Lumberjack loggers can be wrapped with a tagged logger.
|
23
|
+
|
1
24
|
## 1.2.3
|
2
25
|
|
3
26
|
* Fix structured formatter so no-recursive, duplicate references are allowed.
|
@@ -12,7 +35,7 @@
|
|
12
35
|
|
13
36
|
## 1.2.0
|
14
37
|
|
15
|
-
* Enable compatibility with ActiveSupport::TaggedLogger by calling `tagged_logger!` on a logger.
|
38
|
+
* Enable compatibility with `ActiveSupport::TaggedLogger` by calling `tagged_logger!` on a logger.
|
16
39
|
* Add `tag_formatter` to logger to specify formatting of tags for output.
|
17
40
|
* Allow adding and removing classes by name to formatters.
|
18
41
|
* Allow adding and removing multiple classes in a single call to a formatter.
|
@@ -31,7 +54,7 @@
|
|
31
54
|
|
32
55
|
## 1.1.0
|
33
56
|
|
34
|
-
* Change Lumberjack::Logger to inherit from ::Logger
|
57
|
+
* Change `Lumberjack::Logger` to inherit from ::Logger
|
35
58
|
* Add support for tags on log messages
|
36
59
|
* Add global tag context for all loggers
|
37
60
|
* Add per logger tags and tag contexts
|
@@ -39,8 +62,8 @@
|
|
39
62
|
* Add support for setting datetime format on log devices
|
40
63
|
* Performance optimizations
|
41
64
|
* Add Multi device to output to multiple devices
|
42
|
-
* Add DateTimeFormatter
|
43
|
-
* Add rack Context middleware for setting thread global context
|
65
|
+
* Add `DateTimeFormatter`, `IdFormatter`, `ObjectFormatter`, and `StructuredFormatter`
|
66
|
+
* Add rack `Context` middleware for setting thread global context
|
44
67
|
* End support for ruby versions < 2.3
|
45
68
|
* Add support for modules in formatters
|
46
69
|
|
@@ -52,7 +75,7 @@
|
|
52
75
|
|
53
76
|
## 1.0.12
|
54
77
|
|
55
|
-
* Add support for ActionDispatch request id for better Rails compatibility.
|
78
|
+
* Add support for `ActionDispatch` request id for better Rails compatibility.
|
56
79
|
|
57
80
|
## 1.0.11
|
58
81
|
|
@@ -66,11 +89,11 @@
|
|
66
89
|
|
67
90
|
## 1.0.9
|
68
91
|
|
69
|
-
* Add method so Formatter is compatible with ActiveSupport logging extensions.
|
92
|
+
* Add method so Formatter is compatible with `ActiveSupport` logging extensions.
|
70
93
|
|
71
94
|
## 1.0.8
|
72
95
|
|
73
|
-
* Fix another internal variable name conflict with ActiveSupport logging extensions.
|
96
|
+
* Fix another internal variable name conflict with `ActiveSupport` logging extensions.
|
74
97
|
|
75
98
|
## 1.0.7
|
76
99
|
|
@@ -78,7 +101,7 @@
|
|
78
101
|
|
79
102
|
## 1.0.6
|
80
103
|
|
81
|
-
* Fix internal variable name conflict with ActiveSupport logging extensions.
|
104
|
+
* Fix internal variable name conflict with `ActiveSupport` logging extensions.
|
82
105
|
|
83
106
|
## 1.0.5
|
84
107
|
|
File without changes
|
data/README.md
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
[![Build Status](https://travis-ci.org/bdurand/lumberjack.svg?branch=master)](https://travis-ci.org/bdurand/lumberjack)
|
1
|
+
![Continuous Integration](https://github.com/bdurand/lumberjack/workflows/Continuous%20Integration/badge.svg)
|
4
2
|
[![Maintainability](https://api.codeclimate.com/v1/badges/a0abc03721fff9b0cde1/maintainability)](https://codeclimate.com/github/bdurand/lumberjack/maintainability)
|
3
|
+
[![Ruby Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://github.com/testdouble/standard)
|
4
|
+
|
5
|
+
# Lumberjack
|
5
6
|
|
6
7
|
Lumberjack is a simple, powerful, and fast logging implementation in Ruby. It uses nearly the same API as the Logger class in the Ruby standard library and as ActiveSupport::BufferedLogger in Rails.
|
7
8
|
|
@@ -59,8 +60,10 @@ You can specify tags that will only be applied to the logger in a block as well.
|
|
59
60
|
```ruby
|
60
61
|
logger.tag(thread_id: Thread.current.object_id) do
|
61
62
|
logger.info("here") # Will include the `thread_id` tag
|
63
|
+
logger.tag(count: 15)
|
64
|
+
logger.info("with count") # Will include the `count` tag
|
62
65
|
end
|
63
|
-
logger.info("there") # Will not include the `thread_id` tag
|
66
|
+
logger.info("there") # Will not include the `thread_id` or `count` tag
|
64
67
|
```
|
65
68
|
|
66
69
|
You can also set tags to `Proc` objects that will be evaluated when creating a log entry.
|
@@ -85,13 +88,13 @@ logger.info("no requests") # Will not include the `request_id` tag
|
|
85
88
|
|
86
89
|
Tag keys are always converted to strings. Tags are inherited so that message tags take precedence over block tags which take precedence over global tags.
|
87
90
|
|
88
|
-
#### Compatibility with ActiveSupport::
|
91
|
+
#### Compatibility with ActiveSupport::TaggedLogging
|
89
92
|
|
90
|
-
`Lumberjack::Logger` version 1.1.2 or greater is compatible with `ActiveSupport::
|
93
|
+
`Lumberjack::Logger` version 1.1.2 or greater is compatible with `ActiveSupport::TaggedLogging`. This is so that other code that expect to have a logger that responds to the `tagged` method will work. Any tags added with the `tagged` method will be appended to an array in the the "tagged" tag.
|
91
94
|
|
92
95
|
```ruby
|
93
|
-
logger.tagged("foo", "bar=1", "
|
94
|
-
logger.info("here") # will include tags: {"tagged" => ["foo", "
|
96
|
+
logger.tagged("foo", "bar=1", "other") do
|
97
|
+
logger.info("here") # will include tags: {"tagged" => ["foo", "bar=1", "other"]}
|
95
98
|
end
|
96
99
|
```
|
97
100
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.2.
|
1
|
+
1.2.8
|
data/lib/lumberjack.rb
CHANGED
@@ -1,26 +1,26 @@
|
|
1
1
|
# frozen_string_literals: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require 'logger'
|
3
|
+
require "rbconfig"
|
4
|
+
require "time"
|
5
|
+
require "securerandom"
|
6
|
+
require "logger"
|
8
7
|
|
9
8
|
module Lumberjack
|
10
|
-
LINE_SEPARATOR = (RbConfig::CONFIG[
|
9
|
+
LINE_SEPARATOR = (RbConfig::CONFIG["host_os"] =~ /mswin/i ? "\r\n" : "\n")
|
11
10
|
|
12
|
-
require_relative "lumberjack/severity
|
13
|
-
require_relative "lumberjack/formatter
|
11
|
+
require_relative "lumberjack/severity"
|
12
|
+
require_relative "lumberjack/formatter"
|
14
13
|
|
15
|
-
require_relative "lumberjack/context
|
16
|
-
require_relative "lumberjack/log_entry
|
17
|
-
require_relative "lumberjack/device
|
18
|
-
require_relative "lumberjack/logger
|
19
|
-
require_relative "lumberjack/tags
|
20
|
-
require_relative "lumberjack/tag_formatter
|
21
|
-
require_relative "lumberjack/tagged_logger_support
|
22
|
-
require_relative "lumberjack/
|
23
|
-
require_relative "lumberjack/
|
14
|
+
require_relative "lumberjack/context"
|
15
|
+
require_relative "lumberjack/log_entry"
|
16
|
+
require_relative "lumberjack/device"
|
17
|
+
require_relative "lumberjack/logger"
|
18
|
+
require_relative "lumberjack/tags"
|
19
|
+
require_relative "lumberjack/tag_formatter"
|
20
|
+
require_relative "lumberjack/tagged_logger_support"
|
21
|
+
require_relative "lumberjack/tagged_logging"
|
22
|
+
require_relative "lumberjack/template"
|
23
|
+
require_relative "lumberjack/rack"
|
24
24
|
|
25
25
|
class << self
|
26
26
|
# Define a unit of work within a block. Within the block supplied to this
|
@@ -53,31 +53,41 @@ module Lumberjack
|
|
53
53
|
#
|
54
54
|
# Otherwise, it will return the current context. If one doesn't exist, it will return a new one
|
55
55
|
# but that context will not be in any scope.
|
56
|
-
def context
|
56
|
+
def context(&block)
|
57
57
|
current_context = Thread.current[:lumberjack_context]
|
58
|
-
if
|
59
|
-
|
60
|
-
begin
|
61
|
-
yield
|
62
|
-
ensure
|
63
|
-
Thread.current[:lumberjack_context] = current_context
|
64
|
-
end
|
58
|
+
if block
|
59
|
+
use_context(Context.new(current_context), &block)
|
65
60
|
else
|
66
61
|
current_context || Context.new
|
67
62
|
end
|
68
63
|
end
|
69
64
|
|
65
|
+
# Set the context to use within a block.
|
66
|
+
def use_context(context, &block)
|
67
|
+
current_context = Thread.current[:lumberjack_context]
|
68
|
+
begin
|
69
|
+
Thread.current[:lumberjack_context] = (context || Context.new)
|
70
|
+
yield
|
71
|
+
ensure
|
72
|
+
Thread.current[:lumberjack_context] = current_context
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Return true if inside a context block.
|
77
|
+
def context?
|
78
|
+
!!Thread.current[:lumberjack_context]
|
79
|
+
end
|
80
|
+
|
70
81
|
# Return the tags from the current context or nil if there are no tags.
|
71
82
|
def context_tags
|
72
83
|
context = Thread.current[:lumberjack_context]
|
73
|
-
context
|
84
|
+
context&.tags
|
74
85
|
end
|
75
86
|
|
76
87
|
# Set tags on the current context
|
77
88
|
def tag(tags)
|
78
89
|
context = Thread.current[:lumberjack_context]
|
79
|
-
context
|
90
|
+
context&.tag(tags)
|
80
91
|
end
|
81
|
-
|
82
92
|
end
|
83
93
|
end
|
data/lib/lumberjack/device.rb
CHANGED
@@ -4,37 +4,37 @@ module Lumberjack
|
|
4
4
|
# This is an abstract class for logging devices. Subclasses must implement the +write+ method and
|
5
5
|
# may implement the +close+ and +flush+ methods if applicable.
|
6
6
|
class Device
|
7
|
-
require_relative "device/writer
|
8
|
-
require_relative "device/log_file
|
9
|
-
require_relative "device/rolling_log_file
|
10
|
-
require_relative "device/date_rolling_log_file
|
11
|
-
require_relative "device/size_rolling_log_file
|
12
|
-
require_relative "device/multi
|
13
|
-
require_relative "device/null
|
7
|
+
require_relative "device/writer"
|
8
|
+
require_relative "device/log_file"
|
9
|
+
require_relative "device/rolling_log_file"
|
10
|
+
require_relative "device/date_rolling_log_file"
|
11
|
+
require_relative "device/size_rolling_log_file"
|
12
|
+
require_relative "device/multi"
|
13
|
+
require_relative "device/null"
|
14
14
|
|
15
15
|
# Subclasses must implement this method to write a LogEntry.
|
16
16
|
def write(entry)
|
17
17
|
raise NotImplementedError
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
# Subclasses may implement this method to close the device.
|
21
21
|
def close
|
22
22
|
flush
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
# Subclasses may implement this method to reopen the device.
|
26
26
|
def reopen(logdev = nil)
|
27
27
|
flush
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
# Subclasses may implement this method to flush any buffers used by the device.
|
31
31
|
def flush
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
# Subclasses may implement this method to get the format for log timestamps.
|
35
35
|
def datetime_format
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
# Subclasses may implement this method to set a format for log timestamps.
|
39
39
|
def datetime_format=(format)
|
40
40
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literals: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "date"
|
4
4
|
|
5
5
|
module Lumberjack
|
6
6
|
class Device
|
@@ -16,7 +16,7 @@ module Lumberjack
|
|
16
16
|
def initialize(path, options = {})
|
17
17
|
@manual = options[:manual]
|
18
18
|
@file_date = Date.today
|
19
|
-
if options[:roll]
|
19
|
+
if options[:roll]&.to_s&.match(/(daily)|(weekly)|(monthly)/i)
|
20
20
|
@roll_period = $~[0].downcase.to_sym
|
21
21
|
options.delete(:roll)
|
22
22
|
else
|
@@ -28,11 +28,11 @@ module Lumberjack
|
|
28
28
|
def archive_file_suffix
|
29
29
|
case @roll_period
|
30
30
|
when :weekly
|
31
|
-
|
31
|
+
@file_date.strftime("week-of-%Y-%m-%d").to_s
|
32
32
|
when :monthly
|
33
|
-
|
33
|
+
@file_date.strftime("%Y-%m").to_s
|
34
34
|
else
|
35
|
-
|
35
|
+
@file_date.strftime("%Y-%m-%d").to_s
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
@@ -54,9 +54,9 @@ module Lumberjack
|
|
54
54
|
end
|
55
55
|
end
|
56
56
|
end
|
57
|
-
|
57
|
+
|
58
58
|
protected
|
59
|
-
|
59
|
+
|
60
60
|
def after_roll
|
61
61
|
@file_date = Date.today
|
62
62
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literals: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "fileutils"
|
4
4
|
|
5
5
|
module Lumberjack
|
6
6
|
class Device
|
@@ -10,23 +10,23 @@ module Lumberjack
|
|
10
10
|
|
11
11
|
# The absolute path of the file being logged to.
|
12
12
|
attr_reader :path
|
13
|
-
|
13
|
+
|
14
14
|
# Create a logger to the file at +path+. Options are passed through to the Writer constructor.
|
15
15
|
def initialize(path, options = {})
|
16
16
|
@path = File.expand_path(path)
|
17
17
|
FileUtils.mkdir_p(File.dirname(@path))
|
18
18
|
super(file_stream, options)
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
def reopen(logdev = nil)
|
22
22
|
close
|
23
23
|
@stream = file_stream
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
private
|
27
|
-
|
27
|
+
|
28
28
|
def file_stream
|
29
|
-
File.new(@path,
|
29
|
+
File.new(@path, "a", encoding: EXTERNAL_ENCODING)
|
30
30
|
end
|
31
31
|
end
|
32
32
|
end
|
@@ -7,35 +7,35 @@ module Lumberjack
|
|
7
7
|
def initialize(*devices)
|
8
8
|
@devices = devices.flatten
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
def write(entry)
|
12
12
|
@devices.each do |device|
|
13
13
|
device.write(entry)
|
14
14
|
end
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
def flush
|
18
18
|
@devices.each do |device|
|
19
19
|
device.flush
|
20
20
|
end
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
def close
|
24
24
|
@devices.each do |device|
|
25
25
|
device.close
|
26
26
|
end
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
def reopen(logdev = nil)
|
30
30
|
@devices.each do |device|
|
31
31
|
device.reopen(logdev = nil)
|
32
32
|
end
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
def datetime_format
|
36
36
|
@devices.detect(&:datetime_format).datetime_format
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
def datetime_format=(format)
|
40
40
|
@devices.each do |device|
|
41
41
|
device.datetime_format = format
|
@@ -14,35 +14,43 @@ module Lumberjack
|
|
14
14
|
class RollingLogFile < LogFile
|
15
15
|
attr_reader :path
|
16
16
|
attr_accessor :keep
|
17
|
-
|
17
|
+
|
18
18
|
def initialize(path, options = {})
|
19
19
|
@path = File.expand_path(path)
|
20
20
|
@keep = options[:keep]
|
21
21
|
super(path, options)
|
22
|
-
@file_inode =
|
22
|
+
@file_inode = begin
|
23
|
+
stream.lstat.ino
|
24
|
+
rescue
|
25
|
+
nil
|
26
|
+
end
|
23
27
|
@@rolls = []
|
24
28
|
@next_stat_check = Time.now.to_f
|
25
29
|
@min_roll_check = (options[:min_roll_check] || 1.0).to_f
|
26
30
|
end
|
27
|
-
|
31
|
+
|
28
32
|
# Returns a suffix that will be appended to the file name when it is archived.. The suffix should
|
29
33
|
# change after it is time to roll the file. The log file will be renamed when it is rolled.
|
30
34
|
def archive_file_suffix
|
31
35
|
raise NotImplementedError
|
32
36
|
end
|
33
|
-
|
37
|
+
|
34
38
|
# Return +true+ if the file should be rolled.
|
35
39
|
def roll_file?
|
36
40
|
raise NotImplementedError
|
37
41
|
end
|
38
|
-
|
42
|
+
|
39
43
|
# Roll the log file by renaming it to the archive file name and then re-opening a stream to the log
|
40
44
|
# file path. Rolling a file is safe in multi-threaded or multi-process environments.
|
41
45
|
def roll_file! #:nodoc:
|
42
46
|
do_once(stream) do
|
43
47
|
archive_file = "#{path}.#{archive_file_suffix}"
|
44
48
|
stream.flush
|
45
|
-
current_inode =
|
49
|
+
current_inode = begin
|
50
|
+
File.stat(path).ino
|
51
|
+
rescue
|
52
|
+
nil
|
53
|
+
end
|
46
54
|
if @file_inode && current_inode == @file_inode && !File.exist?(archive_file) && File.exist?(path)
|
47
55
|
begin
|
48
56
|
File.rename(path, archive_file)
|
@@ -59,41 +67,49 @@ module Lumberjack
|
|
59
67
|
end
|
60
68
|
|
61
69
|
protected
|
62
|
-
|
70
|
+
|
63
71
|
# This method will be called after a file has been rolled. Subclasses can
|
64
72
|
# implement code to reset the state of the device. This method is thread safe.
|
65
73
|
def after_roll
|
66
74
|
end
|
67
|
-
|
75
|
+
|
68
76
|
# Handle rolling the file before flushing.
|
69
77
|
def before_flush # :nodoc:
|
70
78
|
if @min_roll_check <= 0.0 || Time.now.to_f >= @next_stat_check
|
71
79
|
@next_stat_check += @min_roll_check
|
72
|
-
path_inode =
|
80
|
+
path_inode = begin
|
81
|
+
File.lstat(path).ino
|
82
|
+
rescue
|
83
|
+
nil
|
84
|
+
end
|
73
85
|
if path_inode != @file_inode
|
74
86
|
@file_inode = path_inode
|
75
87
|
reopen_file
|
76
|
-
|
77
|
-
roll_file!
|
88
|
+
elsif roll_file?
|
89
|
+
roll_file!
|
78
90
|
end
|
79
91
|
end
|
80
92
|
end
|
81
|
-
|
93
|
+
|
82
94
|
private
|
83
95
|
|
84
96
|
def reopen_file
|
85
97
|
old_stream = stream
|
86
|
-
new_stream = File.open(path,
|
98
|
+
new_stream = File.open(path, "a", encoding: EXTERNAL_ENCODING)
|
87
99
|
new_stream.sync = true if buffer_size > 0
|
88
|
-
@file_inode =
|
100
|
+
@file_inode = begin
|
101
|
+
new_stream.lstat.ino
|
102
|
+
rescue
|
103
|
+
nil
|
104
|
+
end
|
89
105
|
self.stream = new_stream
|
90
106
|
old_stream.close
|
91
107
|
end
|
92
108
|
end
|
93
|
-
|
109
|
+
|
94
110
|
def cleanup_files!
|
95
111
|
if keep
|
96
|
-
files = Dir.glob("#{path}.*").collect{|f| [f, File.ctime(f)]}.sort{|a,b| b.last <=> a.last}.collect{|a| a.first}
|
112
|
+
files = Dir.glob("#{path}.*").collect { |f| [f, File.ctime(f)] }.sort { |a, b| b.last <=> a.last }.collect { |a| a.first }
|
97
113
|
if files.size > keep
|
98
114
|
files[keep, files.length].each do |f|
|
99
115
|
File.delete(f)
|
@@ -101,7 +117,7 @@ module Lumberjack
|
|
101
117
|
end
|
102
118
|
end
|
103
119
|
end
|
104
|
-
|
120
|
+
|
105
121
|
def do_once(file)
|
106
122
|
begin
|
107
123
|
file.flock(File::LOCK_EX)
|
@@ -110,11 +126,19 @@ module Lumberjack
|
|
110
126
|
return
|
111
127
|
end
|
112
128
|
begin
|
113
|
-
verify =
|
129
|
+
verify = begin
|
130
|
+
file.lstat
|
131
|
+
rescue
|
132
|
+
nil
|
133
|
+
end
|
114
134
|
# Execute only if the file we locked is still the same one that needed to be rolled
|
115
135
|
yield if verify && verify.ino == @file_inode && verify.size > 0
|
116
136
|
ensure
|
117
|
-
|
137
|
+
begin
|
138
|
+
file.flock(File::LOCK_UN)
|
139
|
+
rescue
|
140
|
+
nil
|
141
|
+
end
|
118
142
|
end
|
119
143
|
end
|
120
144
|
end
|