You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Answering my own question, I guess more related to pry-rails but
class LoggingReadline
delegate :completion_proc, :completion_proc=, to: Readline
def readline(prompt)
Readline.readline(prompt).tap do |user_input|
logger.info(user_input)
end
end
private
def logger
@logger ||= Logger.new('log/console.log')
end
end
Pry.config.input = LoggingReadline.new
class PryLogger
class LoggingReadline
attr_accessor :logger
delegate :completion_proc, :completion_proc=, to: Readline
def initialize(logger:)
self.logger = logger
end
def readline(prompt)
Readline.readline(prompt).tap do |user_input|
self.logger.info("console input: #{user_input}")
end
end
end
class LoggedIO
attr_accessor :io, :logger
def initialize(io, logger:)
self.io = io
self.logger = logger
end
def method_missing(method_name, *args)
if method_name == :print
self.logger.info("console output: #{args.first}")
end
io.send(method_name, *args)
end
end
def self.configure(pry_config, input_logger: nil, output_logger: nil)
pry_config.input = LoggingReadline.new(logger: input_logger) if input_logger
pry_config.output = LoggedIO.new($stdout, logger: output_logger) if output_logger
end
end
usage:
# config/initializers/pry.rb
Pry.configure do |config|
PryLogger.configure(config, input_logger: Rails.logger, output_logger: Rails.logger)
end
Not an issue but a question/request?
Is it possible to hook into pry to log any command sent and the (string) result of a command?
Thank you
The text was updated successfully, but these errors were encountered: