@@ -1,10 +1,11 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ type server struct {
|
|||||||
Address string `yaml:"address"`
|
Address string `yaml:"address"`
|
||||||
ExternalUrl string `yaml:"external_url"`
|
ExternalUrl string `yaml:"external_url"`
|
||||||
DebugMode string `yaml:"debug_mode"`
|
DebugMode string `yaml:"debug_mode"`
|
||||||
FileLogger string `yaml:"file_logger"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type database struct {
|
type database struct {
|
||||||
|
|||||||
@@ -1,36 +1,46 @@
|
|||||||
package logger
|
package logger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
log "github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/viper"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
FileLogger := viper.GetBool("server.file_logger")
|
levelStr := os.Getenv("LOG_LEVEL")
|
||||||
DebugMode := viper.GetBool("server.debug_mode")
|
level, err := logrus.ParseLevel(levelStr)
|
||||||
|
|
||||||
if FileLogger == true {
|
|
||||||
gin.DisableConsoleColor()
|
|
||||||
file, err := os.OpenFile("app.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicln("Error opening log file: ", err)
|
level = logrus.InfoLevel
|
||||||
}
|
|
||||||
log.SetFormatter(&log.JSONFormatter{})
|
|
||||||
log.SetOutput(file)
|
|
||||||
log.SetLevel(log.DebugLevel)
|
|
||||||
} else {
|
|
||||||
log.SetFormatter(&log.JSONFormatter{})
|
|
||||||
log.SetOutput(os.Stdout)
|
|
||||||
log.SetLevel(log.WarnLevel)
|
|
||||||
}
|
}
|
||||||
|
logrus.SetLevel(level)
|
||||||
|
|
||||||
if DebugMode == true {
|
if level == logrus.DebugLevel {
|
||||||
gin.SetMode(gin.DebugMode)
|
gin.SetMode(gin.DebugMode)
|
||||||
log.SetLevel(log.DebugLevel)
|
|
||||||
} else {
|
} else {
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
|
gin.DisableConsoleColor()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logrus.SetFormatter(&logrus.JSONFormatter{
|
||||||
|
TimestampFormat: "2006-01-02 15:04:05",
|
||||||
|
})
|
||||||
|
|
||||||
|
logrus.SetReportCaller(true)
|
||||||
|
|
||||||
|
file, err := os.OpenFile("app.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
|
||||||
|
if err != nil {
|
||||||
|
logrus.SetOutput(os.Stdout)
|
||||||
|
logrus.Errorf("Error to create log file: %v", err)
|
||||||
|
} else {
|
||||||
|
logrus.SetOutput(io.MultiWriter(os.Stdout, file))
|
||||||
|
}
|
||||||
|
|
||||||
|
logrus.RegisterExitHandler(func() {
|
||||||
|
if file != nil {
|
||||||
|
file.Sync()
|
||||||
|
file.Close()
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -8,8 +8,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
config.Init()
|
|
||||||
logger.Init()
|
logger.Init()
|
||||||
|
config.Init()
|
||||||
data.Init()
|
data.Init()
|
||||||
server.Start()
|
server.Start()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,9 +7,10 @@ import (
|
|||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GinLogger(log *logrus.Logger) gin.HandlerFunc {
|
func GinLogger() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
var body []byte
|
var body []byte
|
||||||
if c.Request.Body != nil {
|
if c.Request.Body != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user