Documentation
¶
Index ¶
- func Accepted(w http.ResponseWriter, r *http.Request, data any)
- func ContextMiddleware() func(http.Handler) http.Handler
- func Created(w http.ResponseWriter, r *http.Request, data any)
- func Critical(msg string, err error)
- func Criticalf(err error, format string, v ...interface{})
- func Debug(msg string)
- func Debugf(format string, v ...interface{})
- func Error(msg string)
- func Errorf(format string, v ...interface{})
- func Fatal(msg string)
- func Fatalf(format string, v ...interface{})
- func GetOrgIDFromContext(ctx context.Context) string
- func GetRequestIDFromContext(ctx context.Context) string
- func GetTraceIDFromContext(ctx context.Context) string
- func GetUserIDFromContext(ctx context.Context) string
- func Info(msg string)
- func Infof(format string, v ...interface{})
- func InitGlobalLogger(cfg LogConfig)
- func LoggingMiddleware(logger *Logger) func(http.Handler) http.Handler
- func NoContent(w http.ResponseWriter, r *http.Request)
- func OK(w http.ResponseWriter, r *http.Request, data any)
- func RecoveryMiddleware(logger *Logger) func(http.Handler) http.Handler
- func RespondError(w http.ResponseWriter, r *http.Request, err error)
- func Warn(msg string)
- func Warnf(format string, v ...interface{})
- func WithOrgID(ctx context.Context, orgID string) context.Context
- func WithRequestID(ctx context.Context, requestID string) context.Context
- func WithTraceID(ctx context.Context, traceID string) context.Context
- func WithUserID(ctx context.Context, userID string) context.Context
- func WriteAppError(w http.ResponseWriter, r *http.Request, err *AppError)
- func WriteError(w http.ResponseWriter, r *http.Request, status int, code, message string, ...)
- func WriteJSON(w http.ResponseWriter, status int, v any)
- func WriteList(w http.ResponseWriter, r *http.Request, status int, data []any, page any)
- func WriteSuccess(w http.ResponseWriter, r *http.Request, status int, data any)
- type AppError
- func BadRequest(message string) *AppError
- func Conflict(message string) *AppError
- func Forbidden(message string) *AppError
- func InternalServerError(message string) *AppError
- func IsAppError(err error) (*AppError, bool)
- func NewAppError(httpStatus int, code, message string) *AppError
- func NotFound(resource, id string) *AppError
- func ServiceUnavailable(message string) *AppError
- func Unauthorized(message string) *AppError
- func UnprocessableEntity(message string) *AppError
- func ValidationError(err error) *AppError
- type ErrorBody
- type ErrorDetail
- type ErrorResponse
- type FieldErrors
- type List
- type LogConfig
- type LogLevel
- type Logger
- func (l *Logger) Critical(msg string, err error)
- func (l *Logger) Criticalf(err error, format string, v ...interface{})
- func (l *Logger) Debug(msg string)
- func (l *Logger) Debugf(format string, v ...interface{})
- func (l *Logger) Error(msg string)
- func (l *Logger) Errorf(format string, v ...interface{})
- func (l *Logger) Fatal(msg string)
- func (l *Logger) Fatalf(format string, v ...interface{})
- func (l *Logger) Info(msg string)
- func (l *Logger) Infof(format string, v ...interface{})
- func (l *Logger) LogAudit(actor, action, resource string, metadata map[string]interface{})
- func (l *Logger) LogDatabaseQuery(query string, duration time.Duration, err error)
- func (l *Logger) LogHTTPRequest(method, path string, statusCode int, duration time.Duration, ...)
- func (l *Logger) LogMQEvent(eventType, topic string, success bool, metadata map[string]interface{})
- func (l *Logger) LogServiceState(state, action string, metadata map[string]interface{})
- func (l *Logger) Warn(msg string)
- func (l *Logger) Warnf(format string, v ...interface{})
- func (l *Logger) WithContext(ctx context.Context) *Logger
- func (l *Logger) WithError(err error) *Logger
- func (l *Logger) WithField(key string, value interface{}) *Logger
- func (l *Logger) WithFields(fields map[string]interface{}) *Logger
- type Meta
- type Success
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContextMiddleware ¶
ContextMiddleware adds common context values from headers
func GetOrgIDFromContext ¶
GetOrgIDFromContext extracts organization ID from context
func GetRequestIDFromContext ¶
GetRequestIDFromContext extracts request ID from context
func GetTraceIDFromContext ¶
GetTraceIDFromContext extracts trace ID from context
func GetUserIDFromContext ¶
GetUserIDFromContext extracts user ID from context
func InitGlobalLogger ¶
func InitGlobalLogger(cfg LogConfig)
InitGlobalLogger initializes global logger
func LoggingMiddleware ¶
LoggingMiddleware only logs failed HTTP requests.
func RecoveryMiddleware ¶
RecoveryMiddleware recovers from panics and logs them
func RespondError ¶
func RespondError(w http.ResponseWriter, r *http.Request, err error)
RespondError handles error response - checks if error is AppError, otherwise returns 500
func WithRequestID ¶
WithRequestID adds request ID to context
func WithTraceID ¶
WithTraceID adds trace ID to context (for distributed tracing)
func WithUserID ¶
WithUserID adds user ID to context
func WriteAppError ¶
func WriteAppError(w http.ResponseWriter, r *http.Request, err *AppError)
WriteAppError writes error response from AppError
func WriteError ¶
func WriteError(w http.ResponseWriter, r *http.Request, status int, code, message string, details map[string]any)
WriteError writes error response
func WriteSuccess ¶
WriteSuccess writes success response with data
Types ¶
type AppError ¶
type AppError struct {
HTTPStatus int // HTTP status code
Code string // Error code untuk client
Message string // Human-readable message
Details map[string]any // Additional error details
Err error // Underlying error (tidak di-expose ke client)
}
AppError represents application error with HTTP status code, error code, message, and details
func InternalServerError ¶
func IsAppError ¶
IsAppError checks if error is AppError
func NewAppError ¶
NewAppError creates a new application error
func ServiceUnavailable ¶
func Unauthorized ¶
func UnprocessableEntity ¶
func ValidationError ¶
ValidationError creates validation error from validator errors
func (*AppError) WithDetail ¶
WithDetail adds a single detail to the error
func (*AppError) WithDetails ¶
WithDetails adds details to the error
type ErrorDetail ¶
type ErrorDetail struct {
Detail error `json:"-"`
}
func NewErrorDetail ¶
func NewErrorDetail(err error) *ErrorDetail
func Wrap ¶
func Wrap(err error) *ErrorDetail
func (*ErrorDetail) MarshalJSON ¶
func (e *ErrorDetail) MarshalJSON() ([]byte, error)
type ErrorResponse ¶
type FieldErrors ¶
func ParseValidationErrors ¶
func ParseValidationErrors(err error) FieldErrors
type LogConfig ¶
type LogConfig struct {
Level LogLevel // Log level
ServiceName string // Service name (e.g., "control-plane-api")
Environment string // Environment (e.g., "production", "staging", "development")
Pretty bool // Pretty print for development
Output io.Writer
ErrorOutput io.Writer
ErrorFilePath string
}
LogConfig holds logger configuration
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger wraps zerolog with additional functionality
func GetGlobalLogger ¶
func GetGlobalLogger() *Logger
GetGlobalLogger returns global logger instance
func (*Logger) Critical ¶
Critical logs critical error (for alerting) This should trigger alerts in Prometheus/Alertmanager
func (*Logger) LogDatabaseQuery ¶
LogDatabaseQuery logs database queries (for debugging)
func (*Logger) LogHTTPRequest ¶
func (l *Logger) LogHTTPRequest(method, path string, statusCode int, duration time.Duration, metadata map[string]interface{})
LogHTTPRequest logs HTTP requests
func (*Logger) LogMQEvent ¶
LogMQEvent logs message queue events
func (*Logger) LogServiceState ¶
LogServiceState logs service state changes
func (*Logger) WithContext ¶
WithContext extracts logger from context or returns current logger
func (*Logger) WithFields ¶
WithFields adds structured fields to logger