Skip to content

Commit 7944daf

Browse files
committed
Feat: add zap log print
Signed-off-by: fengshunli <[email protected]>
1 parent 5feab2c commit 7944daf

File tree

19 files changed

+219
-337
lines changed

19 files changed

+219
-337
lines changed

cli/cli/cli.go

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ package cli
2525
import (
2626
"errors"
2727
"fmt"
28+
"go.uber.org/zap"
2829
"io"
2930
"os"
3031
"path"
@@ -41,7 +42,7 @@ import (
4142
tui "github.com/opencurve/curveadm/internal/tui/common"
4243
"github.com/opencurve/curveadm/internal/utils"
4344
cliutil "github.com/opencurve/curveadm/internal/utils"
44-
log "github.com/opencurve/curveadm/pkg/log/glg"
45+
"github.com/opencurve/curveadm/pkg/log/zaplog"
4546
"github.com/opencurve/curveadm/pkg/module"
4647
)
4748

@@ -131,15 +132,11 @@ func (curveadm *CurveAdm) init() error {
131132
configure.ReplaceGlobals(config)
132133

133134
// (3) Init logger
134-
now := time.Now().Format("2006-01-02_15-04-05")
135-
logpath := fmt.Sprintf("%s/curveadm-%s.log", curveadm.logDir, now)
136-
if err := log.Init(config.GetLogLevel(), logpath); err != nil {
137-
return errno.ERR_INIT_LOGGER_FAILED.E(err)
138-
} else {
139-
log.Info("Init logger success",
140-
log.Field("LogPath", logpath),
141-
log.Field("LogLevel", config.GetLogLevel()))
142-
}
135+
logpath := fmt.Sprintf("%s/curveadm.log", curveadm.logDir)
136+
zaplog.Init(config, logpath)
137+
138+
zaplog.Info("Init logger success",
139+
zap.String("LogPath", logpath))
143140

144141
// (4) Init error code
145142
errno.Init(logpath)
@@ -148,17 +145,17 @@ func (curveadm *CurveAdm) init() error {
148145
dbpath := fmt.Sprintf("%s/curveadm.db", curveadm.dataDir)
149146
s, err := storage.NewStorage(dbpath)
150147
if err != nil {
151-
log.Error("Init SQLite database failed",
152-
log.Field("Error", err))
148+
zaplog.Error("Init SQLite database failed",
149+
zap.Any("Error", err))
153150
return errno.ERR_INIT_SQL_DATABASE_FAILED.E(err)
154151
}
155152

156153
// (6) Get hosts
157154
var hosts storage.Hosts
158155
hostses, err := s.GetHostses()
159156
if err != nil {
160-
log.Error("Get hosts failed",
161-
log.Field("Error", err))
157+
zaplog.Error("Get hosts failed",
158+
zap.Any("Error", err))
162159
return errno.ERR_GET_HOSTS_FAILED.E(err)
163160
} else if len(hostses) == 1 {
164161
hosts = hostses[0]
@@ -167,20 +164,20 @@ func (curveadm *CurveAdm) init() error {
167164
// (7) Get current cluster
168165
cluster, err := s.GetCurrentCluster()
169166
if err != nil {
170-
log.Error("Get current cluster failed",
171-
log.Field("Error", err))
167+
zaplog.Error("Get current cluster failed",
168+
zap.Any("Error", err))
172169
return errno.ERR_GET_CURRENT_CLUSTER_FAILED.E(err)
173170
} else {
174-
log.Info("Get current cluster success",
175-
log.Field("ClusterId", cluster.Id),
176-
log.Field("ClusterName", cluster.Name))
171+
zaplog.Info("Get current cluster success",
172+
zap.Int("ClusterId", cluster.Id),
173+
zap.String("ClusterName", cluster.Name))
177174
}
178175

179176
// (8) Get Disks
180177
var disks storage.Disks
181178
diskses, err := s.GetDisks()
182179
if err != nil {
183-
log.Error("Get disks failed", log.Field("Error", err))
180+
zaplog.Error("Get disks failed", zap.Any("Error", err))
184181
return errno.ERR_GET_DISKS_FAILED.E(err)
185182
} else if len(diskses) > 0 {
186183
disks = diskses[0]
@@ -189,7 +186,7 @@ func (curveadm *CurveAdm) init() error {
189186
// (9) Get Disk Records
190187
diskRecords, err := s.GetDisk(comm.DISK_FILTER_ALL)
191188
if err != nil {
192-
log.Error("Get disk records failed", log.Field("Error", err))
189+
zaplog.Error("Get disk records failed", zap.Any("Error", err))
193190
return errno.ERR_GET_DISK_RECORDS_FAILED.E(err)
194191
}
195192

@@ -507,8 +504,8 @@ func (curveadm *CurveAdm) PreAudit(now time.Time, args []string) int64 {
507504
id, err := curveadm.Storage().InsertAuditLog(
508505
now, cwd, command, comm.AUDIT_STATUS_ABORT)
509506
if err != nil {
510-
log.Error("Insert audit log failed",
511-
log.Field("Error", err))
507+
zaplog.Error("Insert audit log failed",
508+
zap.Any("Error", err))
512509
}
513510

514511
return id
@@ -521,8 +518,8 @@ func (curveadm *CurveAdm) PostAudit(id int64, ec error) {
521518

522519
auditLogs, err := curveadm.Storage().GetAuditLog(id)
523520
if err != nil {
524-
log.Error("Get audit log failed",
525-
log.Field("Error", err))
521+
zaplog.Error("Get audit log failed",
522+
zap.Any("Error", err))
526523
return
527524
} else if len(auditLogs) != 1 {
528525
return
@@ -544,7 +541,7 @@ func (curveadm *CurveAdm) PostAudit(id int64, ec error) {
544541

545542
err = curveadm.Storage().SetAuditLogStatus(id, status, errorCode)
546543
if err != nil {
547-
log.Error("Set audit log status failed",
548-
log.Field("Error", err))
544+
zaplog.Error("Set audit log status failed",
545+
zap.Any("Error", err))
549546
}
550547
}

cli/command/cluster/add.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ import (
3131
"github.com/opencurve/curveadm/internal/errno"
3232
"github.com/opencurve/curveadm/internal/playbook"
3333
"github.com/opencurve/curveadm/internal/utils"
34-
log "github.com/opencurve/curveadm/pkg/log/glg"
34+
"github.com/opencurve/curveadm/pkg/log/zaplog"
3535
"github.com/spf13/cobra"
36+
"go.uber.org/zap"
3637
)
3738

3839
const (
@@ -139,9 +140,9 @@ func runAdd(curveadm *cli.CurveAdm, options addOptions) error {
139140
storage := curveadm.Storage()
140141
clusters, err := storage.GetClusters(name)
141142
if err != nil {
142-
log.Error("Get clusters failed",
143-
log.Field("cluster name", name),
144-
log.Field("error", err))
143+
zaplog.Error("Get clusters failed",
144+
zap.String("cluster name", name),
145+
zap.Any("error", err))
145146
return errno.ERR_GET_ALL_CLUSTERS_FAILED.E(err)
146147
} else if len(clusters) > 0 {
147148
return errno.ERR_CLUSTER_ALREADY_EXIST.

cli/command/cluster/checkout.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ import (
2828
"github.com/opencurve/curveadm/cli/cli"
2929
"github.com/opencurve/curveadm/internal/errno"
3030
cliutil "github.com/opencurve/curveadm/internal/utils"
31-
log "github.com/opencurve/curveadm/pkg/log/glg"
31+
"github.com/opencurve/curveadm/pkg/log/zaplog"
3232
"github.com/spf13/cobra"
33+
"go.uber.org/zap"
3334
)
3435

3536
type checkoutOptions struct {
@@ -59,8 +60,8 @@ func runCheckout(curveadm *cli.CurveAdm, options checkoutOptions) error {
5960
storage := curveadm.Storage()
6061
clusters, err := storage.GetClusters(clusterName)
6162
if err != nil {
62-
log.Error("Get clusters failed",
63-
log.Field("error", err))
63+
zaplog.Error("Get clusters failed",
64+
zap.Any("error", err))
6465
return errno.ERR_GET_ALL_CLUSTERS_FAILED.E(err)
6566
} else if len(clusters) == 0 {
6667
return errno.ERR_CLUSTER_NOT_FOUND.

cli/command/cluster/export.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ package cluster
2424

2525
import (
2626
"fmt"
27+
"github.com/opencurve/curveadm/pkg/log/zaplog"
28+
"go.uber.org/zap"
2729
"os"
2830

2931
"github.com/opencurve/curveadm/cli/cli"
3032
"github.com/opencurve/curveadm/internal/storage"
3133
"github.com/opencurve/curveadm/internal/utils"
32-
log "github.com/opencurve/curveadm/pkg/log/glg"
3334
"github.com/spf13/cobra"
3435
)
3536

@@ -129,12 +130,12 @@ func runExport(curveadm *cli.CurveAdm, options exportOptions) error {
129130
storage := curveadm.Storage()
130131
clusters, err := storage.GetClusters(name)
131132
if err != nil {
132-
log.Error("GetClusters", log.Field("error", err))
133+
zaplog.Error("GetClusters", zap.Any("error", err))
133134
return err
134135
} else if len(clusters) == 0 {
135136
return fmt.Errorf("cluster %s not exist", name)
136137
} else if services, err := storage.GetServices(clusters[0].Id); err != nil {
137-
log.Error("GetServices", log.Field("error", err))
138+
zaplog.Error("GetServices", zap.Any("error", err))
138139
return err
139140
} else if err = exportCluster(clusters[0], services, options.outfile); err != nil {
140141
return err

cli/command/cluster/import.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ package cluster
2424

2525
import (
2626
"fmt"
27+
"github.com/opencurve/curveadm/pkg/log/zaplog"
28+
"go.uber.org/zap"
2729
"io"
2830
"os"
2931
"regexp"
3032
"strconv"
3133
"strings"
3234

33-
"github.com/opencurve/curveadm/pkg/log/zaplog"
3435
"github.com/opencurve/curveadm/cli/cli"
3536
"github.com/opencurve/curveadm/internal/storage"
3637
"github.com/opencurve/curveadm/internal/utils"
@@ -189,7 +190,7 @@ func runImport(curveadm *cli.CurveAdm, options importOptions) error {
189190
storage := curveadm.Storage()
190191
clusters, err := storage.GetClusters(name)
191192
if err != nil {
192-
zaplog.Error("GetClusters", zaplog.Field("error", err))
193+
zaplog.Error("GetClusters", zap.Any("error", err))
193194
return err
194195
} else if len(clusters) != 0 {
195196
return fmt.Errorf("cluster %s already exist", name)

cli/command/cluster/list.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ import (
2929
"github.com/opencurve/curveadm/internal/errno"
3030
"github.com/opencurve/curveadm/internal/tui"
3131
cliutil "github.com/opencurve/curveadm/internal/utils"
32-
log "github.com/opencurve/curveadm/pkg/log/glg"
32+
"github.com/opencurve/curveadm/pkg/log/zaplog"
3333
"github.com/spf13/cobra"
34+
"go.uber.org/zap"
3435
)
3536

3637
type listOptions struct {
@@ -62,8 +63,8 @@ func runList(curveadm *cli.CurveAdm, options listOptions) error {
6263
storage := curveadm.Storage()
6364
clusters, err := storage.GetClusters("%")
6465
if err != nil {
65-
log.Error("Get clusters failed",
66-
log.Field("error", err))
66+
zaplog.Error("Get clusters failed",
67+
zap.Any("error", err))
6768
return errno.ERR_GET_ALL_CLUSTERS_FAILED.E(err)
6869
}
6970

cli/command/cluster/remove.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ import (
3030
"github.com/opencurve/curveadm/internal/errno"
3131
tui "github.com/opencurve/curveadm/internal/tui/common"
3232
cliutil "github.com/opencurve/curveadm/internal/utils"
33-
log "github.com/opencurve/curveadm/pkg/log/glg"
33+
"github.com/opencurve/curveadm/pkg/log/zaplog"
3434
"github.com/spf13/cobra"
35+
"go.uber.org/zap"
3536
)
3637

3738
type removeOptions struct {
@@ -86,8 +87,8 @@ func runRemove(curveadm *cli.CurveAdm, options removeOptions) error {
8687
clusterName := options.clusterName
8788
clusters, err := storage.GetClusters(clusterName) // Get all clusters
8889
if err != nil {
89-
log.Error("Get cluster failed",
90-
log.Field("error", err))
90+
zaplog.Error("Get cluster failed",
91+
zap.Any("error", err))
9192
return errno.ERR_GET_ALL_CLUSTERS_FAILED.E(err)
9293
} else if len(clusters) == 0 {
9394
return errno.ERR_CLUSTER_NOT_FOUND.

internal/configure/client.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ package configure
2525
import (
2626
"bytes"
2727
"fmt"
28+
"github.com/opencurve/curveadm/pkg/log/zaplog"
29+
"go.uber.org/zap"
2830
"strings"
2931

3032
"github.com/opencurve/curveadm/internal/build"
3133
"github.com/opencurve/curveadm/internal/configure/topology"
3234
"github.com/opencurve/curveadm/internal/errno"
3335
"github.com/opencurve/curveadm/internal/utils"
34-
log "github.com/opencurve/curveadm/pkg/log/glg"
3536
"github.com/opencurve/curveadm/pkg/variable"
3637
"github.com/spf13/viper"
3738
)
@@ -104,7 +105,7 @@ func NewClientConfig(config map[string]interface{}) (*ClientConfig, error) {
104105
vars.Register(variable.Variable{Name: "prefix", Value: "/curvebs/nebd"})
105106
err := vars.Build()
106107
if err != nil {
107-
log.Error("Build variables failed", log.Field("Error", err))
108+
zaplog.Error("Build variables failed", zap.Any("Error", err))
108109
return nil, errno.ERR_RESOLVE_CLIENT_VARIABLE_FAILED.E(err)
109110
}
110111

internal/configure/curveadm/curveadm.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ const (
5454

5555
type (
5656
CurveAdmConfig struct {
57-
LogLevel string
58-
SudoAlias string
59-
Timeout int
60-
AutoUpgrade bool
61-
SSHRetries int
62-
SSHTimeout int
57+
LogLevel string
58+
SudoAlias string
59+
Timeout int
60+
AutoUpgrade bool
61+
ShowLine bool
62+
LogInConsole bool
63+
SSHRetries int
64+
SSHTimeout int
6365
}
6466

6567
CurveAdm struct {
@@ -72,12 +74,14 @@ var (
7274
GlobalCurveAdmConfig *CurveAdmConfig
7375

7476
defaultCurveAdmConfig = &CurveAdmConfig{
75-
LogLevel: "error",
76-
SudoAlias: "sudo",
77-
Timeout: 180,
78-
AutoUpgrade: true,
79-
SSHRetries: 3,
80-
SSHTimeout: 10,
77+
LogLevel: "info",
78+
SudoAlias: "sudo",
79+
Timeout: 180,
80+
AutoUpgrade: true,
81+
ShowLine: true,
82+
LogInConsole: true,
83+
SSHRetries: 3,
84+
SSHTimeout: 10,
8185
}
8286

8387
SUPPORT_LOG_LEVEL = map[string]bool{

internal/configure/topology/dc.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ package topology
2626

2727
import (
2828
"fmt"
29+
"github.com/opencurve/curveadm/pkg/log/zaplog"
30+
"go.uber.org/zap"
2931
"strconv"
3032

3133
"github.com/opencurve/curveadm/internal/build"
3234
"github.com/opencurve/curveadm/internal/errno"
3335
"github.com/opencurve/curveadm/internal/utils"
34-
log "github.com/opencurve/curveadm/pkg/log/glg"
3536
"github.com/opencurve/curveadm/pkg/variable"
3637
)
3738

@@ -156,8 +157,8 @@ func NewDeployConfig(ctx *Context, kind, role, host, name string, replicas int,
156157
func (dc *DeployConfig) renderVariables() error {
157158
vars := dc.GetVariables()
158159
if err := vars.Build(); err != nil {
159-
log.Error("Build variables failed",
160-
log.Field("error", err))
160+
zaplog.Error("Build variables failed",
161+
zap.Any("error", err))
161162
return errno.ERR_RESOLVE_VARIABLE_FAILED.E(err)
162163
}
163164

@@ -257,8 +258,8 @@ func (dc *DeployConfig) ResolveHost() error {
257258

258259
vars := dc.GetVariables()
259260
if err := vars.Build(); err != nil {
260-
log.Error("Build variables failed",
261-
log.Field("error", err))
261+
zaplog.Error("Build variables failed",
262+
zap.Any("error", err))
262263
return errno.ERR_RESOLVE_VARIABLE_FAILED.E(err)
263264
}
264265

0 commit comments

Comments
 (0)