Skip to content

Commit

Permalink
Feat: 使用 slog 优化日志 TencentBlueKing#41 (TencentBlueKing#42)
Browse files Browse the repository at this point in the history
* Feat: 使用 slog 优化日志 TencentBlueKing#41

* Feat: 使用 slog 优化日志 TencentBlueKing#41

* Feat: 使用 slog 优化日志 TencentBlueKing#41

* Feat: 使用 slog 优化日志 TencentBlueKing#41

* Feat: 使用 slog 优化日志 TencentBlueKing#41

* Feat: 使用 slog 优化日志 TencentBlueKing#41

* Feat: 使用 slog 优化日志 TencentBlueKing#41

* Feat: 使用 slog 优化日志 TencentBlueKing#41

* Feat: 使用 slog 优化日志 TencentBlueKing#41

* Feat: 使用 slog 优化日志 TencentBlueKing#41

* Feat: 使用 slog 优化日志 TencentBlueKing#41

* Feat: 使用 slog 优化日志 TencentBlueKing#41

* Feat: 使用 slog 优化日志 TencentBlueKing#41

* Feat: 使用 slog 优化日志 TencentBlueKing#41

* Feat: 使用 slog 优化日志 TencentBlueKing#41

* Feat: 使用 slog 优化日志 TencentBlueKing#41

* Feat: 使用 slog 优化日志 TencentBlueKing#41

* Feat: 使用 slog 优化日志 TencentBlueKing#41

* Feat: 使用 slog 优化日志 TencentBlueKing#41
  • Loading branch information
ifooth committed Dec 13, 2023
1 parent b97d706 commit 3f4ce2f
Show file tree
Hide file tree
Showing 27 changed files with 412 additions and 192 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ build_local.sh
/bscp.exe
/dev*
cover.out
.envrc
.envrc
.vscode
33 changes: 19 additions & 14 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@
package cache

import (
"encoding/json"
"fmt"
"io"
"os"
"path"

"bscp.io/pkg/logs"
"bscp.io/pkg/runtime/jsoni"
sfs "bscp.io/pkg/sf-share"
"bscp.io/pkg/tools"
"golang.org/x/exp/slog"

"github.com/TencentBlueKing/bscp-go/downloader"
"github.com/TencentBlueKing/bscp-go/logger"
)

var defaultCachePath = "/tmp/bk-bscp"
Expand Down Expand Up @@ -58,20 +59,21 @@ func GetCache() *Cache {
// OnReleaseChange is the callback to refresh cache when release change event was received.
func (c *Cache) OnReleaseChange(event *sfs.ReleaseChangeEvent) {
pl := new(sfs.ReleaseChangePayload)
if err := jsoni.Unmarshal(event.Payload, pl); err != nil {
logs.Errorf("decode release change event payload failed, skip the event, err: %s, rid: %s", err.Error(), event.Rid)
if err := json.Unmarshal(event.Payload, pl); err != nil {
logger.Error("decode release change event payload failed, skip the event",
logger.ErrAttr(err), slog.String("rid", event.Rid))
return
}

if err := os.MkdirAll(c.path, os.ModePerm); err != nil {
logs.Errorf("mkdir cache path %s failed, err: %s", c.path, err.Error())
logger.Error("mkdir cache path failed", slog.String("path", c.path), logger.ErrAttr(err))
return
}

for _, ci := range pl.ReleaseMeta.CIMetas {
exists, err := c.checkFileCacheExists(ci)
if err != nil {
logs.Errorf("check config item exists failed, err: %s, rid: %s", err.Error(), event.Rid)
logger.Error("check config item exists failed", logger.ErrAttr(err), slog.String("rid", event.Rid))
continue
}
if exists {
Expand All @@ -80,7 +82,7 @@ func (c *Cache) OnReleaseChange(event *sfs.ReleaseChangeEvent) {
filePath := path.Join(c.path, ci.ContentSpec.Signature)
if err := downloader.GetDownloader().Download(ci.PbFileMeta(), ci.RepositoryPath, ci.ContentSpec.ByteSize,
downloader.DownloadToFile, nil, filePath); err != nil {
logs.Errorf("download file failed, err: %s, rid: %s", err.Error(), event.Rid)
logger.Error("download file failed", logger.ErrAttr(err), slog.String("rid", event.Rid))
return
}
}
Expand Down Expand Up @@ -115,7 +117,8 @@ func (c *Cache) checkFileCacheExists(ci *sfs.ConfigItemMetaV1) (bool, error) {
func (c *Cache) GetFileContent(ci *sfs.ConfigItemMetaV1) (bool, []byte) {
exists, err := c.checkFileCacheExists(ci)
if err != nil {
logs.Errorf("check config item %s cache exists failed, err: %s", ci.ContentSpec.Signature, err.Error())
logger.Error("check config item cache exists failed",
slog.String("item", ci.ContentSpec.Signature), logger.ErrAttr(err))
return false, nil
}
if !exists {
Expand All @@ -124,7 +127,8 @@ func (c *Cache) GetFileContent(ci *sfs.ConfigItemMetaV1) (bool, []byte) {
filePath := path.Join(c.path, ci.ContentSpec.Signature)
bytes, err := os.ReadFile(filePath)
if err != nil {
logs.Errorf("read config item cache file %s failed, err: %s", filePath, err.Error())
logger.Error("read config item cache file failed",
slog.String("file", filePath), logger.ErrAttr(err))
return false, nil
}
return true, bytes
Expand All @@ -134,7 +138,8 @@ func (c *Cache) GetFileContent(ci *sfs.ConfigItemMetaV1) (bool, []byte) {
func (c *Cache) CopyToFile(ci *sfs.ConfigItemMetaV1, filePath string) bool {
exists, err := c.checkFileCacheExists(ci)
if err != nil {
logs.Warnf("check config item %s cache exists failed, err: %s", ci.ContentSpec.Signature, err.Error())
logger.Warn("check config item cache exists failed",
slog.String("item", ci.ContentSpec.Signature), logger.ErrAttr(err))
return false
}
if !exists {
Expand All @@ -143,19 +148,19 @@ func (c *Cache) CopyToFile(ci *sfs.ConfigItemMetaV1, filePath string) bool {
cacheFilePath := path.Join(c.path, ci.ContentSpec.Signature)
src, err := os.Open(cacheFilePath)
if err != nil {
logs.Errorf("open config item cache file %s failed, err: %s", cacheFilePath, err.Error())
logger.Error("open config item cache file failed", slog.String("file", cacheFilePath), logger.ErrAttr(err))
return false
}
defer src.Close()
dst, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
if err != nil {
logs.Errorf("open destination file %s failed, err: %s", filePath, err.Error())
logger.Error("open destination file failed", slog.String("file", filePath), logger.ErrAttr(err))
return false
}
defer dst.Close()
if _, err := io.Copy(dst, src); err != nil {
logs.Errorf("copy config item cache file %s to destination file %s failed, err: %s",
cacheFilePath, filePath, err.Error())
logger.Error("copy config item cache file to destination file failed",
slog.String("cache_file", cacheFilePath), slog.String("file", filePath), logger.ErrAttr(err))
return false
}
return true
Expand Down
19 changes: 11 additions & 8 deletions cli/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import (
"reflect"
"strings"

"bscp.io/pkg/logs"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"golang.org/x/exp/slog"

"github.com/TencentBlueKing/bscp-go/cli/config"
"github.com/TencentBlueKing/bscp-go/cli/constant"
"github.com/TencentBlueKing/bscp-go/logger"
"github.com/TencentBlueKing/bscp-go/pkg/util"
)

Expand All @@ -54,7 +55,8 @@ var (

// rootEnvs variable definition
rootEnvs = map[string]string{
"verbose": "verbosity",
"log.format": "log.format",
"log.level": "log.level",
}

// commonEnvs variable definition
Expand Down Expand Up @@ -210,9 +212,9 @@ func watchLabelsFile(ctx context.Context, path string, oldLabels map[string]stri
for {
select {
case <-ctx.Done():
logs.Infof("watch labels file %s stoped because of %s", path, ctx.Err().Error())
logger.Info("watch labels file stoped because of ctx done", slog.String("file", path), logger.ErrAttr(ctx.Err()))
if err := watcher.Close(); err != nil {
logs.Warnf("close watcher failed, err: %s", err.Error())
logger.Warn("close watcher failed", logger.ErrAttr(err))
}
return
case event := <-watcher.Events:
Expand All @@ -223,7 +225,7 @@ func watchLabelsFile(ctx context.Context, path string, oldLabels map[string]stri

absPath, err := filepath.Abs(event.Name)
if err != nil {
logs.Warnf("get labels file absPath failed, err: %s", err.Error())
logger.Warn("get labels file absPath failed", logger.ErrAttr(err))
continue
}
if absPath != path {
Expand All @@ -247,12 +249,13 @@ func watchLabelsFile(ctx context.Context, path string, oldLabels map[string]stri
continue
}

logs.Infof("labels file %s changed, try reset labels, old: %s, new: %s", path, oldLabels, labels)
logger.Info("labels file changed, try reset labels",
slog.String("file", path), slog.Any("old", oldLabels), slog.Any("new", labels))
msg.Labels = labels
watchChan <- msg
oldLabels = labels
case err := <-watcher.Errors:
logs.Errorf("watcher error: %s", err.Error())
logger.Error("watcher error", logger.ErrAttr(err))
}
}
}()
Expand All @@ -265,7 +268,7 @@ func readLabelsFile(path string) (map[string]string, error) {
labels := make(map[string]string)
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
logs.Warnf("labels file %s not exist, skip read", path)
logger.Warn("labels file not exist, skip read", slog.String("path", path))
return labels, nil
}
return nil, fmt.Errorf("stat labels file %s failed, err: %s", path, err.Error())
Expand Down
14 changes: 7 additions & 7 deletions cli/cmd/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ import (
"time"

"bscp.io/pkg/dal/table"
"bscp.io/pkg/logs"
"github.com/spf13/cobra"
"golang.org/x/exp/slog"

"github.com/TencentBlueKing/bscp-go/cli/constant"
"github.com/TencentBlueKing/bscp-go/cli/eventmeta"
"github.com/TencentBlueKing/bscp-go/cli/util"
"github.com/TencentBlueKing/bscp-go/client"
"github.com/TencentBlueKing/bscp-go/logger"
"github.com/TencentBlueKing/bscp-go/option"
pkgutil "github.com/TencentBlueKing/bscp-go/pkg/util"
)
Expand All @@ -44,14 +45,14 @@ var (
// Pull executes the pull command.
func Pull(cmd *cobra.Command, args []string) {
if err := initArgs(); err != nil {
logs.Errorf(err.Error())
logger.Error("init", logger.ErrAttr(err))
os.Exit(1)
}

if conf.LabelsFile != "" {
labels, err := readLabelsFile(conf.LabelsFile)
if err != nil {
logs.Errorf("read labels file failed, err: %s", err.Error())
logger.Error("read labels file failed", logger.ErrAttr(err))
os.Exit(1)
}
conf.Labels = pkgutil.MergeLabels(conf.Labels, labels)
Expand All @@ -62,10 +63,9 @@ func Pull(cmd *cobra.Command, args []string) {
option.Token(conf.Token),
option.Labels(conf.Labels),
option.UID(conf.UID),
option.LogVerbosity(logVerbosity),
)
if err != nil {
logs.Errorf(err.Error())
logger.Error("init client", logger.ErrAttr(err))
os.Exit(1)
}
for _, app := range conf.Apps {
Expand All @@ -77,7 +77,7 @@ func Pull(cmd *cobra.Command, args []string) {
tempDir = conf.TempDir
}
if err = pullAppFiles(bscp, tempDir, conf.Biz, app.Name, opts); err != nil {
logs.Errorf(err.Error())
logger.Error("pull files failed", logger.ErrAttr(err))
os.Exit(1)
}
}
Expand Down Expand Up @@ -119,7 +119,7 @@ func pullAppFiles(bscp client.Client, tempDir string, biz uint32, app string, op
if err := eventmeta.AppendMetadataToFile(appDir, metadata); err != nil {
return err
}
logs.Infof("pull files success, current releaseID: %d", release.ReleaseID)
logger.Info("pull files success", slog.Any("releaseID", release.ReleaseID))
return nil
}

Expand Down
14 changes: 9 additions & 5 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import (
"fmt"
"os"

"bscp.io/pkg/logs"
"github.com/spf13/cobra"

"github.com/TencentBlueKing/bscp-go/logger"
)

var (
logVerbosity uint
rootCmd = &cobra.Command{
logFormat string
logLevel string
rootCmd = &cobra.Command{
Use: "bscp",
Short: "bscp is a command line tool for blueking service config platform",
Long: `bscp is a command line tool for blueking service config platform`,
Expand All @@ -32,7 +34,7 @@ var (
// Execute executes the root command.
func Execute() {
if err := rootCmd.Execute(); err != nil {
logs.Errorf(err.Error())
logger.Error("exec cmd", logger.ErrAttr(err))
os.Exit(1)
}
}
Expand All @@ -46,7 +48,9 @@ func init() {
rootCmd.AddCommand(PullCmd)
rootCmd.AddCommand(WatchCmd)
rootCmd.AddCommand(VersionCmd)
rootCmd.PersistentFlags().UintVarP(&logVerbosity, "verbosity", "v", 0, "log verbosity")
rootCmd.PersistentFlags().StringVarP(&logFormat, "log.format", "", "logfmt",
"log format to use. possible options: logfmt or json.")
rootCmd.PersistentFlags().StringVarP(&logLevel, "log.level", "", "info", "log filtering level.")
rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "", "config file path")

for env, f := range rootEnvs {
Expand Down

0 comments on commit 3f4ce2f

Please sign in to comment.