Skip to content
This repository has been archived by the owner on Jan 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #40 from icon-project/IS-940-modify-log-format
Browse files Browse the repository at this point in the history
IS-940 modify log format
  • Loading branch information
eunsoo-icon authored Dec 6, 2019
2 parents 2ea4faf + 94d93a6 commit 00adb7a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
11 changes: 2 additions & 9 deletions cmd/icon_rc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"log"
"os"

"github.com/icon-project/rewardcalculator/common"
"github.com/icon-project/rewardcalculator/core"
"github.com/natefinch/lumberjack"
)

var (
Expand Down Expand Up @@ -36,14 +36,7 @@ func main() {
flag.BoolVar(&optVersion, "version", false, "Print version information")
flag.Parse()

log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
log.SetOutput(&lumberjack.Logger{
Filename: cfg.LogFile,
MaxSize: cfg.LogMaxSize,
MaxBackups: cfg.LogMaxBackups,
LocalTime: true,
})

common.SetLog(cfg.LogFile, cfg.LogMaxSize, cfg.LogMaxBackups, true)

if optVersion {
fmt.Printf("icon_rc %s, %s\n", version, build)
Expand Down
40 changes: 40 additions & 0 deletions common/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package common

import (
"log"
"time"

"github.com/natefinch/lumberjack"
)

type Log struct {
logger lumberjack.Logger
}

const (
timestampFormat = "2006-01-02 15:04:05.000"
timestampMilliSecIndex = len(timestampFormat) - 4
)

func (l *Log) Write(p []byte) (n int, err error) {
buf := make([]byte, 0)
timestamp := time.Now().Format(timestampFormat) + " "
buf = append(buf, timestamp...)
buf = append(buf, p...)
buf[timestampMilliSecIndex] = ','
return l.logger.Write(buf)
}

func SetLog(file string, maxSize int, maxBackups int, localtime bool) {
log.SetFlags(log.Lshortfile)
rcLog := Log{
lumberjack.Logger{
Filename: file,
MaxSize: maxSize,
MaxBackups: maxBackups,
LocalTime: localtime,
},
}
log.SetOutput(&rcLog)
}

0 comments on commit 00adb7a

Please sign in to comment.