Skip to content

Commit

Permalink
optimize log and support hide log time and path (#1346)
Browse files Browse the repository at this point in the history
  • Loading branch information
bxy4543 authored Apr 28, 2022
1 parent fc43622 commit f9c3d99
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 39 deletions.
2 changes: 1 addition & 1 deletion apply/processor/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ type CreateProcessor struct {
func (c *CreateProcessor) GetPipeLine() ([]func(cluster *v2.Cluster) error, error) {
var todoList []func(cluster *v2.Cluster) error
todoList = append(todoList,
c.MountImage,
c.PreProcess,
c.GetPhasePluginFunc(plugin.PhaseOriginally),
c.MountImage,
c.RunConfig,
c.MountRootfs,
c.GetPhasePluginFunc(plugin.PhasePreInit),
Expand Down
16 changes: 10 additions & 6 deletions logger/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var levelPrefix = [LevelTrace + 1]string{
}

const (
logTimeDefaultFormat = "2006-01-02 15:04:05" // 日志输出默认格式
LogTimeDefaultFormat = "2006-01-02 15:04:05" // 日志输出默认格式
AdapterConsole = "console" // 控制台输出配置项
AdapterFile = "file" // 文件输出配置项
AdapterConn = "conn" // 网络输出配置项
Expand Down Expand Up @@ -136,7 +136,7 @@ func NewLogger(depth ...int) *LocalLogger {
l.callDepth = dep
l.usePath = true
//l.SetLogger(AdapterConsole)
l.timeFormat = logTimeDefaultFormat
//l.timeFormat = logTimeDefaultFormat
return l
}

Expand Down Expand Up @@ -168,14 +168,12 @@ func Cfg(debugMod bool) {
cfg, err := json.Marshal(&logCfg)
if err == nil {
SetLogger(string(cfg))
SetLogPath(true)
return
}
}
}

SetLogger(fmt.Sprintf(`{
"TimeFormat": "2006-01-02 15:04:05",
"Console": {
"level": "",
"color": true,
Expand All @@ -194,8 +192,6 @@ func Cfg(debugMod bool) {
}}`,
logLev, common.DefaultLogDir, time.Now().Format("2006-01-02"),
))

SetLogPath(true)
}

func (localLog *LocalLogger) SetLogger(adapterName string, configs ...string) {
Expand Down Expand Up @@ -260,6 +256,10 @@ func (localLog *LocalLogger) SetLogPath(bPath bool) {
localLog.usePath = bPath
}

func (localLog *LocalLogger) SetTimeFormat(format string) {
localLog.timeFormat = format
}

func (localLog *LocalLogger) writeToLoggers(when time.Time, msg *loginfo, level logLevel) {
for _, l := range localLog.outputs {
if l.name == AdapterConn {
Expand Down Expand Up @@ -403,6 +403,10 @@ func SetLogPath(show bool) {
defaultLogger.SetLogPath(show)
}

func SetTimeFormat(format string) {
defaultLogger.SetTimeFormat(format)
}

// param 可以是log配置文件名,也可以是log配置内容,默认DEBUG输出到控制台
func SetLogger(param ...string) {
if len(param) == 0 {
Expand Down
1 change: 1 addition & 0 deletions pkg/plugin/hostname_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,6 @@ func (h HostnamePlugin) changeNodeName(hostname, ip string, SSH ssh.Interface) e
if err := SSH.CmdAsync(ip, tmpCMD, perCMD); err != nil {
return fmt.Errorf("failed to change the node %v hostname,%v", ip, err)
}
logger.Info("successfully changed node %s hostname to %s.", ip, hostname)
return nil
}
1 change: 1 addition & 0 deletions pkg/plugin/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func (l LabelsNodes) Run(context Context, phase Phase) error {
if _, err := l.client.UpdateNode(v); err != nil {
return fmt.Errorf("current cluster nodes label failed, %v", err)
}
logger.Info("successfully added node %s labels %v.", internalIP, labels)
}
}
return nil
Expand Down
1 change: 1 addition & 0 deletions pkg/plugin/taint_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (l *Taint) Run(context Context, phase Phase) (err error) {
if err != nil {
return err
}
logger.Info("successfully updated node %s taints to %v.", v.Address, updateTaints)
}
break
}
Expand Down
13 changes: 10 additions & 3 deletions sealer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
type rootOpts struct {
cfgFile string
debugModeOn bool
hideLogTime bool
hideLogPath bool
}

var rootOpt rootOpts
Expand All @@ -53,6 +55,8 @@ func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&rootOpt.cfgFile, "config", "", "config file (default is $HOME/.sealer.json)")
rootCmd.PersistentFlags().BoolVarP(&rootOpt.debugModeOn, "debug", "d", false, "turn on debug mode")
rootCmd.PersistentFlags().BoolVar(&rootOpt.hideLogTime, "hide-time", false, "hide the log time")
rootCmd.PersistentFlags().BoolVar(&rootOpt.hideLogPath, "hide-path", false, "hide the log path")
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.DisableAutoGenTag = true
}
Expand All @@ -70,9 +74,12 @@ func initConfig() {

viper.AutomaticEnv() // read in environment variables that match

logger.InitLogger(logger.Config{
DebugMode: rootOpt.debugModeOn,
})
logger.InitLogger(logger.Config{DebugMode: rootOpt.debugModeOn})

logger.SetLogPath(!rootOpt.hideLogPath)

if !rootOpt.hideLogTime {
logger.SetTimeFormat(logger.LogTimeDefaultFormat)
}
logger.Cfg(rootOpt.debugModeOn)
}
31 changes: 11 additions & 20 deletions test/sealer_images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@ var _ = Describe("sealer image", func() {
image.RemoveImageList(tagImageNames)
})

/* //not support
By("tag by image id", func() {
imageID := image.GetImageID(settings.TestImageName)
image.TagImageList(imageID, tagImageNames)
image.DoImageOps(settings.SubCmdListOfSealer, "")
image.RemoveImageList(tagImageNames)
})*/
By("tag by image id", func() {
imageID := image.GetImageID(settings.TestImageName)
image.TagImageList(imageID, tagImageNames)
image.DoImageOps(settings.SubCmdListOfSealer, "")
image.RemoveImageList(tagImageNames)
})

By("remove tag image", func() {
tagImageName := "e2e_images_test:v0.3"
Expand Down Expand Up @@ -118,18 +117,10 @@ var _ = Describe("sealer image", func() {
AfterEach(func() {
registry.Logout()
})
pushImageNames := []string{
"registry.cn-qingdao.aliyuncs.com/sealer-io/e2e_image_test:v0.01",
"sealer-io/e2e_image_test:v0.01",
"e2e_image_test:v0.01",
}

for _, pushImage := range pushImageNames {
pushImage := pushImage
It(fmt.Sprintf("push image %s", pushImage), func() {
image.TagImages(settings.TestImageName, pushImage)
image.DoImageOps(settings.SubCmdPushOfSealer, pushImage)
})
}
It("push image", func() {
pushImageName := "registry.cn-qingdao.aliyuncs.com/sealer-io/e2e_image_test:v0.01"
image.TagImages(settings.TestImageName, pushImageName)
image.DoImageOps(settings.SubCmdPushOfSealer, pushImageName)
})
})
})
13 changes: 4 additions & 9 deletions test/suites/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/onsi/gomega"

Expand All @@ -47,7 +45,7 @@ func GetContainerBuildDir() string {

// GetTestImageName return specific image name that will be push to registry
func GetTestImageName() string {
return fmt.Sprintf("sealer-io/%s%d:%s", settings.ImageName, 719, "v1")
return fmt.Sprintf("registry.cn-qingdao.aliyuncs.com/sealer-io/%s%d:%s", settings.ImageName, 719, "v1")
}

type ArgsOfBuild struct {
Expand Down Expand Up @@ -94,12 +92,9 @@ func NewArgsOfBuild() *ArgsOfBuild {
}

func CheckIsImageExist(imageName string) bool {
cmd := fmt.Sprintf("%s images | grep %s | wc -l", settings.DefaultSealerBin, imageName)
result, err := utils.RunSimpleCmd(cmd)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
num, err := strconv.Atoi(strings.Replace(result, "\n", "", -1))
gomega.Expect(err).NotTo(gomega.HaveOccurred())
return num == 1
cmd := fmt.Sprintf("%s inspect %s", settings.DefaultSealerBin, imageName)
_, err := utils.RunSimpleCmd(cmd)
return err == nil
}

func UpdateKubeFromImage(imageName string, KubefilePath string) {
Expand Down

0 comments on commit f9c3d99

Please sign in to comment.