Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
65cbfe78c3 | |||
6d60f7f813 | |||
d9564c2b98 | |||
04f9b0db6e | |||
93557356e9 | |||
13a7d8db91 | |||
bdad721b3d |
134
colors.go
Normal file
134
colors.go
Normal file
@@ -0,0 +1,134 @@
|
||||
package cylogger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
// Reset
|
||||
Reset = "\033[0m" // Text Reset
|
||||
|
||||
// Regular Colors
|
||||
Black = "\033[0;30m" // Black
|
||||
Red = "\033[0;31m" // Red
|
||||
Green = "\033[0;32m" // Green
|
||||
Yellow = "\033[0;33m" // Yellow
|
||||
Blue = "\033[0;34m" // Blue
|
||||
Purple = "\033[0;35m" // Purple
|
||||
Cyan = "\033[0;36m" // Cyan
|
||||
White = "\033[0;37m" // White
|
||||
Orange = "\033[38;5;208m" // Orange
|
||||
Pink = "\033[38;5;205m" // Pink
|
||||
Brown = "\033[38;5;130m" // Brown
|
||||
Gray = "\033[38;5;240m" // Gray
|
||||
Magenta = "\033[38;5;201m" // Magenta
|
||||
|
||||
// Bold
|
||||
BBlack = "\033[1;30m" // Black
|
||||
BRed = "\033[1;31m" // Red
|
||||
BGreen = "\033[1;32m" // Green
|
||||
BYellow = "\033[1;33m" // Yellow
|
||||
BBlue = "\033[1;34m" // Blue
|
||||
BPurple = "\033[1;35m" // Purple
|
||||
BCyan = "\033[1;36m" // Cyan
|
||||
BWhite = "\033[1;37m" // White
|
||||
BOrange = "\033[1;38;5;208m" // Bold Orange
|
||||
BPink = "\033[1;38;5;205m" // Bold Pink
|
||||
BBrown = "\033[1;38;5;130m" // Bold Brown
|
||||
BGray = "\033[1;38;5;240m" // Bold Gray
|
||||
BMagenta = "\033[1;38;5;201m" // Bold Magenta
|
||||
|
||||
// Underline
|
||||
UBlack = "\033[4;30m" // Black
|
||||
URed = "\033[4;31m" // Red
|
||||
UGreen = "\033[4;32m" // Green
|
||||
UYellow = "\033[4;33m" // Yellow
|
||||
UBlue = "\033[4;34m" // Blue
|
||||
UPurple = "\033[4;35m" // Purple
|
||||
UCyan = "\033[4;36m" // Cyan
|
||||
UWhite = "\033[4;37m" // White
|
||||
UOrange = "\033[4;38;5;208m" // Underline Orange
|
||||
UPink = "\033[4;38;5;205m" // Underline Pink
|
||||
UBrown = "\033[4;38;5;130m" // Underline Brown
|
||||
UGray = "\033[4;38;5;240m" // Underline Gray
|
||||
UMagenta = "\033[4;38;5;201m" // Underline Magenta
|
||||
|
||||
// Background
|
||||
On_Black = "\033[40m" // Black
|
||||
On_Red = "\033[41m" // Red
|
||||
On_Green = "\033[42m" // Green
|
||||
On_Yellow = "\033[43m" // Yellow
|
||||
On_Blue = "\033[44m" // Blue
|
||||
On_Purple = "\033[45m" // Purple
|
||||
On_Cyan = "\033[46m" // Cyan
|
||||
On_White = "\033[47m" // White
|
||||
On_Orange = "\033[48;5;208m" // Orange Background
|
||||
On_Pink = "\033[48;5;205m" // Pink Background
|
||||
On_Brown = "\033[48;5;130m" // Brown Background
|
||||
On_Gray = "\033[48;5;240m" // Gray Background
|
||||
On_Magenta = "\033[48;5;201m" // Magenta Background
|
||||
|
||||
// High Intensty
|
||||
IBlack = "\033[0;90m" // Black
|
||||
IRed = "\033[0;91m" // Red
|
||||
IGreen = "\033[0;92m" // Green
|
||||
IYellow = "\033[0;93m" // Yellow
|
||||
IBlue = "\033[0;94m" // Blue
|
||||
IPurple = "\033[0;95m" // Purple
|
||||
ICyan = "\033[0;96m" // Cyan
|
||||
IWhite = "\033[0;97m" // White
|
||||
IOrange = "\033[0;38;5;208m" // Intense Orange
|
||||
IPink = "\033[0;38;5;205m" // Intense Pink
|
||||
IBrown = "\033[0;38;5;130m" // Intense Brown
|
||||
IGray = "\033[0;38;5;240m" // Intense Gray
|
||||
IMagenta = "\033[0;38;5;201m" // Intense Magenta
|
||||
|
||||
// Bold High Intensty
|
||||
BIBlack = "\033[1;90m" // Black
|
||||
BIRed = "\033[1;91m" // Red
|
||||
BIGreen = "\033[1;92m" // Green
|
||||
BIYellow = "\033[1;93m" // Yellow
|
||||
BIBlue = "\033[1;94m" // Blue
|
||||
BIPurple = "\033[1;95m" // Purple
|
||||
BICyan = "\033[1;96m" // Cyan
|
||||
BIWhite = "\033[1;97m" // White
|
||||
BIOrange = "\033[1;38;5;208m" // Bold Intense Orange
|
||||
BIPink = "\033[1;38;5;205m" // Bold Intense Pink
|
||||
BIBrown = "\033[1;38;5;130m" // Bold Intense Brown
|
||||
BIGray = "\033[1;38;5;240m" // Bold Intense Gray
|
||||
BIMagenta = "\033[1;38;5;201m" // Bold Intense Magenta
|
||||
|
||||
// High Intensty backgrounds
|
||||
On_IBlack = "\033[0;100m" // Black
|
||||
On_IRed = "\033[0;101m" // Red
|
||||
On_IGreen = "\033[0;102m" // Green
|
||||
On_IYellow = "\033[0;103m" // Yellow
|
||||
On_IBlue = "\033[0;104m" // Blue
|
||||
On_IPurple = "\033[10;95m" // Purple
|
||||
On_ICyan = "\033[0;106m" // Cyan
|
||||
On_IWhite = "\033[0;107m" // White
|
||||
On_IOrange = "\033[0;48;5;208m" // Intense Orange Background
|
||||
On_IPink = "\033[0;48;5;205m" // Intense Pink Background
|
||||
On_IBrown = "\033[0;48;5;130m" // Intense Brown Background
|
||||
On_IGray = "\033[0;48;5;240m" // Intense Gray Background
|
||||
On_IMagenta = "\033[0;48;5;201m" // Intense Magenta Background
|
||||
)
|
||||
|
||||
// The acceptable range is [16, 231] but here we remove some very dark colors
|
||||
// That make text unreadable on a dark terminal
|
||||
// See https://www.hackitu.de/termcolor256/
|
||||
var colors = []int{22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 57, 62, 63, 64, 65, 67, 68, 69, 70, 71, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 148, 149, 150, 151, 152, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 184, 185, 186, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 226, 227, 228, 229, 230}
|
||||
var colorsIndex int = -1
|
||||
var shuffled bool
|
||||
|
||||
func GenerateRandomAnsiColor() string {
|
||||
if !shuffled {
|
||||
rand.Shuffle(len(colors), func(i int, j int) {
|
||||
colors[i], colors[j] = colors[j], colors[i]
|
||||
})
|
||||
shuffled = true
|
||||
}
|
||||
colorsIndex++
|
||||
return fmt.Sprintf("\033[1;4;38;5;%dm", colors[colorsIndex%len(colors)])
|
||||
}
|
229
main.go
229
main.go
@@ -1,4 +1,4 @@
|
||||
package logger
|
||||
package cylogger
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -38,38 +38,53 @@ const (
|
||||
LevelPrefix
|
||||
)
|
||||
|
||||
var levelNames = map[LogLevel]string{
|
||||
LevelError: "ERROR",
|
||||
LevelWarning: "WARNING",
|
||||
LevelInfo: "INFO",
|
||||
LevelDebug: "DEBUG",
|
||||
LevelTrace: "TRACE",
|
||||
LevelLua: "LUA",
|
||||
LevelPrefix: "PREFIX",
|
||||
// LevelStyle defines the visual style for a log level
|
||||
type LevelStyle struct {
|
||||
Tag string // e.g., "ERROR", "INFO"
|
||||
TagColor string // ANSI code for tag text
|
||||
TagBackgroundColor string // ANSI code for tag background
|
||||
MessageColor string // ANSI code for message text
|
||||
MessageBackgroundColor string // ANSI code for message background
|
||||
}
|
||||
|
||||
var levelColors = map[LogLevel]string{
|
||||
LevelError: "\033[1;31m", // Bold Red
|
||||
LevelWarning: "\033[1;33m", // Bold Yellow
|
||||
LevelInfo: "\033[1;32m", // Bold Green
|
||||
LevelDebug: "\033[1;36m", // Bold Cyan
|
||||
LevelTrace: "\033[1;35m", // Bold Magenta
|
||||
LevelLua: "\033[1;34m", // Bold Blue
|
||||
LevelPrefix: "\033[0;90m", // Regular Dark Grey
|
||||
// levelStyles maps LogLevel to its display style
|
||||
var levelStyles = map[LogLevel]LevelStyle{
|
||||
LevelError: {
|
||||
Tag: "ERROR",
|
||||
TagColor: BIRed, // Bold Intense Red
|
||||
TagBackgroundColor: On_White, // White background
|
||||
MessageColor: White, // Bold White text
|
||||
MessageBackgroundColor: On_IRed, // Intense Red background
|
||||
},
|
||||
LevelWarning: {
|
||||
Tag: "WARNING",
|
||||
TagColor: BIOrange, // Bold Intense Orange
|
||||
TagBackgroundColor: On_White, // White background
|
||||
MessageColor: White, // Bold White text
|
||||
MessageBackgroundColor: On_IOrange, // Intense Orange background
|
||||
},
|
||||
LevelInfo: {
|
||||
Tag: "INFO",
|
||||
TagColor: BGreen, // Bold Green
|
||||
},
|
||||
LevelDebug: {
|
||||
Tag: "DEBUG",
|
||||
TagColor: BCyan, // Bold Cyan
|
||||
},
|
||||
LevelTrace: {
|
||||
Tag: "TRACE",
|
||||
TagColor: BPurple, // Bold Purple
|
||||
},
|
||||
LevelLua: {
|
||||
Tag: "LUA",
|
||||
TagColor: BBlue, // Bold Blue
|
||||
},
|
||||
LevelPrefix: {
|
||||
Tag: "PREFIX", // Used for coloring the user prefix
|
||||
TagColor: BIGray, // Bold Gray
|
||||
},
|
||||
}
|
||||
|
||||
// ANSI Background Colors
|
||||
var levelBackgroundColors = map[LogLevel]string{
|
||||
LevelError: "\033[41m", // Red Background
|
||||
LevelWarning: "\033[43m", // Yellow Background
|
||||
}
|
||||
|
||||
// ANSI Foreground Colors (adjusting for readability on backgrounds)
|
||||
const FgWhiteBold = "\033[1;37m"
|
||||
|
||||
// ResetColor is the ANSI code to reset text color
|
||||
const ResetColor = "\033[0m"
|
||||
|
||||
// Logger is our custom logger with level support
|
||||
type Logger struct {
|
||||
mu sync.Mutex
|
||||
@@ -115,8 +130,8 @@ func ParseLevel(levelStr string) LogLevel {
|
||||
|
||||
// String returns the string representation of the log level
|
||||
func (l LogLevel) String() string {
|
||||
if name, ok := levelNames[l]; ok {
|
||||
return name
|
||||
if name, ok := levelStyles[l]; ok {
|
||||
return name.Tag
|
||||
}
|
||||
return fmt.Sprintf("Level(%d)", l)
|
||||
}
|
||||
@@ -318,56 +333,52 @@ func (l *Logger) formatMessage(level LogLevel, format string, args ...interface{
|
||||
fields = " " + strings.Join(pairs, " ")
|
||||
}
|
||||
|
||||
var levelColor, bgColor, msgFgColor, resetColor string
|
||||
useBgColor := false // Flag to indicate if background color should be used
|
||||
var tagFgColor, tagBgColor, messageBgColor string
|
||||
useSpecialFormatting := false // Flag for levels with custom message background/foreground
|
||||
if l.useColors {
|
||||
resetColor = ResetColor
|
||||
levelColor = levelColors[level] // Color for the level tag text ONLY
|
||||
|
||||
if bg, ok := levelBackgroundColors[level]; ok { // Check if this level has a background color defined (ERROR/WARNING)
|
||||
bgColor = bg
|
||||
msgFgColor = FgWhiteBold // Use bold white for the message part on colored background
|
||||
useBgColor = true
|
||||
// Check if a message background color is defined for this level style
|
||||
if levelStyles[level].MessageBackgroundColor != "" {
|
||||
useSpecialFormatting = true
|
||||
// Retrieve all style components from the map
|
||||
tagFgColor = levelStyles[level].TagColor // Assign directly
|
||||
tagBgColor = levelStyles[level].TagBackgroundColor
|
||||
// messageFgColor = levelStyles[level].MessageColor
|
||||
messageBgColor = levelStyles[level].MessageBackgroundColor
|
||||
} else {
|
||||
// For other levels, message part uses default terminal color.
|
||||
// msgFgColor remains empty, bgColor remains empty.
|
||||
// levelColor is still needed for the tag below.
|
||||
// For other levels (INFO, DEBUG, etc.), only TagColor is guaranteed
|
||||
tagFgColor = levelStyles[level].TagColor // Use the defined tag color
|
||||
// tagBgColor, messageFgColor, messageBgColor remain empty (use terminal defaults)
|
||||
}
|
||||
}
|
||||
|
||||
var caller string
|
||||
if l.flag&log.Lshortfile != 0 || l.flag&log.Llongfile != 0 {
|
||||
// Find the actual caller by scanning up the stack
|
||||
// until we find a function outside the logger package
|
||||
var file string
|
||||
var line int
|
||||
var ok bool
|
||||
|
||||
// Start at a reasonable depth and scan up to 10 frames
|
||||
for depth := 4; depth < 15; depth++ {
|
||||
_, file, line, ok = runtime.Caller(depth)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
// If the caller is not in the logger package, we found our caller
|
||||
if !strings.Contains(file, "logger/logger.go") {
|
||||
// Check if the caller is within this logger package itself
|
||||
if !strings.Contains(file, "main.go") && !strings.Contains(file, "colors.go") {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !ok {
|
||||
file = "???"
|
||||
line = 0
|
||||
}
|
||||
|
||||
if l.flag&log.Lshortfile != 0 {
|
||||
file = filepath.Base(file)
|
||||
}
|
||||
// Caller string - no background color applied here
|
||||
caller = fmt.Sprintf("%-25s ", file+":"+strconv.Itoa(line))
|
||||
}
|
||||
|
||||
// Format the timestamp with fixed width
|
||||
// Format the timestamp with fixed width - no background color applied here
|
||||
var timeStr string
|
||||
if l.flag&(log.Ldate|log.Ltime|log.Lmicroseconds) != 0 {
|
||||
t := time.Now()
|
||||
@@ -383,41 +394,75 @@ func (l *Logger) formatMessage(level LogLevel, format string, args ...interface{
|
||||
timeStr = fmt.Sprintf("%-15s ", timeStr)
|
||||
}
|
||||
|
||||
// Add goroutine ID if enabled, with fixed width
|
||||
// Add goroutine ID if enabled, with fixed width - no background color applied here
|
||||
var goroutineStr string
|
||||
if l.showGoroutine {
|
||||
goroutineID := GetGoroutineID()
|
||||
goroutineStr = fmt.Sprintf("[g:%-4s] ", goroutineID)
|
||||
}
|
||||
|
||||
// Create a colored level indicator - color only the level name text, reset locally
|
||||
levelStr := fmt.Sprintf("%s%s%s", levelColor, levelNames[level], resetColor)
|
||||
levelColumn := fmt.Sprintf("%-20s", levelStr) // Pad the tag
|
||||
// --- Level Tag Formatting and Padding ---
|
||||
levelStr := levelStyles[level].Tag
|
||||
visibleTagContent := fmt.Sprintf("[%s]", levelStr)
|
||||
visibleTagLen := len(visibleTagContent)
|
||||
paddingWidth := 10 // Target width for the level column (tag + padding)
|
||||
numSpaces := paddingWidth - visibleTagLen
|
||||
if numSpaces < 0 {
|
||||
numSpaces = 1 // Ensure at least one space
|
||||
}
|
||||
padding := strings.Repeat(" ", numSpaces)
|
||||
|
||||
var levelTagFormatted string
|
||||
if useSpecialFormatting {
|
||||
// ERROR/WARNING: Tag has specific background and foreground
|
||||
levelTagFormatted = fmt.Sprintf("%s%s%s%s", tagBgColor, tagFgColor, visibleTagContent, Reset)
|
||||
} else {
|
||||
// Other levels: Tag has standard foreground color only
|
||||
levelTagFormatted = fmt.Sprintf("%s%s%s", tagFgColor, visibleTagContent, Reset)
|
||||
}
|
||||
levelColumn := levelTagFormatted + padding // Combine formatted tag and padding
|
||||
|
||||
// --- User Prefix Formatting (part of message content for coloring purposes) ---
|
||||
userPrefixStr := ""
|
||||
if l.userPrefix != "" {
|
||||
// Use default foreground color for prefix (dark grey), reset locally
|
||||
prefixColor := levelColors[LevelPrefix]
|
||||
userPrefixStr = fmt.Sprintf("%s[%s]%s ", prefixColor, l.userPrefix, resetColor)
|
||||
// Format the string part here, colors applied later if needed
|
||||
userPrefixStr = fmt.Sprintf("[%s] ", l.userPrefix)
|
||||
}
|
||||
|
||||
// Build the prefix part (timestamp, caller, goroutine, level tag, user prefix)
|
||||
// These parts will use default terminal colors or their specifically set colors (like level tag)
|
||||
prefixPart := fmt.Sprintf("%s%s%s%s%s%s",
|
||||
l.prefix, timeStr, caller, goroutineStr, levelColumn, userPrefixStr)
|
||||
// --- Message Content ---
|
||||
messageContent := fmt.Sprintf("%s%s", msg, fields)
|
||||
|
||||
// Build the message part
|
||||
messagePart := fmt.Sprintf("%s%s", msg, fields)
|
||||
// --- Assemble Final String ---
|
||||
var finalMsg strings.Builder
|
||||
|
||||
// Combine prefix and message, applying background/foreground ONLY to the message part
|
||||
if useBgColor {
|
||||
// Apply background and specific foreground ONLY to the message part
|
||||
return fmt.Sprintf("%s%s%s%s%s", prefixPart, bgColor, msgFgColor, messagePart, resetColor)
|
||||
// Part 1: Timestamp, Caller, Goroutine ID (always default colors)
|
||||
finalMsg.WriteString(l.prefix)
|
||||
finalMsg.WriteString(timeStr)
|
||||
finalMsg.WriteString(caller)
|
||||
finalMsg.WriteString(goroutineStr)
|
||||
|
||||
// Part 2: Level Column (already formatted with tag colors and padding)
|
||||
finalMsg.WriteString(levelColumn)
|
||||
|
||||
// Part 3: User Prefix + Message Content (apply special formatting if needed)
|
||||
if useSpecialFormatting {
|
||||
// ERROR/WARNING: Apply message background and foreground to User Prefix + Message
|
||||
finalMsg.WriteString(messageBgColor)
|
||||
// finalMsg.WriteString(messageFgColor) // This doesn't work...? For some reason?
|
||||
finalMsg.WriteString(userPrefixStr) // Write user prefix inside the colored block
|
||||
finalMsg.WriteString(messageContent)
|
||||
finalMsg.WriteString(Reset)
|
||||
} else {
|
||||
// For non-ERROR/WARNING, just combine prefix (with its colored tag) and the uncolored message part.
|
||||
// No additional color codes needed here for the message itself.
|
||||
return fmt.Sprintf("%s%s", prefixPart, messagePart)
|
||||
// Other levels: User Prefix and Message content use default colors
|
||||
// Apply specific color to user prefix if it exists
|
||||
if l.userPrefix != "" {
|
||||
prefixColor := levelStyles[LevelPrefix].TagColor
|
||||
finalMsg.WriteString(fmt.Sprintf("%s%s%s", prefixColor, userPrefixStr, Reset))
|
||||
} // No else needed, if userPrefix is empty, userPrefixStr is "" anyway
|
||||
finalMsg.WriteString(messageContent) // Append message with default colors
|
||||
}
|
||||
|
||||
return finalMsg.String()
|
||||
}
|
||||
|
||||
// log logs a message at the specified level
|
||||
@@ -573,3 +618,43 @@ func ShowGoroutine() bool {
|
||||
}
|
||||
return Default.ShowGoroutine()
|
||||
}
|
||||
|
||||
func main() {
|
||||
Init(LevelDebug)
|
||||
|
||||
// Test basic logging
|
||||
Debug("This is a debug message")
|
||||
Info("This is an info message")
|
||||
Warning("This is a warning message")
|
||||
Error("This is an error message")
|
||||
|
||||
// Test logging with fields
|
||||
logger := WithField("user", "testuser")
|
||||
logger.Info("User logged in")
|
||||
|
||||
// Test logging with multiple fields
|
||||
fields := map[string]interface{}{
|
||||
"user": "testuser",
|
||||
"role": "admin",
|
||||
"id": 12345,
|
||||
}
|
||||
WithFields(fields).Info("User details")
|
||||
|
||||
// Test error logging with fields
|
||||
WithField("error", "connection failed").Error("Database error")
|
||||
|
||||
logger.WithPrefix("CUSTOM").Info("This is a message with a custom prefix")
|
||||
|
||||
// Test goroutine ID display
|
||||
SetShowGoroutine(true)
|
||||
Info("This message should show goroutine ID")
|
||||
|
||||
// Test different log levels
|
||||
SetLevel(LevelInfo)
|
||||
Debug("This debug message should not appear")
|
||||
Info("This info message should appear")
|
||||
|
||||
// Test with custom prefix
|
||||
WithField("prefix", "custom").Info("Message with custom prefix")
|
||||
Lua("This is a Lua message")
|
||||
}
|
||||
|
49
safe.go
Normal file
49
safe.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package cylogger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime/debug"
|
||||
)
|
||||
|
||||
// PanicHandler handles a panic and logs it
|
||||
func PanicHandler() {
|
||||
if r := recover(); r != nil {
|
||||
goroutineID := GetGoroutineID()
|
||||
stackTrace := debug.Stack()
|
||||
Error("PANIC in goroutine %s: %v\n%s", goroutineID, r, stackTrace)
|
||||
}
|
||||
}
|
||||
|
||||
// SafeGo launches a goroutine with panic recovery
|
||||
// Usage: logger.SafeGo(func() { ... your code ... })
|
||||
func SafeGo(f func()) {
|
||||
go func() {
|
||||
defer PanicHandler()
|
||||
f()
|
||||
}()
|
||||
}
|
||||
|
||||
// SafeGoWithArgs launches a goroutine with panic recovery and passes arguments
|
||||
// Usage: logger.SafeGoWithArgs(func(arg1, arg2 interface{}) { ... }, "value1", 42)
|
||||
func SafeGoWithArgs(f func(...interface{}), args ...interface{}) {
|
||||
go func() {
|
||||
defer PanicHandler()
|
||||
f(args...)
|
||||
}()
|
||||
}
|
||||
|
||||
// SafeExec executes a function with panic recovery
|
||||
// Useful for code that should not panic
|
||||
func SafeExec(f func()) (err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
goroutineID := GetGoroutineID()
|
||||
stackTrace := debug.Stack()
|
||||
Error("PANIC in goroutine %s: %v\n%s", goroutineID, r, stackTrace)
|
||||
err = fmt.Errorf("panic recovered: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
f()
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user