Files
goma-gateway/pkg/logger/logger.go

127 lines
3.1 KiB
Go
Raw Normal View History

2024-10-27 06:10:27 +01:00
package logger
/*
/*
Copyright 2024 Jonas Kaninda
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"fmt"
"log"
"os"
"runtime"
2024-11-12 13:11:38 +01:00
"strings"
"github.com/jkaninda/goma-gateway/util"
2024-10-27 06:10:27 +01:00
)
2024-12-09 13:04:06 +01:00
// Generic logging function
func logMessage(level, defaultOutput, msg string, args ...interface{}) {
logLevel := getLogLevel()
if shouldLog(level, logLevel) {
log.SetOutput(getStd(util.GetStringEnv("GOMA_ACCESS_LOG", defaultOutput)))
logWithCaller(level, msg, args...)
}
}
2024-12-09 13:04:06 +01:00
// Info logs informational messages
func Info(msg string, args ...interface{}) {
logMessage("INFO", "/dev/stdout", msg, args...)
2024-10-27 06:10:27 +01:00
}
2024-12-09 13:04:06 +01:00
// Warn logs warning messages
2024-10-27 06:10:27 +01:00
func Warn(msg string, args ...interface{}) {
2024-12-09 13:04:06 +01:00
logMessage("WARN", "/dev/stdout", msg, args...)
2024-10-27 06:10:27 +01:00
}
// Error logs error messages
2024-10-27 06:10:27 +01:00
func Error(msg string, args ...interface{}) {
2024-12-09 13:04:06 +01:00
logMessage("ERROR", "/dev/stderr", msg, args...)
2024-10-27 06:10:27 +01:00
}
2024-12-09 13:04:06 +01:00
// Fatal logs fatal errors and exits the program
2024-10-27 06:10:27 +01:00
func Fatal(msg string, args ...interface{}) {
log.SetOutput(os.Stdout)
logWithCaller("ERROR", msg, args...)
2024-10-27 06:10:27 +01:00
os.Exit(1)
}
2024-12-09 13:04:06 +01:00
// Debug logs debug messages
2024-10-27 06:10:27 +01:00
func Debug(msg string, args ...interface{}) {
2024-12-09 13:04:06 +01:00
logMessage("DEBUG", "/dev/stdout", msg, args...)
}
2024-12-09 13:04:06 +01:00
// Trace logs trace messages
func Trace(msg string, args ...interface{}) {
2024-12-09 13:04:06 +01:00
logMessage("TRACE", "/dev/stdout", 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,
}
2024-12-09 13:04:06 +01:00
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
func logWithCaller(level, msg string, args ...interface{}) {
// Format message if there are additional arguments
formattedMessage := msg
if len(args) > 0 {
formattedMessage = fmt.Sprintf(msg, args...)
}
// Get the caller's file and line number (skip 2 frames)
_, file, line, ok := runtime.Caller(2)
if !ok {
file = "unknown"
line = 0
}
2024-12-09 13:04:06 +01:00
if getLogLevel() == traceLog {
log.Printf("%s: %s (File: %s, Line: %d)\n", level, formattedMessage, file, line)
} else {
log.Printf("%s: %s\n", level, formattedMessage)
2024-10-27 06:10:27 +01:00
}
}
2024-12-09 13:04:06 +01:00
// Determines the appropriate standard output based on the environment variable
2024-10-27 06:10:27 +01:00
func getStd(out string) *os.File {
switch out {
case "/dev/stdout":
return os.Stdout
case "/dev/stderr":
return os.Stderr
case "/dev/stdin":
return os.Stdin
default:
return os.Stdout
}
}
2024-12-09 13:04:06 +01:00
// Retrieves the current log level from environment variables
func getLogLevel() string {
return strings.ToLower(util.GetStringEnv("GOMA_LOG_LEVEL", ""))
}