Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // SuccessLogger is a logger used for logging successful operations. SuccessLogger *zap.Logger // ClientErrorLogger is a logger used for logging client-side errors. ClientErrorLogger *zap.Logger // ServerErrorLogger is a logger used for logging server-side errors. ServerErrorLogger *zap.Logger // Logs holds a collection of HTTP request logs. Logs *HttpRequestLogs // MaxLogEntries defines the maximum number of log entries that will be stored in memory. // Beyond this limit, older entries may be evicted or ignored. MaxLogEntries = 100000 // MemoryLimit specifies the maximum amount of memory (in bytes) that can be used for storing log entries. // This helps in preventing excessive memory usage by log storage. MemoryLimit int64 = 100 * 1048576 )
Functions ¶
This section is empty.
Types ¶
type HttpRequestLog ¶
type HttpRequestLog struct {
Timestamp time.Time
ReqHeaders http.Header // The Request Headers
ResHeaders http.Header // The Response Headers
RayID string // The Ray ID from the HTTP response
Request string // The URL of the HTTP Request
Status int // The status code of the response
RateLimitConsumed int // The number of requests consumed from the rate limit
Delay int64 // The time (in miliseconds) between the request and the server's response
Response string // The server response
// contains filtered or unexported fields
}
HttpRequestLog represents a single HTTP request log entry. It includes detailed information about the request and response, such as headers, status code, and response body.
Public Methods:
- WriteToLog(debug bool): Writes the log entry to the appropriate logger based on the HTTP response status.
- String() string: Returns a string representation of the HTTP request log entry.
- PrettyPrint(): Prints a formatted representation of the HTTP request log entry.
func AddToLog ¶
func AddToLog(h *HttpRequestLogs, timestamp time.Time, rayID string, request string, rateLimitConsumed int, delay int64, status int, body string, reqHeaders http.Header, resHeaders http.Header) *HttpRequestLog
AddToLog adds a new HTTP request log entry to the HttpRequestLogs.
This method creates a new HttpRequestLog entry based on the provided parameters and appends it to the HttpRequestLogs. After adding a new log entry, it trims the log to ensure the total memory usage and the number of log entries are below their limits.
Parameters:
- h *HttpRequestLogs: A pointer to the HttpRequestLogs to which the new log entry will be added.
- timestamp time.Time: The timestamp of the HTTP request.
- rayID string: The unique identifier for the request.
- request string: The URL of the HTTP request.
- rateLimitConsumed int: The amount of rate limit consumed by the request.
- delay int64: The delay experienced during the request, in milliseconds.
- status int: The HTTP status code of the response.
- body string: The body of the HTTP response.
- reqHeaders http.Header: The HTTP headers of the request.
- resHeaders http.Header: The HTTP headers of the response.
Returns:
- *HttpRequestLog: A pointer to the newly added HttpRequestLog entry. Returns nil if the log entry is not added.
func NewHttpRequestLog ¶ added in v1.0.0
func NewHttpRequestLog(timestamp time.Time, rayID string, request string, rateLimitConsumed int, delay int64, status int, body string, reqHeaders http.Header, resHeaders http.Header) HttpRequestLog
NewHttpRequestLog creates a new instance of HttpRequestLog with the provided parameters. This function initializes the log entry with details of the HTTP request and response, including timestamps, request and response headers, and other relevant information.
Parameters:
- timestamp: The time at which the HTTP request was made.
- rayID: A unique identifier for the request, typically used for tracing requests.
- request: The raw HTTP request data as a string.
- rateLimitConsumed: The amount of rate limit quota consumed by this request.
- delay: The delay experienced during the processing of the request, in milliseconds.
- status: The HTTP status code returned in the response.
- body: The body of the HTTP response.
- reqHeaders: The HTTP headers of the request.
- resHeaders: The HTTP headers of the response.
Returns:
- HttpRequestLog: An instance of HttpRequestLog populated with the provided parameters and calculated memory usage.
func (HttpRequestLog) PrettyPrint ¶ added in v1.0.0
func (h HttpRequestLog) PrettyPrint()
PrettyPrint prints a formatted representation of the HTTP request log entry.
func (HttpRequestLog) String ¶
func (h HttpRequestLog) String() string
String returns a string representation of the HTTP request log entry.
Returns:
- A string representing the log entry.
func (HttpRequestLog) WriteToLog ¶ added in v1.0.0
func (h HttpRequestLog) WriteToLog(debug bool)
WriteToLog writes the log entry to the appropriate logger based on the HTTP response status.
Parameters:
- debug: A boolean indicating whether to log as a debug message.
type HttpRequestLogs ¶
type HttpRequestLogs struct {
Logs []HttpRequestLog
}
HttpRequestLogs represents a collection of HTTP request logs. It provides methods to manipulate and retrieve log entries.
Public Methods:
- String() string: Returns a string representation of all HTTP request logs.
- PrintLatest(): Prints the latest HTTP request log entry.
func (*HttpRequestLogs) GetLastLogResponse ¶ added in v1.0.0
func (h *HttpRequestLogs) GetLastLogResponse() string
GetLastLogResponse returns the response of the last log entry in the HttpRequestLogs.
This method checks if there are any logs present. If there are no logs, it returns a message indicating that no logs are available. If logs are present, it calculates the index of the last log entry, accesses it, and returns its response.
Returns:
- A string representing the response of the last log entry. If no logs are available, returns "No logs available".
func (*HttpRequestLogs) PrintLatest ¶
func (h *HttpRequestLogs) PrintLatest()
PrintLatest prints the latest HTTP request log entry.
func (*HttpRequestLogs) String ¶
func (h *HttpRequestLogs) String() string
String returns a string representation of all HTTP request logs.
Returns:
- A string representing all log entries.