<commit_before># frozen_string_literal: true
# Copyright 2016 <NAME>
#
# This software may be modified and distributed under the terms
# of the MIT license. See the LICENSE.txt file for details.

require 'rack'

require 'rackstash/helpers/time'

module Rackstash
  module Rack
    # The Rack logging middleware provides a way to log structured data for each
    # request passing through to the Rack application.
    #
    # The middleware works as a combination of the `::Rack::Logger` and
    # especially the `::Rack::CommonLogger` middlewares shipped with Rack. For
    # each request, we open a new log environment by pushing a new {Buffer} to
    # the current Thread's stack. All log messages as well as any fields logged
    # to the {#logger} will be added to this buffer. After the request has
    # ended, we pop the buffer from the stack again and flush all logged data to
    # the configured log flows of the given {Rackstash::Logger} object.
    #
    # By default, we log the following fields about each request:
    #
    #   * `"method"` - the HTTP verb, e.g. `"GET"`, `"POST"`, or "`PUT`"
    #   * `"path"` - the request path, e.g. `"/desserts/cake?cream=1&cheese=0"`
    #   * `"status"` - the numeric HTTP response code, e.g. `200`, or `500`
    #   * `"duration"` - the duration in seconds with fractions between first
    #     receiving the request and its eventual delivery to the client by the
    #     application server, e.g. `0.295372`. This time equals the duration
    #     logged by the `::Rack::CommonLogger` middleware which ships with Rack.
    #
    # In addition to that, we can also log additional data from the request or
    # the response. For that, you can set additional (optionally lazy-evaluated)
    # fields and tags.
    #
    # In a Rack builder, you can define the middleware as follows
    #
    #     # First, create the logger however you like. You can use multiple
    #     # flows, add filters, ...
    #     logger = Rackstash::Logger.new(STDOUT)
    #
    #     use Rackstash::Rack::Middleware,
    #       logger,
    #       buffering: :full,
    #       request_fields: {
    #         'user_agent' => ->(request) { request.headers['user-agent'] },
    #         'remote_ip' => ->(request) { request.ip },
    #         'server' => Socket.gethostname
    #       },
    #       request_tags: ['rack', ->(request) { request.scheme }],
    #       response_fields: ->(headers) {
    #         { 'location' => headers['location'] }
    #       },
    #       response_tags: ->(headers) { ["cache_status_#{headers[x-rack-cache]}"] }
    #
    # Here, we instruct the middleware to log each request to the provided
    # `logger`. For each request, we will by default emit a single log event to
    # each flow of the logger. You can customize this by setting the `buffering`
    # argument. See {Buffer#buffering} for a description of the values. During
    # development, it might be useful to set this to `:data` to emit log events
    # directly for each logged message instead of only once after the request
    # was finished.
    #
    # In the log event(s) emitted for the request, we add the default fields
    # described above, plus some data extracted from the HTTP request data. For
    # dynamic values, we can use the `::Rack::Request` object for the current
    # request. In our example above, we add the user agent header of the
    # client's webbrowser and the client's IP address. We also set a `"server"`
    # field containing the server's hostname. We are also setting some tags, in
    # this case we set the static tag `"rack"` and a tag for the current
    # request's scheme. Usually, this is either `"http"` or `"https"`.
    #
    # For the response, we have the same flexibility. Here, we can use the hash
    # containing the response headers to set additional fields or tags to the
    # log buffer. In our example above, we add the value of the `Location`
    # header of the response as a field and add a tag based on the status of the
    # Rack::Cache middleware our app might be using.
    #
    # Note that all fields set by the middleware are deep-merged into the
    # Buffer's current fields using {Fields::Hash#deep_merge!}. All existing
    # fields set by the application will be preserved and are not overwritten.
    #
    # In case there is an unhandled exception during request processing in the
    # Rack aplication or any lower middleware, we log this error using
    # {Rackstash::Logger#add_exception}. The emitted event will still contain
    # all added request fields and tags as well as the `"status"` field (set to
    # `500`) and the duration of the request so far.
    class Middleware
      include Rackstash::Helpers::Time

      # @return [Rackstash::Logger] the Rackstash logger used to log the
      #   request details
      attr_reader :logger

      # @return [Hash<#to_s => Object>, Proc] a `Hash` specifying fields to be
      #   logged for each request. The fields will be added to the log {Buffer}
      #   before the request is passed to the Rack application. You can either
      #   give a literal `Hash` object or a `Proc` which returns such a `Hash`.
      #
      #   When including `Proc`s, they are evaluated with the `Rack::Request`
      #   object as a scope. See {Rackstash::Fields::Hash#deep_merge!}.
      attr_accessor :request_fields

      # @return [Array<#to_s, Proc>, Proc] Strings to add as tags to the logged
      #   event. The fields will be added to the log {Buffer} before the request
      #   is passed to the Rack application. You can either give (`Array`s of)<commit_msg>Fix spelling in description of Rack::Middleware<commit_after># frozen_string_literal: true
# Copyright 2016 <NAME>
#
# This software may be modified and distributed under the terms
# of the MIT license. See the LICENSE.txt file for details.

require 'rack'

require 'rackstash/helpers/time'

module Rackstash
  module Rack
    # The Rack logging middleware provides a way to log structured data for each
    # request passing through to the Rack application.
    #
    # The middleware works as a combination of the `::Rack::Logger` and
    # especially the `::Rack::CommonLogger` middlewares shipped with Rack. For
    # each request, we open a new log environment by pushing a new {Buffer} to
    # the current Thread's stack. All log messages as well as any fields logged
    # to the {#logger} will be added to this buffer. After the request has
    # ended, we pop the buffer from the stack again and flush all logged data to
    # the configured log flows of the given {Rackstash::Logger} object.
    #
    # By default, we log the following fields about each request:
    #
    #   * `"method"` - the HTTP verb, e.g. `"GET"`, `"POST"`, or "`PUT`"
    #   * `"path"` - the request path, e.g. `"/desserts/cake?cream=1&cheese=0"`
    #   * `"status"` - the numeric HTTP response code, e.g. `200`, or `500`
    #   * `"duration"` - the duration in seconds with fractions between first
    #     receiving the request and its eventual delivery to the client by the
    #     application server, e.g. `0.295372`. This time equals the duration
    #     logged by the `::Rack::CommonLogger` middleware which ships with Rack.
    #
    # In addition to that, we can also log additional data from the request or
    # the response. For that, you can set additional (optionally lazy-evaluated)
    # fields and tags.
    #
    # In a Rack builder, you can define the middleware as follows
    #
    #     # First, create the logger however you like. You can use multiple
    #     # flows, add filters, ...
    #     logger = Rackstash::Logger.new(STDOUT)
    #
    #     use Rackstash::Rack::Middleware,
    #       logger,
    #       buffering: :full,
    #       request_fields: {
    #         'user_agent' => ->(request) { request.headers['user-agent'] },
    #         'remote_ip' => ->(request) { request.ip },
    #         'server' => Socket.gethostname
    #       },
    #       request_tags: ['rack', ->(request) { request.scheme }],
    #       response_fields: ->(headers) {
    #         { 'location' => headers['location'] }
    #       },
    #       response_tags: ->(headers) { ["cache_status_#{headers[x-rack-cache]}"] }
    #
    # Here, we instruct the middleware to log each request to the provided
    # `logger`. For each request, we will by default emit a single log event to
    # each flow of the logger. You can customize this by setting the `buffering`
    # argument. See {Buffer#buffering} for a description of the values. During
    # development, it might be useful to set this to `:data` to emit log events
    # directly for each logged message instead of only once after the request
    # was finished.
    #
    # In the log event(s) emitted for the request, we add the default fields
    # described above, plus some data extracted from the HTTP request data. For
    # dynamic values, we can use the `::Rack::Request` object for the current
    # request. In our example above, we add the user agent header of the
    # client's webbrowser and the client's IP address. We also set a `"server"`
    # field containing the server's hostname. We are also setting some tags, in
    # this case we set the static tag `"rack"` and a tag for the current
    # request's scheme. Usually, this is either `"http"` or `"https"`.
    #
    # For the response, we have the same flexibility. Here, we can use the hash
    # containing the response headers to set additional fields or tags to the
    # log buffer. In our example above, we add the value of the `Location`
    # header of the response as a field and add a tag based on the status of the
    # Rack::Cache middleware our app might be using.
    #
    # Note that all fields set by the middleware are deep-merged into the
    # Buffer's current fields using {Fields::Hash#deep_merge!}. All existing
    # fields set by the application will be preserved and are not overwritten.
    #
    # In case there is an unhandled exception during request processing in the
    # Rack application or any lower middleware, we log this error using
    # {Rackstash::Logger#add_exception}. The emitted event will still contain
    # all added request fields and tags as well as the `"status"` field (set to
    # `500`) and the duration of the request so far.
    class Middleware
      include Rackstash::Helpers::Time

      # @return [Rackstash::Logger] the Rackstash logger used to log the
      #   request details
      attr_reader :logger

      # @return [Hash<#to_s => Object>, Proc] a `Hash` specifying fields to be
      #   logged for each request. The fields will be added to the log {Buffer}
      #   before the request is passed to the Rack application. You can either
      #   give a literal `Hash` object or a `Proc` which returns such a `Hash`.
      #
      #   When including `Proc`s, they are evaluated with the `Rack::Request`
      #   object as a scope. See {Rackstash::Fields::Hash#deep_merge!}.
      attr_accessor :request_fields

      # @return [Array<#to_s, Proc>, Proc] Strings to add as tags to the logged
      #   event. The fields will be added to the log {Buffer} before the request
      #   is passed to the Rack application. You can either give (`Array`s of)
{'fraction_non_alphanumeric': 0.08789459722345035, 'fraction_numerical': 0.003890706516933416, 'mean_word_length': 2.849557522123894, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5915342', 'n_tokens_mistral': 3201, 'n_tokens_neox': 3129, 'n_words': 1460}