initial commit
This commit is contained in:
commit
c7333f3762
5 changed files with 138 additions and 0 deletions
37
config/config.go
Normal file
37
config/config.go
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
logger "log"
|
||||||
|
|
||||||
|
"jonasled.dev/jonasled/go-libs/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetOrDefault(configName string, defaultVal string) string {
|
||||||
|
if os.Getenv(configName) != "" {
|
||||||
|
return os.Getenv(configName)
|
||||||
|
}
|
||||||
|
return defaultVal
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetOrError(configName string) (string, error) {
|
||||||
|
if os.Getenv(configName) != "" {
|
||||||
|
return os.Getenv(configName), nil
|
||||||
|
}
|
||||||
|
return "", errors.New("Config " + configName + " not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetOrFatal(configName string) string {
|
||||||
|
if os.Getenv(configName) != "" {
|
||||||
|
return os.Getenv(configName)
|
||||||
|
}
|
||||||
|
if log.Log != nil { // if logrus is already setup, use logrus
|
||||||
|
log.Log.Fatal("Required config key ", configName, " not found")
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
logger.Fatal("Required config key ", configName, " not found")
|
||||||
|
return ""
|
||||||
|
|
||||||
|
}
|
||||||
10
go.mod
Normal file
10
go.mod
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
module jonasled.dev/jonasled/go-libs
|
||||||
|
|
||||||
|
go 1.23.4
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/sirupsen/logrus v1.9.3
|
||||||
|
gopkg.in/natefinch/lumberjack.v2 v2.2.1
|
||||||
|
)
|
||||||
|
|
||||||
|
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
||||||
17
go.sum
Normal file
17
go.sum
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||||
|
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
|
||||||
|
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
|
||||||
|
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
42
log/log.go
Normal file
42
log/log.go
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
package log
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"gopkg.in/natefinch/lumberjack.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Log = logrus.New()
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
Log.SetOutput(os.Stdout)
|
||||||
|
Log.AddHook(&LumberjackHook{
|
||||||
|
Logger: &lumberjack.Logger{
|
||||||
|
Filename: os.Getenv("LOG_FOLDER") + "/application.log",
|
||||||
|
MaxSize: 10, // Max size in MB
|
||||||
|
MaxBackups: 3, // Max number of old log files to keep
|
||||||
|
MaxAge: 28, // Max age in days to keep a log file
|
||||||
|
Compress: true, // Compress old log files
|
||||||
|
}})
|
||||||
|
|
||||||
|
if os.Getenv("LOG_LEVEL") == "" {
|
||||||
|
Log.SetLevel(logrus.InfoLevel)
|
||||||
|
} else {
|
||||||
|
switch os.Getenv("LOG_LEVEL") {
|
||||||
|
case "ERROR":
|
||||||
|
Log.SetLevel(logrus.ErrorLevel)
|
||||||
|
case "WARN":
|
||||||
|
Log.SetLevel(logrus.WarnLevel)
|
||||||
|
case "INFO":
|
||||||
|
Log.SetLevel(logrus.InfoLevel)
|
||||||
|
case "DEBUG":
|
||||||
|
Log.SetLevel(logrus.DebugLevel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Log.SetFormatter(&logrus.TextFormatter{
|
||||||
|
FullTimestamp: true,
|
||||||
|
})
|
||||||
|
Log.Infof("Logger initialized with %s log level", Log.Level)
|
||||||
|
}
|
||||||
|
|
||||||
32
log/lumberjack-hook.go
Normal file
32
log/lumberjack-hook.go
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
package log
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"gopkg.in/natefinch/lumberjack.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type LumberjackHook struct {
|
||||||
|
Logger *lumberjack.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hook *LumberjackHook) Fire(entry *logrus.Entry) error {
|
||||||
|
|
||||||
|
// Write to the lumberjack.Logger (log file with rotation)
|
||||||
|
_, err := hook.Logger.Write(hook.Format(entry))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Implement the logrus.Hook interface method Levels
|
||||||
|
func (hook *LumberjackHook) Levels() []logrus.Level {
|
||||||
|
return logrus.AllLevels
|
||||||
|
}
|
||||||
|
func (hook *LumberjackHook) Format(entry *logrus.Entry) []byte {
|
||||||
|
// Format the timestamp with timezone offset
|
||||||
|
timestamp := entry.Time.Format("2006-01-02T15:04:05-07:00")
|
||||||
|
|
||||||
|
// Format the log message
|
||||||
|
logMessage := fmt.Sprintf("%s[%s] %s\n", entry.Level.String(), timestamp, entry.Message)
|
||||||
|
return []byte(logMessage)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue