improve logger
This commit is contained in:
parent
16ddfba1d1
commit
24c33e6194
@ -1,16 +1,45 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/go-gost/gost/pkg/chain"
|
||||
"github.com/go-gost/gost/pkg/components/connector"
|
||||
"github.com/go-gost/gost/pkg/components/dialer"
|
||||
"github.com/go-gost/gost/pkg/components/handler"
|
||||
"github.com/go-gost/gost/pkg/components/listener"
|
||||
"github.com/go-gost/gost/pkg/config"
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
"github.com/go-gost/gost/pkg/registry"
|
||||
"github.com/go-gost/gost/pkg/service"
|
||||
)
|
||||
|
||||
func logFromConfig(cfg *config.LogConfig) logger.Logger {
|
||||
opts := []logger.LoggerOption{
|
||||
logger.FormatLoggerOption(logger.LogFormat(cfg.Format)),
|
||||
logger.LevelLoggerOption(logger.LogLevel(cfg.Level)),
|
||||
}
|
||||
|
||||
var out io.Writer = os.Stderr
|
||||
switch cfg.Output {
|
||||
case "stdout":
|
||||
out = os.Stdout
|
||||
case "stderr", "":
|
||||
out = os.Stderr
|
||||
default:
|
||||
f, err := os.OpenFile(cfg.Output, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
log.Warnf("log", err)
|
||||
} else {
|
||||
out = f
|
||||
}
|
||||
}
|
||||
opts = append(opts, logger.OutputLoggerOption(out))
|
||||
|
||||
return logger.NewLogger(opts...)
|
||||
}
|
||||
|
||||
func buildService(cfg *config.Config) (services []*service.Service) {
|
||||
if cfg == nil || len(cfg.Services) == 0 {
|
||||
return
|
||||
@ -21,7 +50,15 @@ func buildService(cfg *config.Config) (services []*service.Service) {
|
||||
for _, svc := range cfg.Services {
|
||||
s := &service.Service{}
|
||||
|
||||
ln := registry.GetListener(svc.Listener.Type)(listener.AddrOption(svc.Addr))
|
||||
ln := registry.GetListener(svc.Listener.Type)(
|
||||
listener.AddrOption(svc.Addr),
|
||||
listener.LoggerOption(
|
||||
log.WithFields(map[string]interface{}{
|
||||
"kind": "listener",
|
||||
"type": svc.Listener.Type,
|
||||
}),
|
||||
),
|
||||
)
|
||||
ln.Init(listener.Metadata(svc.Listener.Metadata))
|
||||
s.WithListener(ln)
|
||||
|
||||
@ -32,7 +69,15 @@ func buildService(cfg *config.Config) (services []*service.Service) {
|
||||
break
|
||||
}
|
||||
}
|
||||
h := registry.GetHandler(svc.Handler.Type)(handler.ChainOption(chain))
|
||||
h := registry.GetHandler(svc.Handler.Type)(
|
||||
handler.ChainOption(chain),
|
||||
handler.LoggerOption(
|
||||
log.WithFields(map[string]interface{}{
|
||||
"kind": "handler",
|
||||
"type": svc.Handler.Type,
|
||||
}),
|
||||
),
|
||||
)
|
||||
h.Init(handler.Metadata(svc.Handler.Metadata))
|
||||
s.WithHandler(h)
|
||||
|
||||
@ -58,11 +103,25 @@ func buildChain(cfg *config.Config) (chains []*chain.Chain) {
|
||||
|
||||
tr := &chain.Transport{}
|
||||
|
||||
cr := registry.GetConnector(v.Connector.Type)()
|
||||
cr := registry.GetConnector(v.Connector.Type)(
|
||||
connector.LoggerOption(
|
||||
log.WithFields(map[string]interface{}{
|
||||
"kind": "connector",
|
||||
"type": v.Connector.Type,
|
||||
}),
|
||||
),
|
||||
)
|
||||
cr.Init(connector.Metadata(v.Connector.Metadata))
|
||||
tr.WithConnector(cr)
|
||||
|
||||
d := registry.GetDialer(v.Dialer.Type)()
|
||||
d := registry.GetDialer(v.Dialer.Type)(
|
||||
dialer.LoggerOption(
|
||||
log.WithFields(map[string]interface{}{
|
||||
"kind": "dialer",
|
||||
"type": v.Dialer.Type,
|
||||
}),
|
||||
),
|
||||
)
|
||||
d.Init(dialer.Metadata(v.Dialer.Metadata))
|
||||
tr.WithDialer(d)
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
log:
|
||||
level: info # debug, info, warn, error, fatal
|
||||
format: text # text, json
|
||||
output: stderr # stderr, stdout, /path/to/file
|
||||
level: debug # debug, info, warn, error, fatal
|
||||
format: json # text, json
|
||||
|
||||
services:
|
||||
- url: "http://gost:gost@:8000"
|
||||
@ -15,7 +16,7 @@ services:
|
||||
keepAlive: 15s
|
||||
username: gost
|
||||
password: gost
|
||||
chain: chain01
|
||||
# chain: chain01
|
||||
|
||||
chains:
|
||||
- name: chain01
|
||||
|
@ -5,14 +5,17 @@ import (
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log := logger.NewLogger("main")
|
||||
log.EnableJSONOutput(true)
|
||||
var (
|
||||
log = logger.NewLogger()
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfg := &config.Config{}
|
||||
if err := cfg.Load(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log = logFromConfig(cfg.Log)
|
||||
|
||||
services := buildService(cfg)
|
||||
for _, svc := range services {
|
||||
go svc.Run()
|
||||
|
Loading…
Reference in New Issue
Block a user