Refactor log

This commit is contained in:
世界
2022-07-12 15:17:29 +08:00
parent b47f3adbb3
commit 4fc763cfa2
46 changed files with 760 additions and 457 deletions

View File

@@ -5,10 +5,10 @@ import (
"os"
"github.com/sagernet/sing-box"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/goccy/go-json"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
@@ -21,17 +21,17 @@ var commandCheck = &cobra.Command{
func checkConfiguration(cmd *cobra.Command, args []string) {
configContent, err := os.ReadFile(configPath)
if err != nil {
logrus.Fatal("read config: ", err)
log.Fatal("read config: ", err)
}
var options option.Options
err = json.Unmarshal(configContent, &options)
if err != nil {
logrus.Fatal("decode config: ", err)
log.Fatal("decode config: ", err)
}
ctx, cancel := context.WithCancel(context.Background())
_, err = box.New(ctx, options)
if err != nil {
logrus.Fatal("create service: ", err)
log.Fatal("create service: ", err)
}
cancel()
}

View File

@@ -5,10 +5,10 @@ import (
"os"
"path/filepath"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/goccy/go-json"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
@@ -27,19 +27,19 @@ func init() {
func formatConfiguration(cmd *cobra.Command, args []string) {
configContent, err := os.ReadFile(configPath)
if err != nil {
logrus.Fatal("read config: ", err)
log.Fatal("read config: ", err)
}
var options option.Options
err = json.Unmarshal(configContent, &options)
if err != nil {
logrus.Fatal("decode config: ", err)
log.Fatal("decode config: ", err)
}
buffer := new(bytes.Buffer)
encoder := json.NewEncoder(buffer)
encoder.SetIndent("", " ")
err = encoder.Encode(options)
if err != nil {
logrus.Fatal("encode config: ", err)
log.Fatal("encode config: ", err)
}
if !commandFormatFlagWrite {
os.Stdout.WriteString(buffer.String() + "\n")
@@ -50,12 +50,12 @@ func formatConfiguration(cmd *cobra.Command, args []string) {
}
output, err := os.Create(configPath)
if err != nil {
logrus.Fatal("open output: ", err)
log.Fatal("open output: ", err)
}
_, err = output.Write(buffer.Bytes())
output.Close()
if err != nil {
logrus.Fatal("write output: ", err)
log.Fatal("write output: ", err)
}
outputPath, _ := filepath.Abs(configPath)
os.Stderr.WriteString(outputPath + "\n")

View File

@@ -7,10 +7,10 @@ import (
"syscall"
"github.com/sagernet/sing-box"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/goccy/go-json"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
@@ -23,12 +23,12 @@ var commandRun = &cobra.Command{
func run(cmd *cobra.Command, args []string) {
configContent, err := os.ReadFile(configPath)
if err != nil {
logrus.Fatal("read config: ", err)
log.Fatal("read config: ", err)
}
var options option.Options
err = json.Unmarshal(configContent, &options)
if err != nil {
logrus.Fatal("decode config: ", err)
log.Fatal("decode config: ", err)
}
if disableColor {
if options.Log == nil {
@@ -39,11 +39,11 @@ func run(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithCancel(context.Background())
instance, err := box.New(ctx, options)
if err != nil {
logrus.Fatal("create service: ", err)
log.Fatal("create service: ", err)
}
err = instance.Start()
if err != nil {
logrus.Fatal("start service: ", err)
log.Fatal("start service: ", err)
}
osSignals := make(chan os.Signal, 1)
signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM)

View File

@@ -5,15 +5,9 @@ import (
"github.com/sagernet/sing-box/log"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func init() {
logrus.StandardLogger().SetLevel(logrus.TraceLevel)
logrus.StandardLogger().SetFormatter(&log.LogrusTextFormatter{})
}
var (
configPath string
workingDir string
@@ -38,17 +32,14 @@ func init() {
func main() {
if err := mainCommand.Execute(); err != nil {
logrus.Fatal(err)
log.Fatal(err)
}
}
func preRun(cmd *cobra.Command, args []string) {
if disableColor {
logrus.StandardLogger().SetFormatter(&log.LogrusTextFormatter{DisableColors: true})
}
if workingDir != "" {
if err := os.Chdir(workingDir); err != nil {
logrus.Fatal(err)
log.Fatal(err)
}
}
}