apache_log_tail 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/apache_log_tail.rb +41 -21
  2. metadata +6 -5
@@ -1,40 +1,43 @@
1
1
 
2
2
  # Facilitates reading the most recent additions to a log file.
3
3
  #
4
- # Note that rotation of log files is *not* handled.
5
- #
6
4
  # Example:
7
- # >> tail = LogTail.new "/var/log/apache2/access.log"
8
- # >> tail.state_store.path_to_file = "/tmp/my-state.txt" # Optional: there is a default path
9
- # >> tail.each_new_line {|line| puts line }
5
+ # tail = LogTail.new "/var/log/apache2/access.log"
6
+ # tail.state_store.path_to_file = "/tmp/my-state.txt" # Optional: there is a default path
7
+ # tail.each_new_line {|line| puts line }
8
+ #
9
+ # ## A custom StateStore
10
+ #
11
+ # tail.state_store = MyStateStore.new
12
+ #
13
+ # A StateStore object must provide these methods:
10
14
  #
15
+ # - remember( state:Hash)
16
+ # - recall(): Hash
11
17
  #
12
18
  class LogTail
13
19
 
20
+ # @param [String] path_to_file the path to the file to process
21
+ #
14
22
  def initialize path_to_file
15
23
  @path_to_file = path_to_file
16
24
  end
17
25
 
18
-
19
- # Provides a StateStore object that provides persistent storage of a Hash.
26
+ # The StateStore provides persistent storage of a Hash.
20
27
  #
21
28
  def state_store
22
29
  @state_store ||= FileStateStore.new
23
30
  end
31
+ attr_writer :state_store
24
32
 
25
- # Accepts a StateStore object that provides persistent storage of a Hash.
26
- #
27
- # Example:
28
- # >> tail.state_store = MyStateStore.new
33
+ # Goes through each line in the file that has not yet been processed and
34
+ # passes it to the block given.
29
35
  #
30
- # A StateStore object must provide these methods:
36
+ # @param [String] path_to_file the path to the file that should be
37
+ # processed. This parameter is only intended
38
+ # for internal use ( processing rotated log
39
+ # files) and should be omitted for normal use
31
40
  #
32
- # - remember( state:Hash)
33
- # - recall(): Hash
34
- #
35
- attr_writer :state_store
36
-
37
-
38
41
  def each_new_line path_to_file = @path_to_file
39
42
 
40
43
  # Recall the cursor ( the location in the log file where we left off
@@ -55,13 +58,15 @@ class LogTail
55
58
  end
56
59
  end
57
60
 
58
-
59
61
  # This is the default implementation of StateStore, which stores the state in
60
62
  # a file ( by default in /tmp with a static name although this is
61
63
  # configurable).
62
64
  #
63
65
  class FileStateStore
64
66
 
67
+ # Provides the path to the file that is used to store the state ( unless a
68
+ # custom StateStore is used).
69
+ #
65
70
  def path_to_file
66
71
  @path_to_file ||= "/tmp/.apache_log_tail-state.yml"
67
72
  end
@@ -69,6 +74,9 @@ class LogTail
69
74
 
70
75
  require "yaml"
71
76
 
77
+ # Retrieves the state from the store.
78
+ # @return [Hash]
79
+ #
72
80
  def recall
73
81
  if not File.exists? path_to_file
74
82
  {}
@@ -77,6 +85,9 @@ class LogTail
77
85
  end
78
86
  end
79
87
 
88
+ # Stores the supplied state.
89
+ # @param [Hash] state
90
+ #
80
91
  def remember state
81
92
  File.open path_to_file, "w" do |file|
82
93
  file.write state.to_yaml
@@ -88,12 +99,21 @@ class LogTail
88
99
  end
89
100
 
90
101
 
102
+ # (see LogTail)
103
+ #
104
+ # Will not miss any lines when the log file is rotated between two invocations
105
+ # of #each_new_line ( as long as the rotated file has the same name as the
106
+ # original except for a `.1` suffix and as long as the file hasn't been rotated
107
+ # twice between invocations of #each_new_line).
108
+ #
91
109
  # Note that this class does no parse Apache log entries, only knows how Apache
92
- # log files are rotated on Debian. I have enjoyed using the apachelogregex gem
93
- # for parsing.
110
+ # log files are rotated on Debian. I have enjoyed using the `apachelogregex`
111
+ # gem for parsing.
94
112
  #
95
113
  class ApacheLogTail < LogTail
96
114
 
115
+ # (see LogTail#each_new_line)
116
+ #
97
117
  # Note: This method must be invoked more frequently than the log file
98
118
  # rotation period ( typically 1 week) otherwise an entire file will be
99
119
  # missed.
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apache_log_tail
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Neil Stockbridge
@@ -29,8 +29,8 @@ extra_rdoc_files: []
29
29
  files:
30
30
  - lib/apache_log_tail.rb
31
31
  homepage: http://rubygems.org/gems/apache_log_tail
32
- licenses: []
33
-
32
+ licenses:
33
+ - MIT
34
34
  post_install_message:
35
35
  rdoc_options: []
36
36
 
@@ -63,3 +63,4 @@ specification_version: 3
63
63
  summary: Process only the new lines from an Apache log file
64
64
  test_files: []
65
65
 
66
+ has_rdoc: