Documentation
¶
Index ¶
- func ExportLogHandler(cfg ViewerConfig) gin.HandlerFunc
- func Init(config LoggerConfig)
- func LogDebug(fileName, route, message, sql string, jsonResponse any)
- func LogError(fileName, route, message, sql string, jsonResponse any)
- func LogInfo(fileName, route, message, sql string, jsonResponse any)
- func LogWarning(fileName, route, message, sql string, jsonResponse any)
- func RemoveLoggerSync()
- func ViewerHandler(cfg ViewerConfig) gin.HandlerFunc
- func WriteSyncLogFile(entity, status string, pageNumbers, currentPage int)
- type JsonResponse
- type LogEntry
- type Logger
- type LoggerConfig
- type ViewerConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExportLogHandler ¶
func ExportLogHandler(cfg ViewerConfig) gin.HandlerFunc
ExportLogHandler returns a Gin handler for the log-export endpoint. Register it on a PROTECTED router — the endpoint reads production log files. Pass a ViewerConfig with AllowedRoleIDs to restrict access by role.
protectedRouter.GET("logger/:logField/:logData", easyGoLog.ExportLogHandler(cfg))
func Init ¶
func Init(config LoggerConfig)
Init initialises the package-level logger. Must be called once at application startup. If LogBasePath is empty it is derived as filepath.Dir(LogPath). The timezone for log timestamps defaults to Europe/Rome; UTC is used as fallback.
func LogDebug ¶
LogDebug writes a DEBUG-level entry. Omitted in preprod and prod environments (see LoggerConfig.AppEnv).
func LogError ¶
LogError writes an ERROR-level entry. fileName controls the destination file: "" uses the default configured in Init; a plain name (e.g. "bridge_log") writes to the default directory; a slash-separated path (e.g. "bridge/bridge_log") writes to a subdirectory under LogBasePath.
func LogWarning ¶
LogWarning writes a WARNING-level entry. Omitted in prod environment (see LoggerConfig.AppEnv).
func RemoveLoggerSync ¶
func RemoveLoggerSync()
RemoveLoggerSync truncates SYNC_STATUS.log, resetting the sync status display.
func ViewerHandler ¶
func ViewerHandler(cfg ViewerConfig) gin.HandlerFunc
ViewerHandler returns a Gin handler that serves the HTML log viewer page. The page handles its own JWT authentication via the browser's localStorage, so it can be registered on a public (unauthenticated) router.
publicRouter.GET("logger/viewer", easyGoLog.ViewerHandler(easyGoLog.ViewerConfig{
LoginURL: "/api/login",
LogAPIBase: "/api/logger",
Sources: []string{"app", "bridge", "shopify"},
}))
func WriteSyncLogFile ¶
WriteSyncLogFile appends a progress line to SYNC_STATUS.log. Used by the sync-client (Electron mode) to report per-entity import progress. When status is "OK" or "KO" the previous progress line is removed first, so the file always reflects the current operation.
Types ¶
type JsonResponse ¶
type JsonResponse struct {
JsonInput any `json:"jsonInput"`
JsonResponse any `json:"jsonResponse"`
}
JsonResponse is a helper type for attaching both input and output JSON to a log entry.
type LogEntry ¶
type LogEntry struct {
// Timestamp is the time the event was logged, in the configured timezone.
Timestamp time.Time `json:"timestamp"`
// Level is the severity: ERROR, WARNING, INFO, or DEBUG.
Level string `json:"level"`
// Route identifies the caller context, e.g. an HTTP route or domain operation.
Route string `json:"route"`
// Message is the human-readable description of the event.
Message string `json:"message"`
// Sql contains any SQL query associated with the event (may be empty).
Sql string `json:"sql"`
// JsonResponse holds any structured data attached to the event (optional).
JsonResponse any `json:"jsonResponse"`
}
LogEntry represents a single structured log event as stored in the JSON log file.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is the core log writer. Use Init to create the package-level instance, then call LogError, LogInfo, LogWarning, or LogDebug.
type LoggerConfig ¶
type LoggerConfig struct {
// LogBasePath is the root directory shared by all log modules, e.g. "logs".
// Derived from LogPath when empty.
LogBasePath string
// LogPath is the directory for this module's log files, e.g. "logs/myapp".
LogPath string
// LogFileName is the default base filename used when no override is passed to
// the write functions, e.g. "log".
LogFileName string
// AppEnv controls which severity levels are written to disk.
// "dev" → all levels; "preprod" → INFO/WARNING/ERROR; "prod" → INFO/ERROR.
AppEnv string
// DefaultLogField is the logField value that ExportLogHandler maps to the
// default log path (LogPath / LogFileName). Defaults to "app" when empty.
DefaultLogField string
}
LoggerConfig holds all configuration for the package-level logger. LogPath must be in the form "{LogBasePath}/{module}", e.g. "logs/myapp". LogBasePath is derived automatically from LogPath if not set explicitly.
type ViewerConfig ¶
type ViewerConfig struct {
// LoginURL is the POST endpoint used for authentication, e.g. "/api/login".
LoginURL string
// LogAPIBase is the GET base path for log data, e.g. "/api/logger".
LogAPIBase string
// Sources lists the log sources shown in the viewer's source dropdown.
// Each value must match the logField path segment used by ExportLogHandler.
Sources []string
// AllowedRoleIDs restricts access to users whose role matches one of these IDs.
// Leave empty to allow any authenticated user.
AllowedRoleIDs []string
// GinRoleContextKey is the Gin context key where the JWT middleware stores the role.
// Defaults to "X-Logged-User-Role".
GinRoleContextKey string
// RoleClaimKey is the JWT payload claim used as fallback when GinRoleContextKey
// is not set in the Gin context. Defaults to "role_id".
RoleClaimKey string
// MaxCachedFiles is the maximum number of log files kept in memory simultaneously.
// When exceeded, the least recently accessed file is evicted. Defaults to 10.
MaxCachedFiles int
}
ViewerConfig configures both the log viewer page and the ExportLogHandler endpoint.