This commit is contained in:
Untone 2024-01-22 14:29:32 +03:00
parent 69a87896e9
commit c8413665ae

View File

@ -1,28 +1,31 @@
package logs
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
)
// LogUTCFormatter hels in setting UTC time format for the logs
// LogUTCFormatter helps in setting UTC time format for the logs
type LogUTCFormatter struct {
log.Formatter
}
// Format helps fomratting time to UTC
// Format helps formatting time to UTC
func (u LogUTCFormatter) Format(e *log.Entry) ([]byte, error) {
e.Time = e.Time.UTC()
return u.Formatter.Format(e)
file := filepath.Base(e.Caller.File)
return []byte(fmt.Sprintf("[%s] %s:%d %s\n", e.Time.Format(time.RFC3339), file, e.Caller.Line, e.Message)), nil
}
func InitLog(cliLogLevel string) *log.Logger {
// log instance for gin server
// log instance for the gin server
log := logrus.New()
log.SetFormatter(LogUTCFormatter{&logrus.JSONFormatter{}})
log.SetFormatter(LogUTCFormatter{&logrus.TextFormatter{}})
if cliLogLevel == "" {
cliLogLevel = os.Getenv("LOG_LEVEL")