Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch(pkg/logger): use local system timezone #328

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@
package logger

import (
"time"

"github.com/sirupsen/logrus"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
"gopkg.in/natefinch/lumberjack.v2"
)

type TZFormatter struct {
logrus.Formatter
}

func (u TZFormatter) Format(e *logrus.Entry) ([]byte, error) {
e.Time = time.Now()
return u.Formatter.Format(e)
}

func NewLogger(logLevel string, disableTimestamp bool, logFileOpt *lumberjack.Logger) *logrus.Logger {
log := logrus.New()

Expand All @@ -20,11 +31,11 @@ func NewLogger(logLevel string, disableTimestamp bool, logFileOpt *lumberjack.Lo
}

log.SetLevel(level)
log.SetFormatter(&prefixed.TextFormatter{
log.SetFormatter(TZFormatter{&prefixed.TextFormatter{
DisableTimestamp: disableTimestamp,
FullTimestamp: true,
TimestampFormat: "Jan 02 15:04:05",
})
}})
if logFileOpt != nil {
log.SetOutput(logFileOpt)
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/logger/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package logger

import (
"testing"

"gopkg.in/natefinch/lumberjack.v2"
)

func TestLogger(t *testing.T) {
var logOpts *lumberjack.Logger
log := NewLogger("debug", false, logOpts)
log.Info("Hi there!")
}
Loading