Documentation
¶
Overview ¶
Package logging provides Gin middleware for HTTP request logging and panic recovery. It integrates Gin web framework with logrus for structured logging of HTTP requests, responses, and error handling with panic recovery capabilities.
Package logging provides request logging functionality for the CLI Proxy API server. It handles capturing and storing detailed HTTP request and response data when enabled through configuration, supporting both regular and streaming responses.
Index ¶
- func ConfigureLogOutput(loggingToFile bool) error
- func GinLogrusLogger() gin.HandlerFunc
- func GinLogrusRecovery() gin.HandlerFunc
- func SetupBaseLogger()
- type FileRequestLogger
- func (l *FileRequestLogger) IsEnabled() bool
- func (l *FileRequestLogger) LogRequest(url, method string, requestHeaders map[string][]string, body []byte, ...) error
- func (l *FileRequestLogger) LogStreamingRequest(url, method string, headers map[string][]string, body []byte) (StreamingLogWriter, error)
- func (l *FileRequestLogger) SetEnabled(enabled bool)
- type FileStreamingLogWriter
- type LogFormatter
- type NoOpStreamingLogWriter
- type RequestLogger
- type StreamingLogWriter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConfigureLogOutput ¶ added in v6.0.4
ConfigureLogOutput switches the global log destination between rotating files and stdout.
func GinLogrusLogger ¶
func GinLogrusLogger() gin.HandlerFunc
GinLogrusLogger returns a Gin middleware handler that logs HTTP requests and responses using logrus. It captures request details including method, path, status code, latency, client IP, and any error messages, formatting them in a Gin-style log format.
Returns:
- gin.HandlerFunc: A middleware handler for request logging
func GinLogrusRecovery ¶
func GinLogrusRecovery() gin.HandlerFunc
GinLogrusRecovery returns a Gin middleware handler that recovers from panics and logs them using logrus. When a panic occurs, it captures the panic value, stack trace, and request path, then returns a 500 Internal Server Error response to the client.
Returns:
- gin.HandlerFunc: A middleware handler for panic recovery
func SetupBaseLogger ¶ added in v6.0.4
func SetupBaseLogger()
SetupBaseLogger configures the shared logrus instance and Gin writers. It is safe to call multiple times; initialization happens only once.
Types ¶
type FileRequestLogger ¶
type FileRequestLogger struct {
// contains filtered or unexported fields
}
FileRequestLogger implements RequestLogger using file-based storage. It provides file-based logging functionality for HTTP requests and responses.
func NewFileRequestLogger ¶
func NewFileRequestLogger(enabled bool, logsDir string, configDir string) *FileRequestLogger
NewFileRequestLogger creates a new file-based request logger.
Parameters:
- enabled: Whether request logging should be enabled
- logsDir: The directory where log files should be stored (can be relative)
- configDir: The directory of the configuration file; when logsDir is relative, it will be resolved relative to this directory
Returns:
- *FileRequestLogger: A new file-based request logger instance
func (*FileRequestLogger) IsEnabled ¶
func (l *FileRequestLogger) IsEnabled() bool
IsEnabled returns whether request logging is currently enabled.
Returns:
- bool: True if logging is enabled, false otherwise
func (*FileRequestLogger) LogRequest ¶
func (l *FileRequestLogger) LogRequest(url, method string, requestHeaders map[string][]string, body []byte, statusCode int, responseHeaders map[string][]string, response, apiRequest, apiResponse []byte, apiResponseErrors []*interfaces.ErrorMessage) error
LogRequest logs a complete non-streaming request/response cycle to a file.
Parameters:
- url: The request URL
- method: The HTTP method
- requestHeaders: The request headers
- body: The request body
- statusCode: The response status code
- responseHeaders: The response headers
- response: The raw response data
- apiRequest: The API request data
- apiResponse: The API response data
Returns:
- error: An error if logging fails, nil otherwise
func (*FileRequestLogger) LogStreamingRequest ¶
func (l *FileRequestLogger) LogStreamingRequest(url, method string, headers map[string][]string, body []byte) (StreamingLogWriter, error)
LogStreamingRequest initiates logging for a streaming request.
Parameters:
- url: The request URL
- method: The HTTP method
- headers: The request headers
- body: The request body
Returns:
- StreamingLogWriter: A writer for streaming response chunks
- error: An error if logging initialization fails, nil otherwise
func (*FileRequestLogger) SetEnabled ¶
func (l *FileRequestLogger) SetEnabled(enabled bool)
SetEnabled updates the request logging enabled state. This method allows dynamic enabling/disabling of request logging.
Parameters:
- enabled: Whether request logging should be enabled
type FileStreamingLogWriter ¶
type FileStreamingLogWriter struct {
// contains filtered or unexported fields
}
FileStreamingLogWriter implements StreamingLogWriter for file-based streaming logs. It handles asynchronous writing of streaming response chunks to a file.
func (*FileStreamingLogWriter) Close ¶
func (w *FileStreamingLogWriter) Close() error
Close finalizes the log file and cleans up resources.
Returns:
- error: An error if closing fails, nil otherwise
func (*FileStreamingLogWriter) WriteChunkAsync ¶
func (w *FileStreamingLogWriter) WriteChunkAsync(chunk []byte)
WriteChunkAsync writes a response chunk asynchronously (non-blocking).
Parameters:
- chunk: The response chunk to write
func (*FileStreamingLogWriter) WriteStatus ¶
func (w *FileStreamingLogWriter) WriteStatus(status int, headers map[string][]string) error
WriteStatus writes the response status and headers to the log.
Parameters:
- status: The response status code
- headers: The response headers
Returns:
- error: An error if writing fails, nil otherwise
type LogFormatter ¶ added in v6.0.4
type LogFormatter struct{}
LogFormatter defines a custom log format for logrus. This formatter adds timestamp, level, and source location to each log entry.
type NoOpStreamingLogWriter ¶
type NoOpStreamingLogWriter struct{}
NoOpStreamingLogWriter is a no-operation implementation for when logging is disabled. It implements the StreamingLogWriter interface but performs no actual logging operations.
func (*NoOpStreamingLogWriter) Close ¶
func (w *NoOpStreamingLogWriter) Close() error
Close is a no-op implementation that does nothing and always returns nil.
Returns:
- error: Always returns nil
func (*NoOpStreamingLogWriter) WriteChunkAsync ¶
func (w *NoOpStreamingLogWriter) WriteChunkAsync(_ []byte)
WriteChunkAsync is a no-op implementation that does nothing.
Parameters:
- chunk: The response chunk (ignored)
func (*NoOpStreamingLogWriter) WriteStatus ¶
func (w *NoOpStreamingLogWriter) WriteStatus(_ int, _ map[string][]string) error
WriteStatus is a no-op implementation that does nothing and always returns nil.
Parameters:
- status: The response status code (ignored)
- headers: The response headers (ignored)
Returns:
- error: Always returns nil
type RequestLogger ¶
type RequestLogger interface { // LogRequest logs a complete non-streaming request/response cycle. // // Parameters: // - url: The request URL // - method: The HTTP method // - requestHeaders: The request headers // - body: The request body // - statusCode: The response status code // - responseHeaders: The response headers // - response: The raw response data // - apiRequest: The API request data // - apiResponse: The API response data // // Returns: // - error: An error if logging fails, nil otherwise LogRequest(url, method string, requestHeaders map[string][]string, body []byte, statusCode int, responseHeaders map[string][]string, response, apiRequest, apiResponse []byte, apiResponseErrors []*interfaces.ErrorMessage) error // LogStreamingRequest initiates logging for a streaming request and returns a writer for chunks. // // Parameters: // - url: The request URL // - method: The HTTP method // - headers: The request headers // - body: The request body // // Returns: // - StreamingLogWriter: A writer for streaming response chunks // - error: An error if logging initialization fails, nil otherwise LogStreamingRequest(url, method string, headers map[string][]string, body []byte) (StreamingLogWriter, error) // IsEnabled returns whether request logging is currently enabled. // // Returns: // - bool: True if logging is enabled, false otherwise IsEnabled() bool }
RequestLogger defines the interface for logging HTTP requests and responses. It provides methods for logging both regular and streaming HTTP request/response cycles.
type StreamingLogWriter ¶
type StreamingLogWriter interface { // WriteChunkAsync writes a response chunk asynchronously (non-blocking). // // Parameters: // - chunk: The response chunk to write WriteChunkAsync(chunk []byte) // WriteStatus writes the response status and headers to the log. // // Parameters: // - status: The response status code // - headers: The response headers // // Returns: // - error: An error if writing fails, nil otherwise WriteStatus(status int, headers map[string][]string) error // Close finalizes the log file and cleans up resources. // // Returns: // - error: An error if closing fails, nil otherwise Close() error }
StreamingLogWriter handles real-time logging of streaming response chunks. It provides methods for writing streaming response data asynchronously.