From 6aaca6261de516f2d570aa7e596f694db7cf2a00 Mon Sep 17 00:00:00 2001 From: fengshunli <1171313930@qq.com> Date: Thu, 18 May 2023 22:00:27 +0800 Subject: [PATCH] Feat: add zap log print Signed-off-by: fengshunli <1171313930@qq.com> --- cli/cli/cli.go | 51 ++++--- cli/command/cluster/add.go | 9 +- cli/command/cluster/checkout.go | 7 +- cli/command/cluster/export.go | 7 +- cli/command/cluster/import.go | 2 - cli/command/cluster/list.go | 7 +- cli/command/cluster/remove.go | 7 +- cli/command/restart.go | 1 - cli/command/stop.go | 2 +- internal/build/build.go | 1 + internal/build/release.go | 1 + internal/configure/client.go | 5 +- internal/configure/curveadm/curveadm.go | 28 ++-- internal/configure/os/os.go | 4 +- internal/configure/topology/dc.go | 11 +- internal/playbook/tasks/monitor.go | 1 - internal/playbook/tasks/tasks.go | 1 - internal/task/task/bs/add_target.go | 2 +- internal/task/task/bs/format_status.go | 4 +- internal/task/task/bs/list_targets.go | 17 +-- internal/task/task/bs/unmap.go | 2 +- internal/task/task/checker/permission.go | 2 +- internal/task/task/common/create_container.go | 9 +- internal/task/task/common/update_topology.go | 2 +- internal/task/task/fs/mount.go | 4 +- internal/tui/common/prompt.go | 2 +- pkg/log/glg/glg.go | 1 - pkg/log/glg/log.go | 117 ---------------- pkg/log/log.go | 77 ----------- pkg/log/zaplog/log.go | 130 +++++++++++++----- pkg/module/docker_cli.go | 2 +- pkg/module/file.go | 24 ++-- pkg/module/module.go | 13 +- pkg/module/ssh.go | 37 ++--- pkg/variable/variables.go | 6 +- 35 files changed, 235 insertions(+), 361 deletions(-) delete mode 100644 pkg/log/glg/glg.go delete mode 100644 pkg/log/glg/log.go delete mode 100644 pkg/log/log.go diff --git a/cli/cli/cli.go b/cli/cli/cli.go index 76c0b94fd..f96121e3e 100644 --- a/cli/cli/cli.go +++ b/cli/cli/cli.go @@ -25,6 +25,7 @@ package cli import ( "errors" "fmt" + "go.uber.org/zap" "io" "os" "path" @@ -41,7 +42,7 @@ import ( tui "github.com/opencurve/curveadm/internal/tui/common" "github.com/opencurve/curveadm/internal/utils" cliutil "github.com/opencurve/curveadm/internal/utils" - log "github.com/opencurve/curveadm/pkg/log/glg" + "github.com/opencurve/curveadm/pkg/log/zaplog" "github.com/opencurve/curveadm/pkg/module" ) @@ -131,15 +132,11 @@ func (curveadm *CurveAdm) init() error { configure.ReplaceGlobals(config) // (3) Init logger - now := time.Now().Format("2006-01-02_15-04-05") - logpath := fmt.Sprintf("%s/curveadm-%s.log", curveadm.logDir, now) - if err := log.Init(config.GetLogLevel(), logpath); err != nil { - return errno.ERR_INIT_LOGGER_FAILED.E(err) - } else { - log.Info("Init logger success", - log.Field("LogPath", logpath), - log.Field("LogLevel", config.GetLogLevel())) - } + logpath := fmt.Sprintf("%s/curveadm.log", curveadm.logDir) + zaplog.LOG = zaplog.Init(config, logpath) + + zaplog.LOG.Info("Init logger success", + zap.String("LogPath", logpath)) // (4) Init error code errno.Init(logpath) @@ -148,8 +145,8 @@ func (curveadm *CurveAdm) init() error { dbpath := fmt.Sprintf("%s/curveadm.db", curveadm.dataDir) s, err := storage.NewStorage(dbpath) if err != nil { - log.Error("Init SQLite database failed", - log.Field("Error", err)) + zaplog.LOG.Error("Init SQLite database failed", + zap.Any("Error", err)) return errno.ERR_INIT_SQL_DATABASE_FAILED.E(err) } @@ -157,8 +154,8 @@ func (curveadm *CurveAdm) init() error { var hosts storage.Hosts hostses, err := s.GetHostses() if err != nil { - log.Error("Get hosts failed", - log.Field("Error", err)) + zaplog.LOG.Error("Get hosts failed", + zap.Any("Error", err)) return errno.ERR_GET_HOSTS_FAILED.E(err) } else if len(hostses) == 1 { hosts = hostses[0] @@ -167,20 +164,20 @@ func (curveadm *CurveAdm) init() error { // (7) Get current cluster cluster, err := s.GetCurrentCluster() if err != nil { - log.Error("Get current cluster failed", - log.Field("Error", err)) + zaplog.LOG.Error("Get current cluster failed", + zap.Any("Error", err)) return errno.ERR_GET_CURRENT_CLUSTER_FAILED.E(err) } else { - log.Info("Get current cluster success", - log.Field("ClusterId", cluster.Id), - log.Field("ClusterName", cluster.Name)) + zaplog.LOG.Info("Get current cluster success", + zap.Int("ClusterId", cluster.Id), + zap.String("ClusterName", cluster.Name)) } // (8) Get Disks var disks storage.Disks diskses, err := s.GetDisks() if err != nil { - log.Error("Get disks failed", log.Field("Error", err)) + zaplog.LOG.Error("Get disks failed", zap.Any("Error", err)) return errno.ERR_GET_DISKS_FAILED.E(err) } else if len(diskses) > 0 { disks = diskses[0] @@ -189,7 +186,7 @@ func (curveadm *CurveAdm) init() error { // (9) Get Disk Records diskRecords, err := s.GetDisk(comm.DISK_FILTER_ALL) if err != nil { - log.Error("Get disk records failed", log.Field("Error", err)) + zaplog.LOG.Error("Get disk records failed", zap.Any("Error", err)) return errno.ERR_GET_DISK_RECORDS_FAILED.E(err) } @@ -507,8 +504,8 @@ func (curveadm *CurveAdm) PreAudit(now time.Time, args []string) int64 { id, err := curveadm.Storage().InsertAuditLog( now, cwd, command, comm.AUDIT_STATUS_ABORT) if err != nil { - log.Error("Insert audit log failed", - log.Field("Error", err)) + zaplog.LOG.Error("Insert audit log failed", + zap.Any("Error", err)) } return id @@ -521,8 +518,8 @@ func (curveadm *CurveAdm) PostAudit(id int64, ec error) { auditLogs, err := curveadm.Storage().GetAuditLog(id) if err != nil { - log.Error("Get audit log failed", - log.Field("Error", err)) + zaplog.LOG.Error("Get audit log failed", + zap.Any("Error", err)) return } else if len(auditLogs) != 1 { return @@ -544,7 +541,7 @@ func (curveadm *CurveAdm) PostAudit(id int64, ec error) { err = curveadm.Storage().SetAuditLogStatus(id, status, errorCode) if err != nil { - log.Error("Set audit log status failed", - log.Field("Error", err)) + zaplog.LOG.Error("Set audit log status failed", + zap.Any("Error", err)) } } diff --git a/cli/command/cluster/add.go b/cli/command/cluster/add.go index 2beaa7d61..75f799a3b 100644 --- a/cli/command/cluster/add.go +++ b/cli/command/cluster/add.go @@ -31,8 +31,9 @@ import ( "github.com/opencurve/curveadm/internal/errno" "github.com/opencurve/curveadm/internal/playbook" "github.com/opencurve/curveadm/internal/utils" - log "github.com/opencurve/curveadm/pkg/log/glg" + "github.com/opencurve/curveadm/pkg/log/zaplog" "github.com/spf13/cobra" + "go.uber.org/zap" ) const ( @@ -139,9 +140,9 @@ func runAdd(curveadm *cli.CurveAdm, options addOptions) error { storage := curveadm.Storage() clusters, err := storage.GetClusters(name) if err != nil { - log.Error("Get clusters failed", - log.Field("cluster name", name), - log.Field("error", err)) + zaplog.LOG.Error("Get clusters failed", + zap.String("cluster name", name), + zap.Any("error", err)) return errno.ERR_GET_ALL_CLUSTERS_FAILED.E(err) } else if len(clusters) > 0 { return errno.ERR_CLUSTER_ALREADY_EXIST. diff --git a/cli/command/cluster/checkout.go b/cli/command/cluster/checkout.go index 74d0b2026..2982ba684 100644 --- a/cli/command/cluster/checkout.go +++ b/cli/command/cluster/checkout.go @@ -28,8 +28,9 @@ import ( "github.com/opencurve/curveadm/cli/cli" "github.com/opencurve/curveadm/internal/errno" cliutil "github.com/opencurve/curveadm/internal/utils" - log "github.com/opencurve/curveadm/pkg/log/glg" + "github.com/opencurve/curveadm/pkg/log/zaplog" "github.com/spf13/cobra" + "go.uber.org/zap" ) type checkoutOptions struct { @@ -59,8 +60,8 @@ func runCheckout(curveadm *cli.CurveAdm, options checkoutOptions) error { storage := curveadm.Storage() clusters, err := storage.GetClusters(clusterName) if err != nil { - log.Error("Get clusters failed", - log.Field("error", err)) + zaplog.LOG.Error("Get clusters failed", + zap.Any("error", err)) return errno.ERR_GET_ALL_CLUSTERS_FAILED.E(err) } else if len(clusters) == 0 { return errno.ERR_CLUSTER_NOT_FOUND. diff --git a/cli/command/cluster/export.go b/cli/command/cluster/export.go index 6941516a8..d1f697df7 100644 --- a/cli/command/cluster/export.go +++ b/cli/command/cluster/export.go @@ -24,12 +24,13 @@ package cluster import ( "fmt" + "github.com/opencurve/curveadm/pkg/log/zaplog" + "go.uber.org/zap" "os" "github.com/opencurve/curveadm/cli/cli" "github.com/opencurve/curveadm/internal/storage" "github.com/opencurve/curveadm/internal/utils" - log "github.com/opencurve/curveadm/pkg/log/glg" "github.com/spf13/cobra" ) @@ -129,12 +130,12 @@ func runExport(curveadm *cli.CurveAdm, options exportOptions) error { storage := curveadm.Storage() clusters, err := storage.GetClusters(name) if err != nil { - log.Error("GetClusters", log.Field("error", err)) + zaplog.LOG.Error("GetClusters", zap.Any("error", err)) return err } else if len(clusters) == 0 { return fmt.Errorf("cluster %s not exist", name) } else if services, err := storage.GetServices(clusters[0].Id); err != nil { - log.Error("GetServices", log.Field("error", err)) + zaplog.LOG.Error("GetServices", zap.Any("error", err)) return err } else if err = exportCluster(clusters[0], services, options.outfile); err != nil { return err diff --git a/cli/command/cluster/import.go b/cli/command/cluster/import.go index d5b20654a..442fce89b 100644 --- a/cli/command/cluster/import.go +++ b/cli/command/cluster/import.go @@ -30,7 +30,6 @@ import ( "strconv" "strings" - "github.com/opencurve/curveadm/pkg/log/zaplog" "github.com/opencurve/curveadm/cli/cli" "github.com/opencurve/curveadm/internal/storage" "github.com/opencurve/curveadm/internal/utils" @@ -189,7 +188,6 @@ func runImport(curveadm *cli.CurveAdm, options importOptions) error { storage := curveadm.Storage() clusters, err := storage.GetClusters(name) if err != nil { - zaplog.Error("GetClusters", zaplog.Field("error", err)) return err } else if len(clusters) != 0 { return fmt.Errorf("cluster %s already exist", name) diff --git a/cli/command/cluster/list.go b/cli/command/cluster/list.go index 13bf99694..8c8852103 100644 --- a/cli/command/cluster/list.go +++ b/cli/command/cluster/list.go @@ -29,8 +29,9 @@ import ( "github.com/opencurve/curveadm/internal/errno" "github.com/opencurve/curveadm/internal/tui" cliutil "github.com/opencurve/curveadm/internal/utils" - log "github.com/opencurve/curveadm/pkg/log/glg" + "github.com/opencurve/curveadm/pkg/log/zaplog" "github.com/spf13/cobra" + "go.uber.org/zap" ) type listOptions struct { @@ -62,8 +63,8 @@ func runList(curveadm *cli.CurveAdm, options listOptions) error { storage := curveadm.Storage() clusters, err := storage.GetClusters("%") if err != nil { - log.Error("Get clusters failed", - log.Field("error", err)) + zaplog.LOG.Error("Get clusters failed", + zap.Any("error", err)) return errno.ERR_GET_ALL_CLUSTERS_FAILED.E(err) } diff --git a/cli/command/cluster/remove.go b/cli/command/cluster/remove.go index 4f84ee732..91fff41d4 100644 --- a/cli/command/cluster/remove.go +++ b/cli/command/cluster/remove.go @@ -30,8 +30,9 @@ import ( "github.com/opencurve/curveadm/internal/errno" tui "github.com/opencurve/curveadm/internal/tui/common" cliutil "github.com/opencurve/curveadm/internal/utils" - log "github.com/opencurve/curveadm/pkg/log/glg" + "github.com/opencurve/curveadm/pkg/log/zaplog" "github.com/spf13/cobra" + "go.uber.org/zap" ) type removeOptions struct { @@ -86,8 +87,8 @@ func runRemove(curveadm *cli.CurveAdm, options removeOptions) error { clusterName := options.clusterName clusters, err := storage.GetClusters(clusterName) // Get all clusters if err != nil { - log.Error("Get cluster failed", - log.Field("error", err)) + zaplog.LOG.Error("Get cluster failed", + zap.Any("error", err)) return errno.ERR_GET_ALL_CLUSTERS_FAILED.E(err) } else if len(clusters) == 0 { return errno.ERR_CLUSTER_NOT_FOUND. diff --git a/cli/command/restart.go b/cli/command/restart.go index ee15dda72..8fc0493a6 100644 --- a/cli/command/restart.go +++ b/cli/command/restart.go @@ -34,7 +34,6 @@ import ( "github.com/spf13/cobra" ) - var ( RESTART_PLAYBOOK_STEPS = []int{ playbook.RESTART_SERVICE, diff --git a/cli/command/stop.go b/cli/command/stop.go index f30a4533a..10ccbb779 100644 --- a/cli/command/stop.go +++ b/cli/command/stop.go @@ -107,7 +107,7 @@ func runStop(curveadm *cli.CurveAdm, options stopOptions) error { } // 3) confirm by user - pass := tui.ConfirmYes(tui.PromptStopService(options.id, options.role, options.host)); + pass := tui.ConfirmYes(tui.PromptStopService(options.id, options.role, options.host)) if !pass { curveadm.WriteOut(tui.PromptCancelOpetation("stop service")) return errno.ERR_CANCEL_OPERATION diff --git a/internal/build/build.go b/internal/build/build.go index 0ae75f09e..7925c4878 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -1,3 +1,4 @@ +//go:build debug // +build debug package build diff --git a/internal/build/release.go b/internal/build/release.go index 01e5aecdc..89cbe28b5 100644 --- a/internal/build/release.go +++ b/internal/build/release.go @@ -1,3 +1,4 @@ +//go:build !debug // +build !debug package build diff --git a/internal/configure/client.go b/internal/configure/client.go index 607cb7638..b348434fe 100644 --- a/internal/configure/client.go +++ b/internal/configure/client.go @@ -25,13 +25,14 @@ package configure import ( "bytes" "fmt" + "github.com/opencurve/curveadm/pkg/log/zaplog" + "go.uber.org/zap" "strings" "github.com/opencurve/curveadm/internal/build" "github.com/opencurve/curveadm/internal/configure/topology" "github.com/opencurve/curveadm/internal/errno" "github.com/opencurve/curveadm/internal/utils" - log "github.com/opencurve/curveadm/pkg/log/glg" "github.com/opencurve/curveadm/pkg/variable" "github.com/spf13/viper" ) @@ -104,7 +105,7 @@ func NewClientConfig(config map[string]interface{}) (*ClientConfig, error) { vars.Register(variable.Variable{Name: "prefix", Value: "/curvebs/nebd"}) err := vars.Build() if err != nil { - log.Error("Build variables failed", log.Field("Error", err)) + zaplog.LOG.Error("Build variables failed", zap.Any("Error", err)) return nil, errno.ERR_RESOLVE_CLIENT_VARIABLE_FAILED.E(err) } diff --git a/internal/configure/curveadm/curveadm.go b/internal/configure/curveadm/curveadm.go index 4f1b850ba..53b3bed92 100644 --- a/internal/configure/curveadm/curveadm.go +++ b/internal/configure/curveadm/curveadm.go @@ -54,12 +54,14 @@ const ( type ( CurveAdmConfig struct { - LogLevel string - SudoAlias string - Timeout int - AutoUpgrade bool - SSHRetries int - SSHTimeout int + LogLevel string + SudoAlias string + Timeout int + AutoUpgrade bool + ShowLine bool + LogInConsole bool + SSHRetries int + SSHTimeout int } CurveAdm struct { @@ -72,12 +74,14 @@ var ( GlobalCurveAdmConfig *CurveAdmConfig defaultCurveAdmConfig = &CurveAdmConfig{ - LogLevel: "error", - SudoAlias: "sudo", - Timeout: 180, - AutoUpgrade: true, - SSHRetries: 3, - SSHTimeout: 10, + LogLevel: "info", + SudoAlias: "sudo", + Timeout: 180, + AutoUpgrade: true, + ShowLine: true, + LogInConsole: true, + SSHRetries: 3, + SSHTimeout: 10, } SUPPORT_LOG_LEVEL = map[string]bool{ diff --git a/internal/configure/os/os.go b/internal/configure/os/os.go index b02aeef05..63f7ebb08 100644 --- a/internal/configure/os/os.go +++ b/internal/configure/os/os.go @@ -25,9 +25,9 @@ package os const ( - PATH_FSTAB = "/etc/fstab" + PATH_FSTAB = "/etc/fstab" PATH_OS_RELEASE = "/etc/os-release" - MAX_PORT = 65535 + MAX_PORT = 65535 ) func GetFSTabPath() string { return PATH_FSTAB } diff --git a/internal/configure/topology/dc.go b/internal/configure/topology/dc.go index a39112ff0..1118f9c41 100644 --- a/internal/configure/topology/dc.go +++ b/internal/configure/topology/dc.go @@ -26,12 +26,13 @@ package topology import ( "fmt" + "github.com/opencurve/curveadm/pkg/log/zaplog" + "go.uber.org/zap" "strconv" "github.com/opencurve/curveadm/internal/build" "github.com/opencurve/curveadm/internal/errno" "github.com/opencurve/curveadm/internal/utils" - log "github.com/opencurve/curveadm/pkg/log/glg" "github.com/opencurve/curveadm/pkg/variable" ) @@ -156,8 +157,8 @@ func NewDeployConfig(ctx *Context, kind, role, host, name string, replicas int, func (dc *DeployConfig) renderVariables() error { vars := dc.GetVariables() if err := vars.Build(); err != nil { - log.Error("Build variables failed", - log.Field("error", err)) + zaplog.LOG.Error("Build variables failed", + zap.Any("error", err)) return errno.ERR_RESOLVE_VARIABLE_FAILED.E(err) } @@ -257,8 +258,8 @@ func (dc *DeployConfig) ResolveHost() error { vars := dc.GetVariables() if err := vars.Build(); err != nil { - log.Error("Build variables failed", - log.Field("error", err)) + zaplog.LOG.Error("Build variables failed", + zap.Any("error", err)) return errno.ERR_RESOLVE_VARIABLE_FAILED.E(err) } diff --git a/internal/playbook/tasks/monitor.go b/internal/playbook/tasks/monitor.go index 4eb98cca3..66bd1ef1c 100644 --- a/internal/playbook/tasks/monitor.go +++ b/internal/playbook/tasks/monitor.go @@ -28,7 +28,6 @@ import ( "github.com/opencurve/curveadm/internal/task/task" ) - const ( STATUS_OK = iota STATUS_SKIP diff --git a/internal/playbook/tasks/tasks.go b/internal/playbook/tasks/tasks.go index 589a2dc4e..4322dc9d7 100644 --- a/internal/playbook/tasks/tasks.go +++ b/internal/playbook/tasks/tasks.go @@ -35,7 +35,6 @@ import ( ) type ( - ExecOptions struct { Concurrency uint SilentMainBar bool diff --git a/internal/task/task/bs/add_target.go b/internal/task/task/bs/add_target.go index 4c59ee6bb..2383fe24c 100644 --- a/internal/task/task/bs/add_target.go +++ b/internal/task/task/bs/add_target.go @@ -97,7 +97,7 @@ func NewAddTargetTask(curveadm *cli.CurveAdm, cc *configure.ClientConfig) (*task ContainerId: &containerId, Cmd: "/bin/bash", Args: []string{targetScriptPath, user, volume, strconv.FormatBool(options.Create), strconv.Itoa(options.Size), strconv.FormatUint(options.Blocksize, 10)}, - TaskName: "addTarget"+TranslateVolumeName(volume, user), + TaskName: "addTarget" + TranslateVolumeName(volume, user), ExecOptions: curveadm.ExecOptions(), }) diff --git a/internal/task/task/bs/format_status.go b/internal/task/task/bs/format_status.go index 121dd80b9..d09c27eca 100644 --- a/internal/task/task/bs/format_status.go +++ b/internal/task/task/bs/format_status.go @@ -26,10 +26,10 @@ import ( "fmt" "strings" - comm "github.com/opencurve/curveadm/internal/common" - "github.com/opencurve/curveadm/internal/errno" "github.com/opencurve/curveadm/cli/cli" + comm "github.com/opencurve/curveadm/internal/common" "github.com/opencurve/curveadm/internal/configure" + "github.com/opencurve/curveadm/internal/errno" "github.com/opencurve/curveadm/internal/task/context" "github.com/opencurve/curveadm/internal/task/step" "github.com/opencurve/curveadm/internal/task/task" diff --git a/internal/task/task/bs/list_targets.go b/internal/task/task/bs/list_targets.go index 5e897bf75..32abc7dc3 100644 --- a/internal/task/task/bs/list_targets.go +++ b/internal/task/task/bs/list_targets.go @@ -64,13 +64,14 @@ func addTarget(memStorage *utils.SafeMap, id string, target *step.Target) { /* Output Example: Target 3: iqn.2022-02.com.opencurve:curve.wine93/test03 - ... - LUN information: - LUN: 0 - ... - LUN: 1 - ... - Backing store path: cbd:pool//test03_wine93_ + + ... + LUN information: + LUN: 0 + ... + LUN: 1 + ... + Backing store path: cbd:pool//test03_wine93_ */ func (s *step2FormatTarget) Execute(ctx *context.Context) error { output := *s.output @@ -83,7 +84,7 @@ func (s *step2FormatTarget) Execute(ctx *context.Context) error { mu := titlePattern.FindStringSubmatch(line) if len(mu) > 0 { target = &step.Target{ - Host: s.host, + Host: s.host, Tid: mu[1], Name: mu[2], Store: "-", diff --git a/internal/task/task/bs/unmap.go b/internal/task/task/bs/unmap.go index 9e1740f4a..603350e66 100644 --- a/internal/task/task/bs/unmap.go +++ b/internal/task/task/bs/unmap.go @@ -26,13 +26,13 @@ import ( "fmt" "strings" - tui "github.com/opencurve/curveadm/internal/tui/common" "github.com/opencurve/curveadm/cli/cli" comm "github.com/opencurve/curveadm/internal/common" "github.com/opencurve/curveadm/internal/errno" "github.com/opencurve/curveadm/internal/task/context" "github.com/opencurve/curveadm/internal/task/step" "github.com/opencurve/curveadm/internal/task/task" + tui "github.com/opencurve/curveadm/internal/tui/common" "github.com/opencurve/curveadm/pkg/module" ) diff --git a/internal/task/task/checker/permission.go b/internal/task/task/checker/permission.go index dbe4e0565..d074bb5f2 100644 --- a/internal/task/task/checker/permission.go +++ b/internal/task/task/checker/permission.go @@ -145,7 +145,7 @@ func NewCheckPermissionTask(curveadm *cli.CurveAdm, dc *topology.DeployConfig) ( t.AddStep(&step.Ping{ Destination: &hostname, Count: 1, - Timeout: 1, + Timeout: 1, Success: &success, ExecOptions: curveadm.ExecOptions(), }) diff --git a/internal/task/task/common/create_container.go b/internal/task/task/common/create_container.go index 7de49259c..ef119b14b 100644 --- a/internal/task/task/common/create_container.go +++ b/internal/task/task/common/create_container.go @@ -26,6 +26,8 @@ package common import ( "fmt" + "github.com/opencurve/curveadm/pkg/log/zaplog" + "go.uber.org/zap" "strings" "github.com/opencurve/curveadm/cli/cli" @@ -37,7 +39,6 @@ import ( "github.com/opencurve/curveadm/internal/task/context" "github.com/opencurve/curveadm/internal/task/step" "github.com/opencurve/curveadm/internal/task/task" - log "github.com/opencurve/curveadm/pkg/log/glg" ) type step2GetService struct { @@ -90,9 +91,9 @@ func (s *step2InsertService) Execute(ctx *context.Context) error { err = s.E(err, errno.ERR_INSERT_SERVICE_CONTAINER_ID_FAILED) } - log.SwitchLevel(err)("Insert service", - log.Field("ServiceId", serviceId), - log.Field("ContainerId", containerId)) + zaplog.LOG.Error("Insert service", + zap.String("ServiceId", serviceId), + zap.String("ContainerId", containerId)) return err } diff --git a/internal/task/task/common/update_topology.go b/internal/task/task/common/update_topology.go index b89bb8a43..de6d8529e 100644 --- a/internal/task/task/common/update_topology.go +++ b/internal/task/task/common/update_topology.go @@ -27,10 +27,10 @@ package common import ( "github.com/opencurve/curveadm/cli/cli" comm "github.com/opencurve/curveadm/internal/common" + "github.com/opencurve/curveadm/internal/errno" "github.com/opencurve/curveadm/internal/task/context" "github.com/opencurve/curveadm/internal/task/step" "github.com/opencurve/curveadm/internal/task/task" - "github.com/opencurve/curveadm/internal/errno" ) func updateTopology(curveadm *cli.CurveAdm) step.LambdaType { diff --git a/internal/task/task/fs/mount.go b/internal/task/task/fs/mount.go index a9a48161c..31e6e6309 100644 --- a/internal/task/task/fs/mount.go +++ b/internal/task/task/fs/mount.go @@ -47,7 +47,7 @@ import ( const ( FORMAT_MOUNT_OPTION = "type=bind,source=%s,target=%s,bind-propagation=rshared" - CLIENT_CONFIG_DELIMITER = "=" + CLIENT_CONFIG_DELIMITER = "=" TOOLSV2_CONFIG_DELIMITER = ":" KEY_CURVEBS_CLUSTER = "curvebs.cluster" @@ -229,7 +229,7 @@ func newToolsMutate(cc *configure.ClientConfig, delimiter string) step.Mutate { func newToolsV2Mutate(cc *configure.ClientConfig, delimiter string) step.Mutate { clientConfig := cc.GetServiceConfig() tools2client := map[string]string{ - "mdsAddr": "mdsOpt.rpcRetryOpt.addrs", + "mdsAddr": "mdsOpt.rpcRetryOpt.addrs", } return func(in, key, value string) (out string, err error) { if len(key) == 0 { diff --git a/internal/tui/common/prompt.go b/internal/tui/common/prompt.go index 5479b7dc1..ddeadefcd 100644 --- a/internal/tui/common/prompt.go +++ b/internal/tui/common/prompt.go @@ -210,7 +210,7 @@ func PromptErrorCode(code int, description, clue, logpath string) string { if len(clue) > 0 { prompt.data["clue"] = prettyClue(clue) } - prompt.data["website"] = fmt.Sprintf("https://github.com/opencurve/curveadm/wiki/errno%d#%06d", code / 100000, code) + prompt.data["website"] = fmt.Sprintf("https://github.com/opencurve/curveadm/wiki/errno%d#%06d", code/100000, code) if len(logpath) > 0 { prompt.data["logpath"] = logpath } diff --git a/pkg/log/glg/glg.go b/pkg/log/glg/glg.go deleted file mode 100644 index abbe06612..000000000 --- a/pkg/log/glg/glg.go +++ /dev/null @@ -1 +0,0 @@ -package glg diff --git a/pkg/log/glg/log.go b/pkg/log/glg/log.go deleted file mode 100644 index 5846d12b6..000000000 --- a/pkg/log/glg/log.go +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright (c) 2021 NetEase Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* - * Project: CurveAdm - * Created Date: 2022-07-22 - * Author: Jingli Chen (Wine93) - */ - -// __SIGN_BY_WINE93__ - -package glg - -import ( - "fmt" - "strconv" - - "github.com/kpango/glg" -) - -const ( - INDENT_SPACE = 4 - SPACE = " " -) - -func convertLevel(level string) glg.LEVEL { - switch level { - case "debug": - return glg.DEBG - case "info": - return glg.INFO - case "warn": - return glg.WARN - case "error": - return glg.ERR - default: - return glg.DEBG - } - return glg.DEBG -} - -func Init(level, filename string) error { - writer := glg.FileWriter(filename, 0666) - - glg.Get(). - SetMode(glg.WRITER). // default is STD - SetLevel(convertLevel(level)). - SetLineTraceMode(glg.TraceLineShort). - AddLevelWriter(glg.DEBG, writer). - AddLevelWriter(glg.INFO, writer). - AddLevelWriter(glg.WARN, writer). - AddLevelWriter(glg.ERR, writer) - - return nil -} - -func Field(key string, val interface{}) string { - switch val.(type) { - case bool: - return fmt.Sprintf("%s: %s", key, strconv.FormatBool(val.(bool))) - case string: - return fmt.Sprintf("%s: %s", key, val) - case []byte: - return fmt.Sprintf("%s: %s", key, string(val.([]byte))) - case int: - case int64: - return fmt.Sprintf("%s: %d", key, val) - case error: - return fmt.Sprintf("%s: %s", key, val.(error).Error()) - } - return fmt.Sprintf("%s: %v", key, val) -} - -func format(message string, val ...string) string { - output := message + "\n" - for _, v := range val { - output = output + fmt.Sprintf("%*s%s\n", INDENT_SPACE, SPACE, v) - } - - return output -} - -func Debug(message string, val ...string) error { - return glg.Debug(format(message, val...)) -} - -func Info(message string, val ...string) error { - return glg.Info(format(message, val...)) -} - -func Warn(message string, val ...string) error { - return glg.Warn(format(message, val...)) -} - -func Error(message string, val ...string) error { - return glg.Error(format(message, val...)) -} - -func SwitchLevel(err error) func(message string, val ...string) error { - if err != nil { - return Error - } - return Info -} diff --git a/pkg/log/log.go b/pkg/log/log.go deleted file mode 100644 index eb67ca580..000000000 --- a/pkg/log/log.go +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright (c) 2021 NetEase Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* - * Project: CurveAdm - * Created Date: 2021-10-15 - * Author: Jingli Chen (Wine93) - */ - -package log - -import ( - zaplog "github.com/pingcap/log" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" -) - -var ( - Debug = zaplog.Debug - Info = zaplog.Info - Warn = zaplog.Warn - Error = zaplog.Error -) - -func Init(level, filename string) error { - gl, props, err := zaplog.InitLogger(&zaplog.Config{ - Level: level, - File: zaplog.FileLogConfig{Filename: filename}, - Format: "text", - DisableTimestamp: false, - }, zap.AddStacktrace(zapcore.FatalLevel)) - - if err != nil { - return err - } - - zaplog.ReplaceGlobals(gl, props) - return nil -} - -func SwitchLevel(err error) func(msg string, fields ...zap.Field) { - if err != nil { - return zaplog.Error - } - return zaplog.Info -} - -func Field(key string, val interface{}) zap.Field { - switch val.(type) { - case bool: - return zap.Bool(key, val.(bool)) - case string: - return zap.String(key, val.(string)) - case []byte: - return zap.String(key, string(val.([]byte))) - case int: - return zap.Int(key, val.(int)) - case int64: - return zap.Int64(key, val.(int64)) - case error: - return zap.String(key, val.(error).Error()) - } - return zap.Skip() -} diff --git a/pkg/log/zaplog/log.go b/pkg/log/zaplog/log.go index 76e22b69d..2d70c223e 100644 --- a/pkg/log/zaplog/log.go +++ b/pkg/log/zaplog/log.go @@ -25,55 +25,115 @@ package zaplog import ( - zaplog "github.com/pingcap/log" + "github.com/opencurve/curveadm/internal/configure/curveadm" "go.uber.org/zap" "go.uber.org/zap/zapcore" + "gopkg.in/natefinch/lumberjack.v2" + "os" + "time" ) var ( - Debug = zaplog.Debug - Info = zaplog.Info - Warn = zaplog.Warn - Error = zaplog.Error + level zapcore.Level + cfg *curveadm.CurveAdmConfig + logpath string + LOG *zap.Logger ) -func Init(level, filename string) error { - gl, props, err := zaplog.InitLogger(&zaplog.Config{ - Level: level, - File: zaplog.FileLogConfig{Filename: filename}, - Format: "text", - DisableTimestamp: false, - }, zap.AddStacktrace(zapcore.FatalLevel)) +func Init(config *curveadm.CurveAdmConfig, path string) *zap.Logger { + cfg = config + logpath = path + return Zap() +} +func Zap() (logger *zap.Logger) { + switch cfg.GetLogLevel() { + case "debug": + level = zap.DebugLevel + case "info": + level = zap.InfoLevel + case "warn": + level = zap.WarnLevel + case "error": + level = zap.ErrorLevel + case "dpanic": + level = zap.DPanicLevel + case "panic": + level = zap.PanicLevel + case "fatal": + level = zap.FatalLevel + default: + level = zap.InfoLevel + } - if err != nil { - return err + if level == zap.ErrorLevel { + logger = zap.New(getEncoderCore(), zap.AddStacktrace(level)) + } else { + logger = zap.New(getEncoderCore()) } - zaplog.ReplaceGlobals(gl, props) - return nil + if cfg.ShowLine { + logger = logger.WithOptions(zap.AddCaller()) + } + return logger } -func SwitchLevel(err error) func(msg string, fields ...zap.Field) { - if err != nil { - return zaplog.Error +func getEncoderConfig() (config zapcore.EncoderConfig) { + config = zapcore.EncoderConfig{ + MessageKey: "message", + LevelKey: "level", + TimeKey: "time", + NameKey: "logger", + CallerKey: "caller", + StacktraceKey: "stacktrace", + LineEnding: zapcore.DefaultLineEnding, + EncodeTime: CustomTimeEncoder, + EncodeDuration: zapcore.SecondsDurationEncoder, + EncodeCaller: zapcore.FullCallerEncoder, } - return zaplog.Info + + switch "LowercaseLevelEncoder" { + case "LowercaseLevelEncoder": + config.EncodeLevel = zapcore.LowercaseLevelEncoder + case "LowercaseColorLevelEncoder": + config.EncodeLevel = zapcore.LowercaseColorLevelEncoder + case "CapitalLevelEncoder": + config.EncodeLevel = zapcore.CapitalLevelEncoder + case "CapitalColorLevelEncoder": + config.EncodeLevel = zapcore.CapitalColorLevelEncoder + default: + config.EncodeLevel = zapcore.LowercaseLevelEncoder + } + return config +} + +// getEncoder 获取zapcore.Encoder +func getEncoder() zapcore.Encoder { + // if global.CONFIG.Zap.Format == "json" { + // return zapcore.NewJSONEncoder(getEncoderConfig()) + // } + return zapcore.NewConsoleEncoder(getEncoderConfig()) } -func Field(key string, val interface{}) zap.Field { - switch val.(type) { - case bool: - return zap.Bool(key, val.(bool)) - case string: - return zap.String(key, val.(string)) - case []byte: - return zap.String(key, string(val.([]byte))) - case int: - return zap.Int(key, val.(int)) - case int64: - return zap.Int64(key, val.(int64)) - case error: - return zap.String(key, val.(error).Error()) +func getEncoderCore() (core zapcore.Core) { + writer := getWriteSyncer() + return zapcore.NewCore(getEncoder(), writer, level) +} + +func CustomTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) { + enc.AppendString(t.Format("curveadm" + " 2006/01/02 15:04:05")) +} + +func getWriteSyncer() zapcore.WriteSyncer { + lumberJackLogger := &lumberjack.Logger{ + Filename: logpath, // 日志文件的位置 + MaxSize: 10, // 在进行切割之前,日志文件的最大大小(以MB为单位) + MaxBackups: 200, // 保留旧文件的最大个数 + MaxAge: 30, // 保留旧文件的最大天数 + Compress: true, // 是否压缩/归档旧文件 + } + + if cfg.LogInConsole { + return zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(lumberJackLogger)) } - return zap.Skip() + return zapcore.AddSync(lumberJackLogger) } diff --git a/pkg/module/docker_cli.go b/pkg/module/docker_cli.go index 40b588b97..79b927d86 100644 --- a/pkg/module/docker_cli.go +++ b/pkg/module/docker_cli.go @@ -45,7 +45,7 @@ const ( TEMPLATE_COPY_INTO_CONTAINER = "docker cp {{.options}} {{.srcPath}} {{.container}}:{{.destPath}}" TEMPLATE_INSPECT_CONTAINER = "docker inspect {{.options}} {{.container}}" TEMPLATE_CONTAINER_LOGS = "docker logs {{.options}} {{.container}}" - TEMPLATE_UPDATE_CONTAINER = "docker update {{.options}} {{.container}}" + TEMPLATE_UPDATE_CONTAINER = "docker update {{.options}} {{.container}}" ) type DockerCli struct { diff --git a/pkg/module/file.go b/pkg/module/file.go index 6d428d87b..3db34f758 100644 --- a/pkg/module/file.go +++ b/pkg/module/file.go @@ -27,10 +27,10 @@ package module import ( "errors" "fmt" + "github.com/opencurve/curveadm/pkg/log/zaplog" + "go.uber.org/zap" "io/ioutil" "os" - - log "github.com/opencurve/curveadm/pkg/log/glg" ) const ( @@ -55,11 +55,11 @@ func (f *FileManager) Upload(localPath, remotePath string) error { } err := f.sshClient.Client().Upload(localPath, remotePath) - log.SwitchLevel(err)("UploadFile", - log.Field("remoteAddress", remoteAddr(f.sshClient)), - log.Field("localPath", localPath), - log.Field("remotePath", remotePath), - log.Field("error", err)) + zaplog.LOG.Error("UploadFile", + zap.String("remoteAddress", remoteAddr(f.sshClient)), + zap.String("localPath", localPath), + zap.String("remotePath", remotePath), + zap.Any("error", err)) return err } @@ -69,11 +69,11 @@ func (f *FileManager) Download(remotePath, localPath string) error { } err := f.sshClient.Client().Download(remotePath, localPath) - log.SwitchLevel(err)("DownloadFile", - log.Field("remoteAddress", remoteAddr(f.sshClient)), - log.Field("remotePath", remotePath), - log.Field("localPath", localPath), - log.Field("error", err)) + zaplog.LOG.Error("DownloadFile", + zap.String("remoteAddress", remoteAddr(f.sshClient)), + zap.String("remotePath", remotePath), + zap.String("localPath", localPath), + zap.Any("error", err)) return err } diff --git a/pkg/module/module.go b/pkg/module/module.go index 9d82a7fe5..7b4fdf579 100644 --- a/pkg/module/module.go +++ b/pkg/module/module.go @@ -28,13 +28,14 @@ import ( "bytes" "context" "fmt" + "github.com/opencurve/curveadm/pkg/log/zaplog" + "go.uber.org/zap" "os/exec" "strings" "text/template" "time" "github.com/melbahja/goph" - log "github.com/opencurve/curveadm/pkg/log/glg" ) type ( @@ -145,10 +146,10 @@ func execCommand(sshClient *SSHClient, err = &TimeoutError{options.ExecTimeoutSec} } - log.SwitchLevel(err)("Execute command", - log.Field("remoteAddr", remoteAddr(sshClient)), - log.Field("command", command), - log.Field("output", strings.TrimSuffix(string(out), "\n")), - log.Field("error", err)) + zaplog.LOG.Error("Execute command", + zap.String("remoteAddr", remoteAddr(sshClient)), + zap.String("command", command), + zap.String("output", strings.TrimSuffix(string(out), "\n")), + zap.Any("error", err)) return string(out), err } diff --git a/pkg/module/ssh.go b/pkg/module/ssh.go index 4e8abf889..9da3dc15c 100644 --- a/pkg/module/ssh.go +++ b/pkg/module/ssh.go @@ -26,11 +26,12 @@ package module import ( "errors" + "github.com/opencurve/curveadm/pkg/log/zaplog" + "go.uber.org/zap" "net" "time" "github.com/melbahja/goph" - log "github.com/opencurve/curveadm/pkg/log/glg" "golang.org/x/crypto/ssh" ) @@ -107,13 +108,13 @@ func NewSSHClient(config SSHConfig) (*SSHClient, error) { } if err != nil { - log.Error("Create SSH auth", - log.Field("user", user), - log.Field("host", host), - log.Field("port", port), - log.Field("forwardAgent", forwardAgent), - log.Field("privateKeyPath", privateKeyPath), - log.Field("error", err)) + zaplog.LOG.Error("Create SSH auth", + zap.String("user", user), + zap.String("host", host), + zap.Uint("port", port), + zap.Bool("forwardAgent", forwardAgent), + zap.String("privateKeyPath", privateKeyPath), + zap.Any("error", err)) return nil, err } @@ -129,16 +130,16 @@ connect: Callback: VerifyHost, }) - log.SwitchLevel(err)("Connect remote SSH", - log.Field("user", user), - log.Field("host", host), - log.Field("port", port), - log.Field("forwardAgent", forwardAgent), - log.Field("privateKeyPath", privateKeyPath), - log.Field("timeoutSec", connTimeoutSec), - log.Field("maxRetries", maxRetries), - log.Field("tries", tries), - log.Field("error", err)) + zaplog.LOG.Error("Connect remote SSH", + zap.String("user", user), + zap.String("host", host), + zap.Uint("port", port), + zap.Bool("forwardAgent", forwardAgent), + zap.String("privateKeyPath", privateKeyPath), + zap.Int("timeoutSec", connTimeoutSec), + zap.Int("maxRetries", maxRetries), + zap.Int("tries", tries), + zap.Any("error", err)) if err != nil { if tries < maxRetries { diff --git a/pkg/variable/variables.go b/pkg/variable/variables.go index a22fe1937..4fe118c4b 100644 --- a/pkg/variable/variables.go +++ b/pkg/variable/variables.go @@ -26,9 +26,9 @@ package variable import ( "fmt" + "github.com/opencurve/curveadm/pkg/log/zaplog" + "go.uber.org/zap" "regexp" - - log "github.com/opencurve/curveadm/pkg/log/glg" ) const ( @@ -152,6 +152,6 @@ func (vars *Variables) Rendering(s string) (string, error) { func (vars *Variables) Debug() { for _, v := range vars.m { - log.Info("Variable", log.Field(v.Name, v.Value)) + zaplog.LOG.Info("Variable", zap.String(v.Name, v.Value)) } }