Skip to content

Commit 736512e

Browse files
RT 4753 - Fix bug when the client app is using STDOUT the console logs is duplicated (#37)
Co-authored-by: Arman Ortega <aortega@stackify.com>
1 parent 987caf0 commit 736512e

File tree

4 files changed

+83
-4
lines changed

4 files changed

+83
-4
lines changed

lib/stackify/engine.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,32 @@ class Engine < ::Rails::Engine
44

55
if Rails.version > '3.1'
66
initializer 'Stackify set up of logger', group: :all do
7+
if Gem::Version.new(Rails::VERSION::STRING) >= Gem::Version.new('4.0')
8+
# check if the client app is using the ActiveSupport::Logger
9+
is_activesupport_logger = ::Rails.logger.is_a?(ActiveSupport::Logger)
10+
elsif Gem::Version.new(Rails::VERSION::STRING) >= Gem::Version.new('3.0')
11+
Stackify::Utils.check_buffered_logger
12+
end
13+
14+
# Check if the log output is STDOUT
15+
Stackify::Utils.check_log_output
16+
17+
# Proxy the client Rails logger and write logs to its default log_path.
18+
# At the same time, we send the log messages to the LoggerClient.
719
::Rails.logger = ::Stackify::LoggerProxy.new ::Rails.logger
20+
21+
if Gem::Version.new(Rails::VERSION::STRING) >= Gem::Version.new('6.0')
22+
set_console_logs ::Rails.logger
23+
elsif Gem::Version.new(Rails::VERSION::STRING) >= Gem::Version.new('4.0')
24+
if is_activesupport_logger && Stackify.configuration.stdout_output
25+
set_console_logs ::Rails.logger
26+
end
27+
# Another checking if the client app is using the default logger and not STDOUT
28+
if Stackify.configuration.stdout_output == false
29+
set_console_logs ::Rails.logger
30+
end
31+
end
32+
833
Stackify.run
934
end
1035

@@ -14,6 +39,16 @@ class Engine < ::Rails::Engine
1439
end
1540
end
1641

42+
def set_console_logs logger
43+
# Handle the stdout logs from Action Controller
44+
ActionController::Base.logger = logger
45+
46+
# Handle the stdout logs from Action View
47+
ActionView::Base.logger = logger
48+
49+
# Handle the stdout logs from Active Record
50+
ActiveRecord::Base.logger = logger
51+
end
1752
end
1853

1954
end

lib/stackify/logger_client.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,26 @@ def initialize
1919
# @param msg [Integer] log messages
2020
# @param call_trace [Object] return the current execution stack
2121
def log num_level, level, msg, call_trace
22-
if num_level <= Logger.const_get(level.upcase).to_i
23-
puts msg
22+
display_log = true
23+
log_appender = false
24+
buffer_log = false
25+
if defined? Rails
26+
display_log = false if Stackify.configuration.stdout_output
27+
log_appender = true if defined?(Logging)
28+
buffer_log = true if Stackify.configuration.buffered_logger
29+
unless buffer_log
30+
if Gem::Version.new(Rails::VERSION::STRING) >= Gem::Version.new('4.0')
31+
if display_log && log_appender
32+
puts msg if num_level <= Logger.const_get(level.upcase).to_i
33+
elsif display_log && log_appender == false
34+
puts msg if num_level <= Logger.const_get(level.upcase).to_i
35+
end
36+
end
37+
end
38+
else
39+
puts msg if num_level <= Logger.const_get(level.upcase).to_i
2440
end
41+
2542
return if @@transport.nil?
2643
task = log_message_task level, msg, call_trace
2744
@@transport.log level, msg, call_trace, task

lib/stackify/utils/configuration.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Stackify
33
class Configuration
44

55
attr_accessor :api_key, :app_name, :app_location, :env, :log_level, :logger,
6-
:proxy, :mode, :base_api_url, :api_enabled, :transport, :errors, :http_endpoint
6+
:proxy, :mode, :base_api_url, :api_enabled, :transport, :errors, :http_endpoint, :stdout_output, :buffered_logger
77

88
attr_reader :send_interval, :flood_limit, :queue_max_size, :agent_log_url, :unix_socket_path, :http_endpoint
99

@@ -25,6 +25,8 @@ def initialize
2525
@agent_log_url = '/log'
2626
@unix_socket_path = '/usr/local/stackify/stackify.sock'
2727
@http_endpoint = get_env 'STACKIFY_TRANSPORT_HTTP_ENDPOINT', 'https://localhost:10601'
28+
@stdout_output = false
29+
@buffered_logger = false
2830
end
2931

3032
def get_env env_key, default

lib/stackify/utils/methods.rb

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,29 @@ def self.get_app_settings
4646
'app_location' => Stackify.configuration.app_location || Dir.pwd
4747
}
4848
end
49-
end
49+
50+
# Check if the app is running on rails and the logger output is using STDOUT
51+
def self.check_log_output
52+
if defined? Rails
53+
if Gem::Version.new(Rails::VERSION::STRING) >= Gem::Version.new('5.0')
54+
Stackify.configuration.stdout_output = ActiveSupport::Logger.logger_outputs_to?(Rails.logger, STDOUT)
55+
else
56+
Stackify.configuration.stdout_output = self.logger_stdout
57+
end
58+
end
59+
end
60+
61+
def self.logger_stdout
62+
logdev = ::Rails.logger.instance_variable_get(:@logdev)
63+
logger_source = logdev.dev if logdev.respond_to?(:dev)
64+
sources = [$stdout]
65+
found = sources.any? { |source| source == logger_source }
66+
end
67+
68+
# Check if the rails version 3 and it's using the buffered logger
69+
def self.check_buffered_logger
70+
is_buffered_logger = false
71+
is_buffered_logger = true if ::Rails.logger.is_a?(ActiveSupport::BufferedLogger)
72+
Stackify.configuration.buffered_logger = is_buffered_logger
73+
end
74+
end

0 commit comments

Comments
 (0)