Skip to content

Commit

Permalink
修改日志库
Browse files Browse the repository at this point in the history
  • Loading branch information
lixueping committed Jan 31, 2024
1 parent c8298ed commit c5e2e32
Show file tree
Hide file tree
Showing 39 changed files with 369 additions and 634 deletions.
11 changes: 6 additions & 5 deletions cmd/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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
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,
Expand All @@ -16,11 +16,12 @@ limitations under the License.
package cmd

import (
"fmt"
"log/slog"
"os"
"strings"

"github.com/devopsxp/xp/pipeline"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -37,13 +38,13 @@ var copyCmd = &cobra.Command{
Long: `将src文件传输到远程目标主机dest路径,
eg: ./xp cli copy 127.0.0.1 -u lxp -S /tmp/123 -D /tmp/333`,
Run: func(cmd *cobra.Command, args []string) {
log.Debugf("Cli args: %v", args)
slog.Debug(fmt.Sprintf("Cli args: %v", args))
if cliSrc == "" || cliDest == "" {
log.Error("src or dest is not config")
slog.Error("src or dest is not config")
os.Exit(1)
}

log.Debugln("items", cliItem)
slog.Debug("items", cliItem)
var items []interface{}

if cliItem != "" {
Expand Down
11 changes: 6 additions & 5 deletions cmd/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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
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,
Expand All @@ -16,11 +16,12 @@ limitations under the License.
package cmd

import (
"fmt"
"log/slog"
"os"
"strings"

"github.com/devopsxp/xp/pipeline"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -31,13 +32,13 @@ var fetchCmd = &cobra.Command{
Long: `官方文档:https://docs.ansible.com/ansible/latest/modules/fetch_module.html#fetch-module
eg: ./xp cli fetch 127.0.0.1 -u lxp -S /tmp/123 -D /tmp/333`,
Run: func(cmd *cobra.Command, args []string) {
log.Debugf("Cli args: %v", args)
slog.Debug(fmt.Sprintf("Cli args: %v", args))
if cliSrc == "" || cliDest == "" {
log.Error("src or dest is not config")
slog.Error("src or dest is not config")
os.Exit(1)
}

log.Debugln("items", cliItem)
slog.Debug("items", cliItem)
var items []interface{}

if cliItem != "" {
Expand Down
9 changes: 5 additions & 4 deletions cmd/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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
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,
Expand All @@ -16,10 +16,11 @@ limitations under the License.
package cmd

import (
"fmt"
"log/slog"
"os"

"github.com/devopsxp/xp/pipeline"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -35,9 +36,9 @@ var imageCmd = &cobra.Command{
Short: "基于docker容器进行命令执行",
Long: `本机docker执行pipeline命令`,
Run: func(cmd *cobra.Command, args []string) {
log.Debugf("Cli args: %v", args)
slog.Debug(fmt.Sprintf("Cli args: %v", args))
if imageIE == "" {
log.Error("未检测到执行命令,请确认! [eg: ./xp cli image -i java:8 -a \"-v /tmp:/data -u root \" java -version && for i in {1..10};do echo `date`;sleep 1;done]")
slog.Error("未检测到执行命令,请确认! [eg: ./xp cli image -i java:8 -a \"-v /tmp:/data -u root \" java -version && for i in {1..10};do echo `date`;sleep 1;done]")
os.Exit(1)
}

Expand Down
7 changes: 4 additions & 3 deletions cmd/palybook.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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
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,
Expand All @@ -16,8 +16,9 @@ limitations under the License.
package cmd

import (
"log/slog"

"github.com/devopsxp/xp/pipeline"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -27,7 +28,7 @@ var playbookCmd = &cobra.Command{
Short: "开发测试模块",
Long: `测试ansile-playbook功能和pipeline流程管控`,
Run: func(cmd *cobra.Command, args []string) {
log.Debugln("playbook called")
slog.Debug("playbook called")
// fmt.Printf("%v %v\n", viper.GetStringSlice("stage"), viper.AllSettings())

// 根据yaml解析shell等模块,进行动态匹配,进行顺序执行
Expand Down
47 changes: 5 additions & 42 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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
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,
Expand All @@ -16,15 +16,12 @@ limitations under the License.
package cmd

import (
"fmt"
"log/slog"
"os"
"time"

"github.com/spf13/cobra"

_ "github.com/devopsxp/xp/module"
"github.com/devopsxp/xp/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -55,7 +52,7 @@ to quickly create a Cobra application.`,
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
log.Errorln(err)
slog.Error(err.Error())
os.Exit(1)
}
}
Expand All @@ -82,40 +79,6 @@ func init() {

// initConfig reads in config file and ENV variables if set.
func initConfig() {
customFormatter := new(log.TextFormatter)
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
// Log as JSON instead of the default ASCII formatter.
log.SetFormatter(customFormatter)
customFormatter.FullTimestamp = true // 显示时间
// customFormatter.ForceQuote = true // 强制格式匹配
// customFormatter.PadLevelText = true // 显示完整日志级别

// Output to stdout instead of the default stderr
// Can be any io.Writer, see below for File example
if islog {
file, err := os.OpenFile(fmt.Sprintf("%s.log", time.Now().Format("20060102150405")), os.O_WRONLY|os.O_APPEND|os.O_CREATE|os.O_SYNC, 0600)
if err != nil {
panic(err)
}

info, err := file.Stat()
if err != nil {
panic(err)
}

fileWriter := utils.LogFileWriter{file, info.Size()}
log.SetOutput(fileWriter)
} else {
log.SetOutput(os.Stdout)
}

// log format setting
if debug {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}

if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
Expand All @@ -136,8 +99,8 @@ func initConfig() {

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
log.Debugln("Using config file:", viper.ConfigFileUsed())
slog.Debug("Using config file:", viper.ConfigFileUsed())
} else {
log.Errorf("Using config file error: %s\n", err.Error())
slog.Error("Using config file error", "ERROR", err.Error())
}
}
11 changes: 6 additions & 5 deletions cmd/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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
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,
Expand All @@ -16,10 +16,11 @@ limitations under the License.
package cmd

import (
"fmt"
"log/slog"
"os"

"github.com/devopsxp/xp/pipeline"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -31,12 +32,12 @@ var scriptCmd = &cobra.Command{
Short: "远程执行脚本",
Long: `eg: ./xp cli script 127.0.0.1 -a "test.sh"`,
Run: func(cmd *cobra.Command, args []string) {
log.Debugf("Cli args: %v", args)
slog.Debug(fmt.Sprintf("Cli args: %v", args))
if script == "" {
log.Error("未指定需要执行的脚本路径")
slog.Error("未指定需要执行的脚本路径")
os.Exit(1)
} else if len(args) == 0 {
log.Error("未检测到目标主机,请确认! [eg: ./xp cli script 127.0.0.1-20 -a test.sh]")
slog.Error("未检测到目标主机,请确认! [eg: ./xp cli script 127.0.0.1-20 -a test.sh]")
os.Exit(1)
}

Expand Down
11 changes: 6 additions & 5 deletions cmd/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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
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,
Expand All @@ -16,10 +16,11 @@ limitations under the License.
package cmd

import (
"fmt"
"log/slog"
"os"

"github.com/devopsxp/xp/pipeline"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -34,12 +35,12 @@ var shellCmd = &cobra.Command{
Short: "执行远程命令",
Long: `example: ./xp cli shell 127.0.0.1-20 192.168.50.1-10 -a "zsh" -T -L console`,
Run: func(cmd *cobra.Command, args []string) {
log.Debugf("Cli args: %v", args)
slog.Debug(fmt.Sprintf("Cli args: %v", args))
if cliShell == "" {
log.Error("未检测到执行命令,请确认! [eg: ./xp cli shell 127.0.0.1-20 -a \"hostname\"]")
slog.Error("未检测到执行命令,请确认! [eg: ./xp cli shell 127.0.0.1-20 -a \"hostname\"]")
os.Exit(1)
} else if len(args) == 0 {
log.Error("未检测到目标主机,请确认! [eg: ./xp cli shell 127.0.0.1-20 -a \"hostname\"]")
slog.Error("未检测到目标主机,请确认! [eg: ./xp cli shell 127.0.0.1-20 -a \"hostname\"]")
os.Exit(1)
}
// TODO: 完成数据Message.Data模型拼装
Expand Down
11 changes: 6 additions & 5 deletions cmd/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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
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,
Expand All @@ -16,10 +16,11 @@ limitations under the License.
package cmd

import (
"fmt"
"log/slog"
"os"

"github.com/devopsxp/xp/pipeline"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -37,12 +38,12 @@ var systemdCmd = &cobra.Command{
Long: `官方文档:https://docs.ansible.com/ansible/latest/modules/service_module.html#service-module
eg: ./xp cli systemd -n docker -s restart -r -e`,
Run: func(cmd *cobra.Command, args []string) {
log.Debugf("Cli args: %v", args)
slog.Debug(fmt.Sprintf("Cli args: %v", args))
if systemdService == "" {
log.Error("未指定需要操作的服务名")
slog.Error("未指定需要操作的服务名")
os.Exit(1)
} else if len(args) == 0 {
log.Error("未检测到目标主机,请确认! [eg: ./xp cli systemd 127.0.0.1-20 -n docker -s status]")
slog.Error("未检测到目标主机,请确认! [eg: ./xp cli systemd 127.0.0.1-20 -n docker -s status]")
os.Exit(1)
}

Expand Down
9 changes: 5 additions & 4 deletions cmd/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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
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,
Expand All @@ -16,10 +16,11 @@ limitations under the License.
package cmd

import (
"fmt"
"log/slog"
"os"

"github.com/devopsxp/xp/pipeline"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -64,9 +65,9 @@ COMMITINFO: <font color="comment">{{.info}}</font>
{{end}}
#==============================DEMO=================================`,
Run: func(cmd *cobra.Command, args []string) {
log.Debugf("Cli args: %v Vars: %v", args, viper.GetStringMap("vars"))
slog.Debug(fmt.Sprintf("Cli args: %v Vars: %v", args, viper.GetStringMap("vars")))
if cliSrc == "" || cliDest == "" {
log.Error("src or dest is not config")
slog.Error("src or dest is not config")
os.Exit(1)
}

Expand Down
Loading

0 comments on commit c5e2e32

Please sign in to comment.