在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:trusche/httplog开源软件地址:https://github.com/trusche/httplog开源编程语言:Ruby 99.6%开源软件介绍:httplogLog outgoing HTTP requests made from your application. Helps with debugging pesky API error responses, or just generally understanding what's going on under the hood. Requires ruby >= 2.6 This gem works with the following ruby modules and libraries: These libraries are at least partially supported, where they use one of the above as adapters, but not explicitly tested - YMMV: In theory, it should also work with any library built on top of these. But the difference between theory and practice is bigger in practice than in theory. This is very much a development and debugging tool; it is not recommended to use this in a production environment as it is monkey-patching the respective HTTP implementations. You have been warned - use at your own risk. Installation
Usage
By default, this will log all outgoing HTTP requests and their responses to $stdout on DEBUG level. Notes on content types
ConfigurationYou can override the following default options: HttpLog.configure do |config|
# Enable or disable all logging
config.enabled = true
# You can assign a different logger or method to call on that logger
config.logger = Logger.new($stdout)
config.logger_method = :log
# I really wouldn't change this...
config.severity = Logger::Severity::DEBUG
# Tweak which parts of the HTTP cycle to log...
config.log_connect = true
config.log_request = true
config.log_headers = false
config.log_data = true
config.log_status = true
config.log_response = true
config.log_benchmark = true
# ...or log all request as a single line by setting this to `true`
config.compact_log = false
# You can also log in JSON format
config.json_log = false
# Prettify the output - see below
config.color = false
# Limit logging based on URL patterns
config.url_whitelist_pattern = nil
config.url_blacklist_pattern = nil
# Mask sensitive information in request and response JSON data.
# Enable global JSON masking by setting the parameter to `/.*/`
config.url_masked_body_pattern = nil
# You can specify any custom JSON serializer that implements `load` and `dump` class methods
# to parse JSON responses
config.json_parser = JSON
# When using graylog, you can supply a formatter here - see below for details
config.graylog_formatter = nil
# Mask the values of sensitive request parameters
config.filter_parameters = %w[password]
# Customize the prefix with a proc or lambda
config.prefix = ->{ "[httplog] #{Time.now} " }
end If you want to use this in a Rails app, I'd suggest configuring this specifically for each environment. A global initializer is not a good idea since # config/environments/development.rb
HttpLog.configure do |config|
config.logger = Rails.logger
end If you're running a (hopefully patched) legacy Rails 3 app, you may need to set
You can colorize the output to make it stand out in your logfile, either with a single color for the text: HttpLog.configure do |config|
config.color = :red
end Or with a color hash for text and background: HttpLog.configure do |config|
config.color = {color: :black, background: :yellow}
end For more color options please refer to the rainbow documentation Graylog loggingIf you use Graylog and want to use its search features such as "benchmark:>1 AND method:PUT", you can use this configuration: FORMATTER = Lograge::Formatters::KeyValue.new
HttpLog.configure do |config|
config.logger = <your GELF::Logger>
config.logger_method = :add
config.severity = GELF::Levels::DEBUG
config.graylog_formatter = FORMATTER
end You also can use GELF Graylog format this way: class Lograge::Formatters::Graylog2HttpLog < Lograge::Formatters::Graylog2
def short_message data
data[:response_body] = data[:response_body].to_s.byteslice(0, 32_766) unless data[:response_body].blank?
"[httplog] [#{data[:response_code]}] #{data[:method]} #{data[:url]}"
end
end
FORMATTER = Lograge::Formatters::Graylog2HttpLog.new Or define your own class that implements the Compact loggingIf the log is too noisy for you, but you don't want to completely disable it either, set the JSON loggingIf you want to log HTTP requests in a JSON format, set the Parameter filteringJust like in Rails, you can filter the values of sensitive parameters by setting the Please note that this will only filter the request data with well-formed parameters (in the URL, the headers, and the request data) but not the response. It does not currently filter JSON request data either, just standard "key=value" pairs in the request body. ExampleWith the default configuration, the log output might look like this:
With
With
With
And with
Known IssuesFollowing are some known quirks and issues with particular libraries. If you know a workaround or have a suggestion for a fix, please open an issue or, even better, submit a pull request!
Running the specsMake sure you have the necessary dependencies installed by running ContributingIf you have any issues with or feature requests for httplog, please open an issue on GitHub or fork the project and send a pull request. Please include passing specs with all pull requests. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论