Skip to content

Commit

Permalink
Feat: add zap log print
Browse files Browse the repository at this point in the history
Signed-off-by: fengshunli <[email protected]>
  • Loading branch information
fengshunli committed May 18, 2023
1 parent d7db7c6 commit 6aaca62
Show file tree
Hide file tree
Showing 35 changed files with 235 additions and 361 deletions.
51 changes: 24 additions & 27 deletions cli/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ package cli
import (
"errors"
"fmt"
"go.uber.org/zap"
"io"
"os"
"path"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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)
Expand All @@ -148,17 +145,17 @@ 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)
}

// (6) Get hosts
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]
Expand All @@ -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]
Expand All @@ -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)
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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))
}
}
9 changes: 5 additions & 4 deletions cli/command/cluster/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions cli/command/cluster/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions cli/command/cluster/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions cli/command/cluster/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions cli/command/cluster/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down
7 changes: 4 additions & 3 deletions cli/command/cluster/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion cli/command/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"github.com/spf13/cobra"
)


var (
RESTART_PLAYBOOK_STEPS = []int{
playbook.RESTART_SERVICE,
Expand Down
2 changes: 1 addition & 1 deletion cli/command/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions internal/build/build.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build debug
// +build debug

package build
Expand Down
1 change: 1 addition & 0 deletions internal/build/release.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !debug
// +build !debug

package build
Expand Down
5 changes: 3 additions & 2 deletions internal/configure/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}

Expand Down
28 changes: 16 additions & 12 deletions internal/configure/curveadm/curveadm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions internal/configure/os/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Loading

0 comments on commit 6aaca62

Please sign in to comment.