chore: improvement of logging
This commit is contained in:
@@ -191,7 +191,6 @@ func initConfig(configFile string) error {
|
|||||||
Type: AccessMiddleware,
|
Type: AccessMiddleware,
|
||||||
Paths: []string{
|
Paths: []string{
|
||||||
"/swagger-ui/*",
|
"/swagger-ui/*",
|
||||||
"/v2/swagger-ui/*",
|
|
||||||
"/api-docs/*",
|
"/api-docs/*",
|
||||||
"/actuator/*",
|
"/actuator/*",
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -26,47 +26,62 @@ import (
|
|||||||
"github.com/jkaninda/goma-gateway/util"
|
"github.com/jkaninda/goma-gateway/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Info returns info log
|
// Generic logging function
|
||||||
func Info(msg string, args ...interface{}) {
|
func logMessage(level, defaultOutput, msg string, args ...interface{}) {
|
||||||
log.SetOutput(getStd(util.GetStringEnv("GOMA_ACCESS_LOG", "/dev/stdout")))
|
logLevel := getLogLevel()
|
||||||
logWithCaller("INFO", msg, args...)
|
if shouldLog(level, logLevel) {
|
||||||
|
log.SetOutput(getStd(util.GetStringEnv("GOMA_ACCESS_LOG", defaultOutput)))
|
||||||
|
logWithCaller(level, msg, args...)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warn returns warning log
|
// Info logs informational messages
|
||||||
func Warn(msg string, args ...interface{}) {
|
func Info(msg string, args ...interface{}) {
|
||||||
log.SetOutput(getStd(util.GetStringEnv("GOMA_ACCESS_LOG", "/dev/stdout")))
|
logMessage("INFO", "/dev/stdout", msg, args...)
|
||||||
logWithCaller("WARN", msg, args...)
|
}
|
||||||
|
|
||||||
|
// Warn logs warning messages
|
||||||
|
func Warn(msg string, args ...interface{}) {
|
||||||
|
logMessage("WARN", "/dev/stdout", msg, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error logs error messages
|
// Error logs error messages
|
||||||
func Error(msg string, args ...interface{}) {
|
func Error(msg string, args ...interface{}) {
|
||||||
log.SetOutput(getStd(util.GetStringEnv("GOMA_ERROR_LOG", "/dev/stderr")))
|
logMessage("ERROR", "/dev/stderr", msg, args...)
|
||||||
logWithCaller("ERROR", msg, args...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fatal logs fatal errors and exits the program
|
||||||
func Fatal(msg string, args ...interface{}) {
|
func Fatal(msg string, args ...interface{}) {
|
||||||
log.SetOutput(os.Stdout)
|
log.SetOutput(os.Stdout)
|
||||||
logWithCaller("ERROR", msg, args...)
|
logWithCaller("ERROR", msg, args...)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Debug logs debug messages
|
||||||
func Debug(msg string, args ...interface{}) {
|
func Debug(msg string, args ...interface{}) {
|
||||||
log.SetOutput(getStd(util.GetStringEnv("GOMA_ACCESS_LOG", "/dev/stdout")))
|
logMessage("DEBUG", "/dev/stdout", msg, args...)
|
||||||
logLevel := util.GetStringEnv("GOMA_LOG_LEVEL", "")
|
|
||||||
if strings.ToLower(logLevel) == traceLog || strings.ToLower(logLevel) == "debug" {
|
|
||||||
logWithCaller("DEBUG", msg, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Trace logs trace messages
|
||||||
func Trace(msg string, args ...interface{}) {
|
func Trace(msg string, args ...interface{}) {
|
||||||
log.SetOutput(getStd(util.GetStringEnv("GOMA_ACCESS_LOG", "/dev/stdout")))
|
logMessage("TRACE", "/dev/stdout", msg, args...)
|
||||||
logLevel := util.GetStringEnv("GOMA_LOG_LEVEL", "")
|
}
|
||||||
if strings.ToLower(logLevel) == traceLog {
|
|
||||||
logWithCaller("DEBUG", msg, args...)
|
// Determines whether the message should be logged based on log level
|
||||||
|
func shouldLog(level, currentLevel string) bool {
|
||||||
|
levelOrder := map[string]int{
|
||||||
|
"trace": 1,
|
||||||
|
"debug": 2,
|
||||||
|
"info": 3,
|
||||||
|
"warn": 4,
|
||||||
|
"error": 5,
|
||||||
|
"off": 6,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
current := strings.ToLower(currentLevel)
|
||||||
|
target := strings.ToLower(level)
|
||||||
|
|
||||||
|
return levelOrder[target] >= levelOrder[current]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper function to format and log messages with file and line number
|
// Helper function to format and log messages with file and line number
|
||||||
@@ -83,17 +98,15 @@ func logWithCaller(level, msg string, args ...interface{}) {
|
|||||||
file = "unknown"
|
file = "unknown"
|
||||||
line = 0
|
line = 0
|
||||||
}
|
}
|
||||||
// Log message with caller information if GOMA_LOG_LEVEL is trace
|
|
||||||
logLevel := util.GetStringEnv("GOMA_LOG_LEVEL", "")
|
if getLogLevel() == traceLog {
|
||||||
if strings.ToLower(logLevel) != "off" {
|
log.Printf("%s: %s (File: %s, Line: %d)\n", level, formattedMessage, file, line)
|
||||||
if strings.ToLower(logLevel) == traceLog {
|
} else {
|
||||||
log.Printf("%s: %s (File: %s, Line: %d)\n", level, formattedMessage, file, line)
|
log.Printf("%s: %s\n", level, formattedMessage)
|
||||||
} else {
|
|
||||||
log.Printf("%s: %s\n", level, formattedMessage)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Determines the appropriate standard output based on the environment variable
|
||||||
func getStd(out string) *os.File {
|
func getStd(out string) *os.File {
|
||||||
switch out {
|
switch out {
|
||||||
case "/dev/stdout":
|
case "/dev/stdout":
|
||||||
@@ -104,6 +117,10 @@ func getStd(out string) *os.File {
|
|||||||
return os.Stdin
|
return os.Stdin
|
||||||
default:
|
default:
|
||||||
return os.Stdout
|
return os.Stdout
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Retrieves the current log level from environment variables
|
||||||
|
func getLogLevel() string {
|
||||||
|
return strings.ToLower(util.GetStringEnv("GOMA_LOG_LEVEL", ""))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user