Skip to content

Commit

Permalink
Merge pull request #5 from FlorianRuen/fix-logs-format-for-logstash
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-tacquet authored Sep 15, 2022
2 parents d0f7f34 + 09d291b commit 3758ad9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 72 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ You can also add customization options :
gormv2logrus.WithLogrus(e),
gormv2logrus.WithGormOptions(
gormv2logrus.GormOptions{
Colorful: true,
SlowThreshold: slowThresholdDuration,
LogLevel: logger.Error,
LogLatency: true,
Expand All @@ -53,6 +52,8 @@ You can also add customization options :
)
```

> Colorful option has been removed, because Logrus can't really handle the color code, especially if the logs are redirected to a file, the characters are not removed automatically
## Contibuting

Just feel free to open issues, ask questions, make proposals.
66 changes: 15 additions & 51 deletions gormlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ func (gl *Gormlog) Trace(ctx context.Context, begin time.Time, fc func() (string
logrusFields["duration"] = stopWatch
}

// add number of affected rows as logrus parameter
logrusFields["rows"] = rows

// if source field is definied, we retrieve line number information
if len(gl.SourceField) > 0 {
logrusFields[gl.SourceField] = utils.FileWithLineNum()
Expand All @@ -124,80 +127,41 @@ func (gl *Gormlog) Trace(ctx context.Context, begin time.Time, fc func() (string
logrusFields[logrus.ErrorKey] = err

if gl.opts.lr != nil {
if gl.opts.Colorful {
gl.opts.lr.WithContext(ctx).WithFields(logrusFields).Errorf(
Magenta+"%s\n"+Reset+Red+"[error] "+"[%.3fms] ", traceLog,
float64(stopWatch.Nanoseconds())/1e6)
} else {
gl.opts.lr.WithContext(ctx).WithFields(logrusFields).Errorf(
"%s\n [error] [%.3fms] ", traceLog,
float64(stopWatch.Nanoseconds())/1e6)
}
gl.opts.lr.WithContext(ctx).WithFields(logrusFields).Errorf("%s", traceLog)
}

if gl.opts.logrusEntry != nil {
if gl.opts.Colorful {
gl.opts.logrusEntry.WithContext(ctx).WithFields(logrusFields).Errorf(
Magenta+"%s\n"+Reset+Red+"[error] "+"[%.3fms] ", traceLog,
float64(stopWatch.Nanoseconds())/1e6)
} else {
gl.opts.logrusEntry.WithContext(ctx).WithFields(logrusFields).Errorf(
"%s\n [error] [%.3fms] ", traceLog,
float64(stopWatch.Nanoseconds())/1e6)
}
gl.opts.logrusEntry.WithContext(ctx).WithFields(logrusFields).Errorf("%s", traceLog)
}

return
}
}

if gl.SlowThreshold != 0 && stopWatch > gl.SlowThreshold {
if gl.opts.SlowThreshold != 0 && stopWatch > gl.opts.SlowThreshold && gl.opts.LogLevel >= logger.Warn {

// instead of adding SLOW SQL to the message, add reason field
// this can be parsed easily with logs management tools
logrusFields["reason"] = "SLOW SQL"

if gl.opts.lr != nil {
if gl.opts.Colorful {
gl.opts.lr.WithContext(ctx).WithFields(logrusFields).Warnf(
Green+"SLOW SQL %s\n"+Reset+RedBold+"[%.3fms] ", traceLog,
float64(stopWatch.Nanoseconds())/1e6)
} else {
gl.opts.lr.WithContext(ctx).WithFields(logrusFields).Warnf(
"SLOW SQL %s\n [%.3fms]", traceLog, float64(stopWatch.Nanoseconds())/1e6)
}
gl.opts.lr.WithContext(ctx).WithFields(logrusFields).Warnf("%s", traceLog)
}

if gl.opts.logrusEntry != nil {
if gl.opts.Colorful {
gl.opts.logrusEntry.WithContext(ctx).WithFields(logrusFields).Warnf(
Green+"SLOW SQL %s\n"+Reset+RedBold+"[%.3fms] ", traceLog,
float64(stopWatch.Nanoseconds())/1e6)
} else {
gl.opts.logrusEntry.WithContext(ctx).WithFields(logrusFields).Warnf(
"SLOW SQL %s\n [%.3fms]", traceLog, float64(stopWatch.Nanoseconds())/1e6)
}
gl.opts.logrusEntry.WithContext(ctx).WithFields(logrusFields).Warnf("%s", traceLog)
}

return
}

// Use directly with logrus entry
if gl.opts.lr != nil {
if gl.opts.Colorful {
gl.opts.lr.WithContext(ctx).WithFields(logrusFields).Debugf(
Green+"%s\n"+Reset+Yellow+"[%.3fms] "+BlueBold+"[rows:%v]"+Reset, traceLog,
float64(stopWatch.Nanoseconds())/1e6, rows)
} else {
gl.opts.lr.WithContext(ctx).WithFields(logrusFields).Debugf(
"%s\n [%.3fms] [rows:%v]", traceLog, float64(stopWatch.Nanoseconds())/1e6, rows)
}
gl.opts.lr.WithContext(ctx).WithFields(logrusFields).Debugf("%s", traceLog)
}

// Use with logrusEntry
if gl.opts.logrusEntry != nil {
if gl.opts.Colorful {
gl.opts.logrusEntry.WithContext(ctx).WithFields(logrusFields).Debugf(
Green+"%s\n"+Reset+Yellow+"[%.3fms] "+BlueBold+"[rows:%v]"+Reset, traceLog,
float64(stopWatch.Nanoseconds())/1e6, rows)
} else {
gl.opts.logrusEntry.WithContext(ctx).WithFields(logrusFields).Debugf(
"%s\n [%.3fms] [rows:%v]", traceLog, float64(stopWatch.Nanoseconds())/1e6, rows)
}
gl.opts.logrusEntry.WithContext(ctx).WithFields(logrusFields).Debugf("%s", traceLog)
}
}
20 changes: 0 additions & 20 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ type options struct {

// if set to true, it will add latency informations for your queries
logLatency bool

Colorful bool
}

// BannedKeyword represents a rule for scanning for Keyword in log output.
Expand All @@ -49,28 +47,11 @@ type BannedKeyword struct {
CaseMatters bool
}

// Colors for Colorful option
const (
Reset = "\033[0m"
Red = "\033[31m"
Green = "\033[32m"
Yellow = "\033[33m"
Blue = "\033[34m"
Magenta = "\033[35m"
Cyan = "\033[36m"
White = "\033[37m"
BlueBold = "\033[34;1m"
MagentaBold = "\033[35;1m"
RedBold = "\033[31;1m"
YellowBold = "\033[33;1m"
)

type GormOptions struct {
SlowThreshold time.Duration
LogLevel logger.LogLevel
TruncateLen uint
LogLatency bool
Colorful bool
}

func defaultOptions() options {
Expand Down Expand Up @@ -130,7 +111,6 @@ func WithBannedKeyword(bannedKeywords []BannedKeyword) Option {

func WithGormOptions(gormOpt GormOptions) Option {
return newGormLogOption(func(o *options) {
o.Colorful = gormOpt.Colorful
o.logLatency = gormOpt.LogLatency
o.LogLevel = gormOpt.LogLevel
o.SlowThreshold = gormOpt.SlowThreshold
Expand Down

0 comments on commit 3758ad9

Please sign in to comment.