Skip to content

Commit

Permalink
feat: log level config
Browse files Browse the repository at this point in the history
  • Loading branch information
joyme123 committed Aug 12, 2023
1 parent c6ecca9 commit 19f948a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
go.lsp.dev/protocol v0.12.0
go.lsp.dev/uri v0.3.0
go.uber.org/zap v1.21.0
gopkg.in/yaml.v2 v2.2.8
)

require (
Expand Down
5 changes: 2 additions & 3 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import (
log "github.com/sirupsen/logrus"
)

func Init() {

func Init(logLevel int) {
file := os.TempDir() + "/thriftls.log"
logFile, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
if err != nil {
panic(err)
}
log.SetLevel(log.DebugLevel)
log.SetLevel(log.Level(logLevel))
log.SetOutput(logFile) // 将文件设置为log输出的文件
}

Expand Down
38 changes: 36 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"errors"
"flag"
"io"
"math/rand"
"os"
Expand All @@ -12,13 +13,18 @@ import (
"github.com/joyme123/thrift-ls/lsp"
"go.lsp.dev/jsonrpc2"
"go.lsp.dev/pkg/fakenet"
"gopkg.in/yaml.v2"
)

type Options struct{}
type Options struct {
LogLevel int `yaml:"logLevel"` // 1: fatal, 2: error, 3: warn, 4: info, 5: debug, 6: trace
}

func main() {
rand.Seed(time.Now().UnixMilli())
log.Init()

opts := configInit()
log.Init(opts.LogLevel)

ctx := context.Background()
// server := &lsp.Server{}
Expand All @@ -38,3 +44,31 @@ func main() {
}
panic(err)
}

func configInit() *Options {
logLevel := -1
flag.IntVar(&logLevel, "logLevel", -1, "set log level")
flag.Parse()

dir, err := os.UserHomeDir()
if err != nil {
dir = os.TempDir()
}
dir = dir + "/.thriftls"
configFile := dir + "/config.yaml"
opts := &Options{}

data, err := os.ReadFile(configFile)
if err == nil {
yaml.Unmarshal(data, opts)
}

if logLevel >= 0 {
opts.LogLevel = logLevel // flag can override config file
}
if opts.LogLevel == 0 {
opts.LogLevel = 3
}

return opts
}

0 comments on commit 19f948a

Please sign in to comment.