logs-fix-4
This commit is contained in:
parent
c53ada95eb
commit
bdcf2c39f7
|
@ -1,60 +0,0 @@
|
||||||
package logs
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
// LogJSONFormatter is a custom log formatter for JSON output
|
|
||||||
type LogJSONFormatter struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
// Format formats the log entry to JSON
|
|
||||||
func (f *LogJSONFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
|
||||||
data := make(logrus.Fields)
|
|
||||||
|
|
||||||
// Include the log level, message, and time in the JSON output
|
|
||||||
data["level"] = entry.Level.String()
|
|
||||||
data["msg"] = entry.Message
|
|
||||||
data["time"] = entry.Time.UTC().Format(time.RFC3339)
|
|
||||||
|
|
||||||
// Include additional fields from the entry
|
|
||||||
for k, v := range entry.Data {
|
|
||||||
data[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
// Marshal the JSON data
|
|
||||||
jsonData, err := json.Marshal(data)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to marshal log entry to JSON: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Append a newline to the JSON data
|
|
||||||
return append(jsonData, '\n'), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParseLogLine parses the log line and extracts the log level, message, and time
|
|
||||||
func ParseLogLine(line string) (logrus.Level, string, time.Time, error) {
|
|
||||||
var entry struct {
|
|
||||||
Level string `json:"level"`
|
|
||||||
Msg string `json:"msg"`
|
|
||||||
Time time.Time `json:"time"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unmarshal the JSON data
|
|
||||||
err := json.Unmarshal([]byte(line), &entry)
|
|
||||||
if err != nil {
|
|
||||||
return logrus.InfoLevel, "", time.Time{}, fmt.Errorf("failed to unmarshal log line: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert the log level string to logrus.Level
|
|
||||||
level, err := logrus.ParseLevel(entry.Level)
|
|
||||||
if err != nil {
|
|
||||||
return logrus.InfoLevel, "", time.Time{}, fmt.Errorf("failed to parse log level: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return level, entry.Msg, entry.Time, nil
|
|
||||||
}
|
|
|
@ -7,22 +7,11 @@ import (
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LogUTCFormatter hels in setting UTC time format for the logs
|
|
||||||
type LogUTCFormatter struct {
|
|
||||||
log.Formatter
|
|
||||||
}
|
|
||||||
|
|
||||||
// Format helps fomratting time to UTC
|
|
||||||
func (u LogUTCFormatter) Format(e *log.Entry) ([]byte, error) {
|
|
||||||
e.Time = e.Time.UTC()
|
|
||||||
return u.Formatter.Format(e)
|
|
||||||
}
|
|
||||||
|
|
||||||
func InitLog(cliLogLevel string) *log.Logger {
|
func InitLog(cliLogLevel string) *log.Logger {
|
||||||
|
|
||||||
// log instance for gin server
|
// log instance for gin server
|
||||||
log := logrus.New()
|
log := logrus.New()
|
||||||
log.SetFormatter(&LogJSONFormatter{})
|
log.SetFormatter(&LogTextFormatter{})
|
||||||
|
|
||||||
if cliLogLevel == "" {
|
if cliLogLevel == "" {
|
||||||
cliLogLevel = os.Getenv("LOG_LEVEL")
|
cliLogLevel = os.Getenv("LOG_LEVEL")
|
||||||
|
|
18
server/logs/text.go
Normal file
18
server/logs/text.go
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
package logs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// LogTextFormatter is a custom log formatter for text output
|
||||||
|
type LogTextFormatter struct {
|
||||||
|
logrus.Formatter
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format helps fomratting time to UTC
|
||||||
|
func (u LogTextFormatter) Format(e *logrus.Entry) ([]byte, error) {
|
||||||
|
return []byte(fmt.Sprintf("[%s] %s", strings.ToUpper(e.Level.String()), e.Message)), nil
|
||||||
|
}
|
16
server/logs/utc.go
Normal file
16
server/logs/utc.go
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
package logs
|
||||||
|
|
||||||
|
import (
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// LogUTCFormatter hels in setting UTC time format for the logs
|
||||||
|
type LogUTCFormatter struct {
|
||||||
|
log.Formatter
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format helps fomratting time to UTC
|
||||||
|
func (u LogUTCFormatter) Format(e *log.Entry) ([]byte, error) {
|
||||||
|
e.Time = e.Time.UTC()
|
||||||
|
return u.Formatter.Format(e)
|
||||||
|
}
|
|
@ -28,7 +28,7 @@ func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// global log level
|
// global log level
|
||||||
logrus.SetFormatter(&logs.LogJSONFormatter{})
|
logrus.SetFormatter(&logs.LogTextFormatter{})
|
||||||
|
|
||||||
constants.VERSION = VERSION
|
constants.VERSION = VERSION
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user