redis_stream_logger 0.1.0 → 0.2.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
  SHA256:
3
- metadata.gz: ea9895c1c5c8f4b5694ef368b53ff70d1472017f859a24bfe7680067eb5b2964
4
- data.tar.gz: 67fc064a242db94edd55baf60df633b852a1cc7ba7377626eb5cafc45c515830
3
+ metadata.gz: 39514125c61948167aa8d2f5c6720daaff2e5a2014a982db9e28db08cfa17ced
4
+ data.tar.gz: 0d08dbcf9a91e83401cd2549a5ba0b4a08147e445e3b1df1dd90043aaf21f587
5
5
  SHA512:
6
- metadata.gz: 9af9e64b0008d920b21d2fa7f34a1fc85179bf959010f4bc01dc2fab221a19fbbebfc5af9827eefcaa61fe52f5328a7b2a79f61a2f91bf5c15450de0c877775f
7
- data.tar.gz: 636e1dc374ca3877513da738bbde9153ce3f38f7b35ff0fda9c87c72e8f7523b51b152e9a8318d2ac3f0715b25e90abaf7662d581798722e506e403382a33554
6
+ metadata.gz: 70de80ef89b06c7bd677ac15b96b9404fc312d49e98eb52d72738225119c7de0e3e276c7ad400fd13438a18ae3c71e5e8c629e8fe74fa917caefb23884e63bf4
7
+ data.tar.gz: 176fa6ae2845a05af6b2dd217777c3aa242b510f174589d860e8a6b1d2d9cb081d43638c44efa1adc201811dc263b677d7a2335506d6a859c8631bd7a083656e
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- redis_stream_logger (0.1.0)
4
+ redis_stream_logger (0.2.0)
5
5
  redis (~> 4.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -5,6 +5,9 @@ This gem creates a [log device](https://github.com/ruby/logger/blob/bf6d5aa37ee9
5
5
  The log device will buffer requests internally until either a time interval or buffer size is hit and then it will send all the log entries as a pipeline to minimize network
6
6
  traffic.
7
7
 
8
+ There is a basic viewer for this logger (or any Redis stream if you're feeling brave) [here](https://github.com/mlh758/stream_log_viewer). It supports tailing
9
+ a stream or searching logs with basic text matching.
10
+
8
11
  ## Installation
9
12
 
10
13
  Add this line to your application's Gemfile:
@@ -15,17 +18,41 @@ gem 'redis_stream_logger'
15
18
 
16
19
  ## Usage
17
20
 
18
- To use in a Rails application add something like this to your config:
21
+ To use in an application add something like this:
19
22
 
20
23
  ```rb
21
24
  redis_log = RedisStreamLogger::LogDevice.new do |config|
22
25
  config.connection = Redis.new
23
26
  end
24
- config.logger = Logger.new(redis_log)
27
+ logger = Logger.new(redis_log)
25
28
  ```
26
29
 
27
30
  It is _highly_ recommended that you set timeouts on your Redis connection. See the [Redis docs](https://github.com/redis/redis-rb/#timeouts).
28
31
 
32
+ If you are using a forking Rails server like Passenger this gets a lot weirder because currently the logger uses threads
33
+ to handle IO and avoid blocking the main app. For Rails and Passenger it will look something like this:
34
+
35
+ ```rb
36
+ # config.ru
37
+ if defined?(PhusionPassenger)
38
+ PhusionPassenger.on_event(:starting_worker_process) do |forked|
39
+ next unless forked
40
+ Rails.logger.close
41
+ redis_log = RedisStreamLogger::LogDevice.new do |config|
42
+ config.connection = Redis.new
43
+ config.stream_name = 'app-logs'
44
+ end
45
+ logger = Logger.new(redis_log)
46
+ logger.level = Rails.application.config.log_level
47
+ Rails.logger = logger
48
+ Rails.application.config.logger = logger
49
+ Rails.application.config.action_controller.logger = logger
50
+ end
51
+ end
52
+ ```
53
+
54
+ I'm currently looking for a way to use one of the async libraries to improve things.
55
+
29
56
  ### Configuration
30
57
 
31
58
  * buffer_size: Max number of items to hold in queue before attempting to write a batch. Defaults to 100
@@ -39,7 +66,7 @@ It is _highly_ recommended that you set timeouts on your Redis connection. See t
39
66
 
40
67
  ## Path to 1.0
41
68
 
42
- 1. Custom formatter: Right now it just takes the string from the logger and writes it to Redis. Ideally, the timestamp, log level, and tags would be written as separate keys.
69
+ The use of threads caused unexpected headaches in applications that rely on forking like Passenger. I'm hoping this library can drop the reliance on threads.
43
70
 
44
71
  ## Development
45
72
 
@@ -1,5 +1,6 @@
1
1
  require "redis_stream_logger/version"
2
2
  require "redis_stream_logger/log_device"
3
+ require 'redis_stream_logger/railtie' if defined?(Rails::Railtie)
3
4
 
4
5
  module RedisStreamLogger
5
6
  class Error < StandardError; end
@@ -12,20 +12,15 @@ module RedisStreamLogger
12
12
  def initialize(conn = nil, stream: 'rails-log')
13
13
  @config = Config.new
14
14
  @closed = false
15
- yield @config if block_given?
15
+ # Just in case a whole new config is passed in like in the Railtie
16
+ new_conf = yield @config if block_given?
17
+ @config = new_conf if new_conf.is_a?(Config)
16
18
  @config.connection ||= conn
17
19
  @config.stream_name ||= stream
18
20
  raise ArgumentError, 'must provide connection' if @config.connection.nil?
19
21
 
20
22
  @q = Queue.new
21
- @error_logger = ::Logger.new(STDERR)
22
- @ticker = Thread.new do
23
- ticker(@config.send_interval)
24
- end
25
- @writer = Thread.new do
26
- writer(@config.buffer_size, @config.send_interval)
27
- end
28
- at_exit { close }
23
+ start
29
24
  end
30
25
 
31
26
  def write(msg)
@@ -33,7 +28,9 @@ module RedisStreamLogger
33
28
  end
34
29
 
35
30
  def reopen(log = nil)
36
- # no op
31
+ close
32
+ @config.connection._client.connect
33
+ start
37
34
  end
38
35
 
39
36
  def close
@@ -47,6 +44,18 @@ module RedisStreamLogger
47
44
 
48
45
  private
49
46
 
47
+ def start
48
+ @closed = false
49
+ @error_logger = ::Logger.new(STDERR)
50
+ @ticker = Thread.new do
51
+ ticker(@config.send_interval)
52
+ end
53
+ @writer = Thread.new do
54
+ writer(@config.buffer_size, @config.send_interval)
55
+ end
56
+ at_exit { close }
57
+ end
58
+
50
59
  def send_options
51
60
  return {} if @config.max_len.nil?
52
61
 
@@ -0,0 +1,26 @@
1
+ require 'rails/railtie'
2
+
3
+ module RedisStreamLogger
4
+ # I based this heavily on the LogstashLogger implementation but we'll only
5
+ # accept our own config class here
6
+ def self.setup(app)
7
+ return unless app.config.redis_stream_logger.present?
8
+ conf = app.config.redis_stream_logger
9
+ raise ArgumentError, 'unexpected config class' unless conf.is_a?(Config)
10
+
11
+ logdev = RedisStreamLogger::LogDevice.new do |_c|
12
+ conf
13
+ end
14
+
15
+ logger = Logger.new(logdev)
16
+ logger.level = app.config.log_level
17
+ app.config.logger = logger
18
+ end
19
+ end
20
+
21
+ class Railtie < ::Rails::Railtie
22
+ config.redis_stream_logger = nil
23
+ initializer :redis_stream_logger, before: :initialize_logger do |app|
24
+ RedisStreamLogger.setup(app)
25
+ end
26
+ end
@@ -1,3 +1,3 @@
1
1
  module RedisStreamLogger
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_stream_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Harris
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-02-04 00:00:00.000000000 Z
11
+ date: 2021-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -44,6 +44,7 @@ files:
44
44
  - lib/redis_stream_logger.rb
45
45
  - lib/redis_stream_logger/config.rb
46
46
  - lib/redis_stream_logger/log_device.rb
47
+ - lib/redis_stream_logger/railtie.rb
47
48
  - lib/redis_stream_logger/version.rb
48
49
  - redis_stream_logger.gemspec
49
50
  homepage: https://github.com/mlh758/redis_stream_logger