Skip to content

Commit 542c529

Browse files
authored
Merge pull request #41 from covalenthq/filelog
using lumberjack logger for file logging (with rotation)
2 parents 4767a1e + 50b6685 commit 542c529

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

cmd/bspagent/main.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
log "github.com/sirupsen/logrus"
2323
"github.com/ubiq/go-ubiq/rlp"
2424
"gopkg.in/avro.v0"
25+
"gopkg.in/natefinch/lumberjack.v2"
2526

2627
"github.com/covalenthq/bsp-agent/internal/config"
2728
"github.com/covalenthq/bsp-agent/internal/event"
@@ -65,7 +66,14 @@ func init() {
6566
}}
6667
formatter.Line = true
6768
log.SetFormatter(&formatter)
68-
log.SetOutput(os.Stdout)
69+
bspLoggerOutput := utils.NewLoggerOut(os.Stdout, &lumberjack.Logger{
70+
// logs folder created/searched in directory in which agent was started.
71+
Filename: "./logs/log.log",
72+
MaxSize: 100, // megabytes
73+
MaxBackups: 7,
74+
MaxAge: 10, // days
75+
})
76+
log.SetOutput(&bspLoggerOutput)
6977
log.SetLevel(log.InfoLevel)
7078
log.WithFields(log.Fields{"file": "main.go"}).Info("bsp-agent is running...")
7179
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ require (
2222
github.com/ubiq/go-ubiq v3.0.1+incompatible
2323
google.golang.org/api v0.70.0
2424
gopkg.in/avro.v0 v0.0.0-20171217001914-a730b5802183
25+
gopkg.in/natefinch/lumberjack.v2 v2.0.0
2526
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
2627
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,8 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN
979979
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
980980
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
981981
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
982+
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
983+
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
982984
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce h1:+JknDZhAj8YMt7GC73Ei8pv4MzjDUNPHgQWJdtMAaDU=
983985
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce/go.mod h1:5AcXVHNjg+BDxry382+8OKon8SEWiKktQR07RKPsv1c=
984986
gopkg.in/olebedev/go-duktape.v3 v3.0.0-20200619000410-60c24ae608a6/go.mod h1:uAJfkITjFhyEEuUfm7bsmCZRbW5WRq8s9EY8HZ6hCns=

internal/utils/bsploggerout.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"io"
6+
)
7+
8+
// BsploggerOutput logger that can log to file (lumberjack) as well as stdout
9+
type BsploggerOutput struct {
10+
writers []io.Writer
11+
}
12+
13+
// NewLoggerOut combine all writers to create a new writer
14+
func NewLoggerOut(writers ...io.Writer) BsploggerOutput {
15+
return BsploggerOutput{writers: writers}
16+
}
17+
18+
func (logger *BsploggerOutput) Write(p []byte) (n int, err error) {
19+
var lastn int
20+
var lasterr error
21+
for _, writer := range logger.writers {
22+
lastn, lasterr = writer.Write(p)
23+
}
24+
25+
if lasterr != nil {
26+
return lastn, fmt.Errorf("error from last log writer: %w", lasterr)
27+
}
28+
29+
return lastn, nil
30+
}

0 commit comments

Comments
 (0)