mirror of
https://github.com/Noooste/garage-ui.git
synced 2026-08-17 08:37:49 +00:00
Initial gh-pages
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// Logger is a wrapper around zerolog.Logger
|
||||
type Logger struct {
|
||||
zerolog.Logger
|
||||
}
|
||||
|
||||
var (
|
||||
// Global logger instance
|
||||
globalLogger *Logger
|
||||
)
|
||||
|
||||
// Config holds logger configuration
|
||||
type Config struct {
|
||||
Level string // debug, info, warn, error
|
||||
Pretty bool // Enable console pretty printing
|
||||
TimeFormat string // Time format (default: time.RFC3339)
|
||||
}
|
||||
|
||||
// Init initializes the global logger with the given configuration
|
||||
func Init(cfg Config) {
|
||||
var output io.Writer = os.Stdout
|
||||
|
||||
// Set up pretty console output if enabled
|
||||
if cfg.Pretty {
|
||||
output = zerolog.ConsoleWriter{
|
||||
Out: os.Stdout,
|
||||
TimeFormat: time.RFC3339,
|
||||
NoColor: false,
|
||||
}
|
||||
}
|
||||
|
||||
// Parse log level
|
||||
level := zerolog.InfoLevel
|
||||
switch cfg.Level {
|
||||
case "debug":
|
||||
level = zerolog.DebugLevel
|
||||
case "info":
|
||||
level = zerolog.InfoLevel
|
||||
case "warn":
|
||||
level = zerolog.WarnLevel
|
||||
case "error":
|
||||
level = zerolog.ErrorLevel
|
||||
}
|
||||
|
||||
// Create logger
|
||||
logger := zerolog.New(output).
|
||||
Level(level).
|
||||
With().
|
||||
Timestamp().
|
||||
Caller().
|
||||
Logger()
|
||||
|
||||
globalLogger = &Logger{logger}
|
||||
log.Logger = logger
|
||||
}
|
||||
|
||||
// Get returns the global logger instance
|
||||
func Get() *Logger {
|
||||
if globalLogger == nil {
|
||||
// Initialize with defaults if not initialized
|
||||
Init(Config{
|
||||
Level: "info",
|
||||
Pretty: true,
|
||||
})
|
||||
}
|
||||
return globalLogger
|
||||
}
|
||||
|
||||
// Debug logs a debug message
|
||||
func Debug() *zerolog.Event {
|
||||
return Get().Debug()
|
||||
}
|
||||
|
||||
// Info logs an info message
|
||||
func Info() *zerolog.Event {
|
||||
return Get().Info()
|
||||
}
|
||||
|
||||
// Warn logs a warning message
|
||||
func Warn() *zerolog.Event {
|
||||
return Get().Warn()
|
||||
}
|
||||
|
||||
// Error logs an error message
|
||||
func Error() *zerolog.Event {
|
||||
return Get().Error()
|
||||
}
|
||||
|
||||
// Fatal logs a fatal message and exits
|
||||
func Fatal() *zerolog.Event {
|
||||
return Get().Fatal()
|
||||
}
|
||||
|
||||
// WithContext creates a new logger with additional context fields
|
||||
func (l *Logger) WithContext(fields map[string]interface{}) *Logger {
|
||||
ctx := l.Logger.With()
|
||||
for k, v := range fields {
|
||||
ctx = ctx.Interface(k, v)
|
||||
}
|
||||
return &Logger{ctx.Logger()}
|
||||
}
|
||||
|
||||
// WithComponent creates a logger with a component field
|
||||
func WithComponent(component string) *Logger {
|
||||
return &Logger{Get().With().Str("component", component).Logger()}
|
||||
}
|
||||
|
||||
// WithError creates a logger with an error field
|
||||
func WithError(err error) *zerolog.Event {
|
||||
return Get().Error().Err(err)
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// CacheItem represents a cached item with expiration
|
||||
type CacheItem struct {
|
||||
Value interface{}
|
||||
Expiration time.Time
|
||||
}
|
||||
|
||||
// Cache represents a simple in-memory cache with expiration
|
||||
type Cache struct {
|
||||
mu sync.RWMutex
|
||||
items map[string]CacheItem
|
||||
}
|
||||
|
||||
// NewCache creates a new cache instance
|
||||
func NewCache() *Cache {
|
||||
c := &Cache{
|
||||
items: make(map[string]CacheItem),
|
||||
}
|
||||
|
||||
// Start cleanup goroutine
|
||||
go c.cleanupExpired()
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
// Get retrieves a value from the cache
|
||||
func (c *Cache) Get(key string) interface{} {
|
||||
c.mu.RLock()
|
||||
defer c.mu.RUnlock()
|
||||
|
||||
item, exists := c.items[key]
|
||||
if !exists {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check if item has expired
|
||||
if time.Now().After(item.Expiration) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return item.Value
|
||||
}
|
||||
|
||||
// Set stores a value in the cache with an expiration duration
|
||||
func (c *Cache) Set(key string, value interface{}, duration time.Duration) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
c.items[key] = CacheItem{
|
||||
Value: value,
|
||||
Expiration: time.Now().Add(duration),
|
||||
}
|
||||
}
|
||||
|
||||
// Delete removes a value from the cache
|
||||
func (c *Cache) Delete(key string) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
delete(c.items, key)
|
||||
}
|
||||
|
||||
// Clear removes all items from the cache
|
||||
func (c *Cache) Clear() {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
c.items = make(map[string]CacheItem)
|
||||
}
|
||||
|
||||
// cleanupExpired periodically removes expired items
|
||||
func (c *Cache) cleanupExpired() {
|
||||
ticker := time.NewTicker(5 * time.Minute)
|
||||
defer ticker.Stop()
|
||||
|
||||
for range ticker.C {
|
||||
c.mu.Lock()
|
||||
now := time.Now()
|
||||
for key, item := range c.items {
|
||||
if now.After(item.Expiration) {
|
||||
delete(c.items, key)
|
||||
}
|
||||
}
|
||||
c.mu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
// Global cache instance
|
||||
var GlobalCache = NewCache()
|
||||
Reference in New Issue
Block a user