Class: LogStruct::Log::Request

Inherits:
T::Struct
  • Object
show all
Extended by:
T::Sig
Includes:
AddRequestFields, Interfaces::CommonFields, Interfaces::RequestFields, SerializeCommon
Defined in:
lib/log_struct/log/request.rb

Overview

Request log entry for structured logging

Constant Summary collapse

RequestEvent =
T.type_alias {
  Event::Request
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AddRequestFields

#add_request_fields

Methods included from SerializeCommon

#as_json, #serialize_common

Constructor Details

#initialize(source: T.let(Source::Rails, Source::Rails), event: T.let(Event::Request, RequestEvent), timestamp:, level: T.let(Level::Info, Level), http_method: nil, path: nil, format: nil, controller: nil, action: nil, status: nil, duration: nil, view: nil, db: nil, params: nil, source_ip: nil, user_agent: nil, referer: nil, request_id: nil) ⇒ void

Parameters:

  • source (Source::Rails) (defaults to: T.let(Source::Rails, Source::Rails))

    Common fields

  • event (RequestEvent) (defaults to: T.let(Event::Request, RequestEvent))
  • timestamp (Time)
  • level (Level) (defaults to: T.let(Level::Info, Level))
  • http_method (String, nil) (defaults to: nil)

    Request-specific fields NOTE: method is a reserved word, so we use http_method prop while setting method in the serialized output

  • path (String, nil) (defaults to: nil)
  • format (String, nil) (defaults to: nil)
  • controller (String, nil) (defaults to: nil)
  • action (String, nil) (defaults to: nil)
  • status (Integer, nil) (defaults to: nil)
  • duration (Float, nil) (defaults to: nil)
  • view (Float, nil) (defaults to: nil)
  • db (Float, nil) (defaults to: nil)
  • params (Hash{Symbol => T.untyped}, nil) (defaults to: nil)
  • source_ip (String, nil) (defaults to: nil)
  • user_agent (String, nil) (defaults to: nil)
  • referer (String, nil) (defaults to: nil)
  • request_id (String, nil) (defaults to: nil)


# File ''

const :source, Source::Rails, default: T.let(Source::Rails, Source::Rails)
const :event, RequestEvent, default: T.let(Event::Request, RequestEvent)
const :timestamp, Time, factory: -> { Time.now }
const :level, Level, default: T.let(Level::Info, Level)
const :http_method, T.nilable(String), default: nil
const :path, T.nilable(String), default: nil
const :format, T.nilable(String), default: nil
const :controller, T.nilable(String), default: nil
const :action, T.nilable(String), default: nil
const :status, T.nilable(Integer), default: nil
const :duration, T.nilable(Float), default: nil
const :view, T.nilable(Float), default: nil
const :db, T.nilable(Float), default: nil
const :params, T.nilable(T::Hash[Symbol, T.untyped]), default: nil
const :source_ip, T.nilable(String), default: nil
const :user_agent, T.nilable(String), default: nil
const :referer, T.nilable(String), default: nil
const :request_id, T.nilable(String), default: nil

Instance Attribute Details

#actionString? (readonly)

Returns the value of prop action.

Returns:

  • (String, nil)


# File ''

const :action, T.nilable(String), default: nil

#controllerString? (readonly)

Returns the value of prop controller.

Returns:

  • (String, nil)


# File ''

const :controller, T.nilable(String), default: nil

#dbFloat? (readonly)

Returns the value of prop db.

Returns:

  • (Float, nil)


# File ''

const :db, T.nilable(Float), default: nil

#durationFloat? (readonly)

Returns the value of prop duration.

Returns:

  • (Float, nil)


# File ''

const :duration, T.nilable(Float), default: nil

#eventRequestEvent (readonly)

Returns the value of prop event.

Returns:



# File ''

const :event, RequestEvent, default: T.let(Event::Request, RequestEvent)

#formatString? (readonly)

Returns the value of prop format.

Returns:

  • (String, nil)


# File ''

const :format, T.nilable(String), default: nil

#http_methodString? (readonly)

Request-specific fields NOTE: method is a reserved word, so we use http_method prop while setting method in the serialized output

Returns:

  • (String, nil)


# File ''

const :http_method, T.nilable(String), default: nil

#levelLevel (readonly)

Returns the value of prop level.

Returns:



# File ''

const :level, Level, default: T.let(Level::Info, Level)

#paramsHash{Symbol => T.untyped}? (readonly)

Returns the value of prop params.

Returns:

  • (Hash{Symbol => T.untyped}, nil)


# File ''

const :params, T.nilable(T::Hash[Symbol, T.untyped]), default: nil

#pathString? (readonly)

Returns the value of prop path.

Returns:

  • (String, nil)


# File ''

const :path, T.nilable(String), default: nil

#refererString? (readonly)

Returns the value of prop referer.

Returns:

  • (String, nil)


# File ''

const :referer, T.nilable(String), default: nil

#request_idString? (readonly)

Returns the value of prop request_id.

Returns:

  • (String, nil)


# File ''

const :request_id, T.nilable(String), default: nil

#sourceSource::Rails (readonly)

Common fields

Returns:



# File ''

const :source, Source::Rails, default: T.let(Source::Rails, Source::Rails)

#source_ipString? (readonly)

Returns the value of prop source_ip.

Returns:

  • (String, nil)


# File ''

const :source_ip, T.nilable(String), default: nil

#statusInteger? (readonly)

Returns the value of prop status.

Returns:

  • (Integer, nil)


# File ''

const :status, T.nilable(Integer), default: nil

#timestampTime (readonly)

Returns the value of prop timestamp.

Returns:

  • (Time)


# File ''

const :timestamp, Time, factory: -> { Time.now }

#user_agentString? (readonly)

Returns the value of prop user_agent.

Returns:

  • (String, nil)


# File ''

const :user_agent, T.nilable(String), default: nil

#viewFloat? (readonly)

Returns the value of prop view.

Returns:

  • (Float, nil)


# File ''

const :view, T.nilable(Float), default: nil

Instance Method Details

#serialize(strict = true) ⇒ Hash{Symbol => T.untyped}

Convert the log entry to a hash for serialization

Parameters:

  • strict (Boolean) (defaults to: true)

Returns:

  • (Hash{Symbol => T.untyped})


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/log_struct/log/request.rb', line 54

def serialize(strict = true)
  hash = serialize_common(strict)
  add_request_fields(hash)
  hash[LOG_KEYS.fetch(:http_method)] = http_method if http_method
  hash[LOG_KEYS.fetch(:path)] = path if path
  hash[LOG_KEYS.fetch(:format)] = format if format
  hash[LOG_KEYS.fetch(:controller)] = controller if controller
  hash[LOG_KEYS.fetch(:action)] = action if action
  hash[LOG_KEYS.fetch(:status)] = status if status
  hash[LOG_KEYS.fetch(:duration)] = duration if duration
  hash[LOG_KEYS.fetch(:view)] = view if view
  hash[LOG_KEYS.fetch(:db)] = db if db
  hash[LOG_KEYS.fetch(:params)] = params if params
  hash[LOG_KEYS.fetch(:source_ip)] = source_ip if source_ip
  hash[LOG_KEYS.fetch(:user_agent)] = user_agent if user_agent
  hash[LOG_KEYS.fetch(:referer)] = referer if referer
  hash[LOG_KEYS.fetch(:request_id)] = request_id if request_id

  hash
end