fix: add log rotation and version in logging (#76)

Signed-off-by: Nianyu Shen <xiaoyu9964@gmail.com>
This commit is contained in:
Nianyu Shen
2025-01-27 21:41:53 -08:00
committed by GitHub
parent ee39a2bd51
commit 15d74ed7f8
8 changed files with 54 additions and 5 deletions

View File

@@ -11,7 +11,7 @@ permissions:
jobs:
lint:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: docker-practice/actions-setup-docker@master

View File

@@ -37,6 +37,7 @@ BUILD_GOLANG:
COPY . ./
ARG BIN
ARG SRC
ENV GO_LDFLAGS=" -X github.com/kairos-io/provider-rke2/pkg/version.Version=${VERSION} -w -s"
IF $FIPS_ENABLED
RUN go-build-fips.sh -a -o ${BIN} ./${SRC}

3
go.mod
View File

@@ -1,4 +1,4 @@
module github.com/c3os-io/c3os/provider-rke2
module github.com/kairos-io/provider-rke2
go 1.23.1
@@ -7,6 +7,7 @@ require (
github.com/mudler/go-pluggable v0.0.0-20230126220627-7710299a0ae5
github.com/mudler/yip v1.10.0
github.com/sirupsen/logrus v1.9.3
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gopkg.in/yaml.v3 v3.0.1
sigs.k8s.io/yaml v1.4.0
)

2
go.sum
View File

@@ -125,6 +125,8 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

@@ -1,13 +1,16 @@
package main
import (
"github.com/c3os-io/c3os/provider-rke2/pkg/provider"
"github.com/kairos-io/kairos-sdk/clusterplugin"
"github.com/kairos-io/provider-rke2/pkg/log"
"github.com/kairos-io/provider-rke2/pkg/provider"
"github.com/mudler/go-pluggable"
"github.com/sirupsen/logrus"
)
func main() {
log.InitLogger("/var/log/provider-rke2.log")
plugin := clusterplugin.ClusterPlugin{
Provider: provider.ClusterProvider,
}

39
pkg/log/log.go Normal file
View File

@@ -0,0 +1,39 @@
package log
import (
"os"
"github.com/kairos-io/provider-rke2/pkg/version"
"github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
)
func InitLogger(path string) {
f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
logfile := &lumberjack.Logger{
Filename: f.Name(),
MaxSize: 10,
MaxBackups: 5,
Compress: true,
}
logrus.SetOutput(logfile)
logrus.SetFormatter(KubeadmLogger{
Version: version.Version,
Formatter: logrus.StandardLogger().Formatter,
})
}
type KubeadmLogger struct {
Version string
Formatter logrus.Formatter
}
func (l KubeadmLogger) Format(entry *logrus.Entry) ([]byte, error) {
entry.Data["version"] = l.Version
return l.Formatter.Format(entry)
}

View File

@@ -10,9 +10,9 @@ import (
_ "embed"
"github.com/c3os-io/c3os/provider-rke2/pkg/constants"
"github.com/c3os-io/c3os/provider-rke2/pkg/types"
"github.com/kairos-io/kairos-sdk/clusterplugin"
"github.com/kairos-io/provider-rke2/pkg/constants"
"github.com/kairos-io/provider-rke2/pkg/types"
yip "github.com/mudler/yip/pkg/schema"
"gopkg.in/yaml.v3"
kyaml "sigs.k8s.io/yaml"

3
pkg/version/version.go Normal file
View File

@@ -0,0 +1,3 @@
package version
var Version string