Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add zap log print #233

Open
wants to merge 1 commit into
base: tombstone
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.Init(config, logpath)

zaplog.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.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.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.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.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.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.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.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.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.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.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.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.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.Error("GetServices", zap.Any("error", err))
return err
} else if err = exportCluster(clusters[0], services, options.outfile); err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions cli/command/cluster/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ package cluster

import (
"fmt"
"github.com/opencurve/curveadm/pkg/log/zaplog"
"go.uber.org/zap"
"io"
"os"
"regexp"
"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 +190,7 @@ 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))
zaplog.Error("GetClusters", zap.Any("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.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.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
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.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
11 changes: 6 additions & 5 deletions internal/configure/topology/dc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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.Error("Build variables failed",
zap.Any("error", err))
return errno.ERR_RESOLVE_VARIABLE_FAILED.E(err)
}

Expand Down Expand Up @@ -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.Error("Build variables failed",
zap.Any("error", err))
return errno.ERR_RESOLVE_VARIABLE_FAILED.E(err)
}

Expand Down
Loading