add logging to the operator
Some checks failed
Build images / Run e2e tests (push) Failing after 1m57s
Build images / Run lint test (push) Failing after 7m27s
Lint / Run on Ubuntu (push) Failing after 6m47s
Build images / Run unit test (push) Failing after 8m53s
Build images / Build docker image (push) Has been skipped
E2E Tests / Run on Ubuntu (push) Failing after 1m50s
Tests / Run on Ubuntu (push) Failing after 3m1s
Some checks failed
Build images / Run e2e tests (push) Failing after 1m57s
Build images / Run lint test (push) Failing after 7m27s
Lint / Run on Ubuntu (push) Failing after 6m47s
Build images / Run unit test (push) Failing after 8m53s
Build images / Build docker image (push) Has been skipped
E2E Tests / Run on Ubuntu (push) Failing after 1m50s
Tests / Run on Ubuntu (push) Failing after 3m1s
add logging
This commit is contained in:
43
cmd/logging.go
Normal file
43
cmd/logging.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/go-logr/zapr"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
)
|
||||
|
||||
// CreateFileLogger Create a logger that will write to console, and file along with log rotation
|
||||
func CreateFileLogger(fileName string, logLevel zapcore.LevelEnabler) logr.Logger {
|
||||
|
||||
// Create file with desired permissions
|
||||
file, err := os.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
config := zap.NewProductionEncoderConfig()
|
||||
config.EncodeTime = zapcore.ISO8601TimeEncoder
|
||||
fileEncoder := zapcore.NewJSONEncoder(config)
|
||||
consoleEncoder := zapcore.NewConsoleEncoder(config)
|
||||
writer := zapcore.AddSync(&lumberjack.Logger{
|
||||
Filename: fileName,
|
||||
MaxSize: 1000, //MB
|
||||
MaxBackups: 3,
|
||||
MaxAge: 90, //days
|
||||
Compress: false,
|
||||
})
|
||||
|
||||
core := zapcore.NewTee(
|
||||
zapcore.NewCore(fileEncoder, writer, logLevel),
|
||||
zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), logLevel),
|
||||
)
|
||||
|
||||
logger := zap.New(core, zap.AddCaller(), zap.AddStacktrace(zapcore.ErrorLevel))
|
||||
return zapr.NewLogger(logger)
|
||||
}
|
||||
Reference in New Issue
Block a user