Documentation
¶
Index ¶
- Variables
- func Debug(msg string, fields ...map[string]interface{})
- func Error(msg string, fields ...map[string]interface{})
- func ErrorWithErr(err error, msg string, fields ...map[string]interface{})
- func Fatal(msg string, fields ...map[string]interface{})
- func FatalWithErr(err error, msg string, fields ...map[string]interface{})
- func Info(msg string, fields ...map[string]interface{})
- func InitLogger(errorLogPath, ginLogPath string) (errorWriter io.Writer, ginWriter io.Writer)
- func InitUniRTMLogger(operationLogPath, errorLogPath string) (operationWriter io.Writer, errorWriter io.Writer)
- func Log(level zerolog.Level, v ...interface{})deprecated
- func Logf(level zerolog.Level, format string, v ...interface{})deprecated
- func Panic(msg string, fields ...map[string]interface{})
- func Trace(msg string, fields ...map[string]interface{})
- func Warn(msg string, fields ...map[string]interface{})
- func WithContext(fields map[string]interface{}) *zerolog.Logger
- func WithError(err error) *zerolog.Logger
- func WithFields(keysAndValues ...interface{}) *zerolog.Logger
- type ErrorHook
- type UniRTMErrorHook
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // Logger is the global logger with a human-friendly console writer by default. Logger = zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339}).With().Timestamp().Logger() // NoColor disables the colorized output. NoColor bool )
Functions ¶
func Error ¶
Error logs a message at error level with optional context fields. Stack traces are automatically captured for error-level logs.
Example ¶
ExampleError demonstrates error-level logging with automatic stack trace capture.
package main
import (
"github.com/snowdreamtech/unirtm/internal/pkg/logger"
)
func main() {
// Initialize logger
logger.InitUniRTMLogger("unirtm.log", "error.log")
// Log a simple error message (stack trace is automatically captured)
logger.Error("Failed to connect to database")
// Log with context fields
logger.Error("Installation failed", map[string]interface{}{
"tool": "python",
"version": "3.11.0",
"reason": "checksum mismatch",
})
}
Output:
func ErrorWithErr ¶
ErrorWithErr logs an error with its message and optional context fields. This is a convenience function for logging errors with automatic error field extraction.
Example ¶
ExampleErrorWithErr demonstrates logging errors with error objects.
package main
import (
"errors"
"github.com/snowdreamtech/unirtm/internal/pkg/logger"
)
func main() {
// Initialize logger
logger.InitUniRTMLogger("unirtm.log", "error.log")
// Create an error
err := errors.New("network timeout")
// Log error with message
logger.ErrorWithErr(err, "Failed to download artifact")
// Log error with context fields
logger.ErrorWithErr(err, "Download failed", map[string]interface{}{
"url": "https://example.com/artifact.tar.gz",
"retries": 5,
"timeout": "30s",
})
}
Output:
func Fatal ¶
Fatal logs a message at fatal level with optional context fields and exits. Stack traces are automatically captured for fatal-level logs.
func FatalWithErr ¶
FatalWithErr logs an error with its message and optional context fields, then exits.
func Info ¶
Info logs a message at info level with optional context fields.
Example ¶
ExampleInfo demonstrates basic info-level logging.
package main
import (
"github.com/snowdreamtech/unirtm/internal/pkg/logger"
)
func main() {
// Initialize logger
logger.InitUniRTMLogger("unirtm.log", "error.log")
// Log a simple info message
logger.Info("Application started successfully")
// Log with context fields
logger.Info("Tool installed", map[string]interface{}{
"tool": "node",
"version": "20.0.0",
"path": "/usr/local/bin/node",
})
}
Output:
func InitLogger ¶
InitLogger initializes the logger with the specified error and gin log paths. It returns the error and gin writers.
Parameters:
- errorLogPath: The path to the error log file.
- ginLogPath: The path to the gin log file.
Returns:
- errorWriter: The error writer.
- ginWriter: The gin writer.
func InitUniRTMLogger ¶
func InitUniRTMLogger(operationLogPath, errorLogPath string) (operationWriter io.Writer, errorWriter io.Writer)
InitUniRTMLogger initializes the logger for UniRTM with rotating file writers for both operation logs (unirtm.log) and error logs (error.log).
This function configures:
- Rotating file writer for unirtm.log (operations, max 500MB, 10 backups, 30 days retention)
- Rotating file writer for error.log (errors only, max 500MB, 10 backups, 30 days retention)
- Console output with color-coded log levels
- Structured logging with context fields
- Stack trace capture for errors
Parameters:
- operationLogPath: The path to the operation log file (default: "unirtm.log")
- errorLogPath: The path to the error log file (default: "error.log")
Returns:
- operationWriter: The operation log writer
- errorWriter: The error log writer
Example ¶
ExampleInitUniRTMLogger demonstrates how to initialize the UniRTM logger with rotating file writers for operation and error logs.
package main
import (
"fmt"
"github.com/snowdreamtech/unirtm/internal/pkg/logger"
)
func main() {
// Initialize logger with custom paths
opWriter, errWriter := logger.InitUniRTMLogger("unirtm.log", "error.log")
fmt.Printf("Operation writer: %v\n", opWriter != nil)
fmt.Printf("Error writer: %v\n", errWriter != nil)
}
Output: Operation writer: true Error writer: true
func Log
deprecated
Log logs a message at the specified log level. The log message is formatted with the current time, log level, and the provided arguments. The log output is written to the default writer of the gin framework.
Parameters:
- level: The log level as a string (e.g., "INF", "ERR").
TraceLevel: "TRC", DebugLevel: "DBG", InfoLevel: "INF", WarnLevel: "WRN", ErrorLevel: "ERR", FatalLevel: "FTL", PanicLevel: "PNC",
- v: Variadic arguments to be included in the log message.
Deprecated: Use Zerolog instead.
func Logf
deprecated
Logf logs a formatted message at the specified log level. The log message includes a timestamp, the log level, and the formatted message.
Parameters:
- level: The log level (e.g., "INF", "ERR").
TraceLevel: "TRC", DebugLevel: "DBG", InfoLevel: "INF", WarnLevel: "WRN", ErrorLevel: "ERR", FatalLevel: "FTL", PanicLevel: "PNC",
- format: The format string for the log message.
- v: The values to be formatted according to the format string.
Deprecated: Use Zerolog instead.
func Panic ¶
Panic logs a message at panic level with optional context fields and panics. Stack traces are automatically captured for panic-level logs.
func WithContext ¶
WithContext returns a logger with the specified context fields. This enables structured logging with key-value pairs.
Example:
logger.WithContext(map[string]interface{}{
"user_id": 123,
"operation": "install",
"tool": "node",
"version": "20.0.0",
}).Info("Installing tool")
Example ¶
ExampleWithContext demonstrates structured logging with context fields.
package main
import (
"github.com/snowdreamtech/unirtm/internal/pkg/logger"
)
func main() {
// Initialize logger
logger.InitUniRTMLogger("unirtm.log", "error.log")
// Create a logger with context
contextLogger := logger.WithContext(map[string]interface{}{
"request_id": "req-123456",
"user_id": "user-789",
"operation": "install",
})
// Use the context logger
contextLogger.Info().Msg("Starting installation")
contextLogger.Debug().Msg("Downloading artifact")
contextLogger.Info().Msg("Installation completed")
}
Output:
func WithError ¶
WithError returns a logger with the error field set. This is a convenience function for logging errors with context.
Example:
logger.WithError(err).Error("Failed to install tool")
func WithFields ¶
WithFields returns a logger with multiple fields set. This is a convenience function for structured logging.
Example:
logger.WithFields(
"user_id", 123,
"operation", "install",
).Info("Starting operation")
Example ¶
ExampleWithFields demonstrates structured logging with variadic key-value pairs.
package main
import (
"github.com/snowdreamtech/unirtm/internal/pkg/logger"
)
func main() {
// Initialize logger
logger.InitUniRTMLogger("unirtm.log", "error.log")
// Create a logger with fields
fieldsLogger := logger.WithFields(
"tool", "go",
"version", "1.21.0",
"backend", "github",
)
// Use the fields logger
fieldsLogger.Info().Msg("Tool resolved")
fieldsLogger.Debug().Msg("Downloading from GitHub")
fieldsLogger.Info().Msg("Installation successful")
}
Output:
Types ¶
type ErrorHook ¶
type ErrorHook struct{}
ErrorHook is a hook for logging errors.
func (ErrorHook) Run ¶
Run executes the ErrorHook, logging the event at the specified level with the given message. It writes the log output to the default error writer for the gin framework.
Parameters:
- e: The zerolog event to be logged.
- level: The logging level of the event.
- msg: The message to be logged.
type UniRTMErrorHook ¶
type UniRTMErrorHook struct{}
UniRTMErrorHook is a hook for logging errors in UniRTM. It captures stack traces for errors and writes them to the error log file.