log to file
This commit is contained in:
parent
59496affaa
commit
b6d7af3c8c
6 changed files with 48 additions and 1 deletions
|
|
@ -1,4 +1,5 @@
|
|||
LOG_LEVEL=DEBUG
|
||||
LOG_FOLDER="./logs"
|
||||
|
||||
UPDATE_CHECK=true
|
||||
UPDATE_INSTALL=true
|
||||
|
|
|
|||
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -1,3 +1,4 @@
|
|||
.env
|
||||
database.db
|
||||
fms32-pro-mqtt
|
||||
fms32-pro-mqtt
|
||||
logs/
|
||||
1
go.mod
1
go.mod
|
|
@ -10,6 +10,7 @@ require (
|
|||
github.com/nekomeowww/gorm-logger-logrus v1.0.8
|
||||
github.com/sirupsen/logrus v1.9.3
|
||||
github.com/wind-c/comqtt/v2 v2.6.0
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1
|
||||
gorm.io/gorm v1.25.12
|
||||
)
|
||||
|
||||
|
|
|
|||
2
go.sum
2
go.sum
|
|
@ -56,6 +56,8 @@ golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
|
|||
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
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/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
|
|
|||
|
|
@ -4,12 +4,22 @@ 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 {
|
||||
|
|
|
|||
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