add logger component
This commit is contained in:
@ -1,10 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/go-gost/core/logger"
|
||||
"github.com/go-gost/core/service"
|
||||
"github.com/go-gost/x/api"
|
||||
@ -17,15 +13,14 @@ import (
|
||||
hosts_parser "github.com/go-gost/x/config/parsing/hosts"
|
||||
ingress_parser "github.com/go-gost/x/config/parsing/ingress"
|
||||
limiter_parser "github.com/go-gost/x/config/parsing/limiter"
|
||||
logger_parser "github.com/go-gost/x/config/parsing/logger"
|
||||
recorder_parser "github.com/go-gost/x/config/parsing/recorder"
|
||||
resolver_parser "github.com/go-gost/x/config/parsing/resolver"
|
||||
router_parser "github.com/go-gost/x/config/parsing/router"
|
||||
sd_parser "github.com/go-gost/x/config/parsing/sd"
|
||||
service_parser "github.com/go-gost/x/config/parsing/service"
|
||||
xlogger "github.com/go-gost/x/logger"
|
||||
metrics "github.com/go-gost/x/metrics/service"
|
||||
"github.com/go-gost/x/registry"
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
)
|
||||
|
||||
func buildService(cfg *config.Config) (services []service.Service) {
|
||||
@ -35,6 +30,14 @@ func buildService(cfg *config.Config) (services []service.Service) {
|
||||
|
||||
log := logger.Default()
|
||||
|
||||
for _, loggerCfg := range cfg.Loggers {
|
||||
if lg := logger_parser.ParseLogger(loggerCfg); lg != nil {
|
||||
if err := registry.LoggerRegistry().Register(loggerCfg.Name, lg); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, autherCfg := range cfg.Authers {
|
||||
if auther := auth_parser.ParseAuther(autherCfg); auther != nil {
|
||||
if err := registry.AutherRegistry().Register(autherCfg.Name, auther); err != nil {
|
||||
@ -171,48 +174,6 @@ func buildService(cfg *config.Config) (services []service.Service) {
|
||||
return
|
||||
}
|
||||
|
||||
func logFromConfig(cfg *config.LogConfig) logger.Logger {
|
||||
if cfg == nil {
|
||||
cfg = &config.LogConfig{}
|
||||
}
|
||||
opts := []xlogger.LoggerOption{
|
||||
xlogger.FormatLoggerOption(logger.LogFormat(cfg.Format)),
|
||||
xlogger.LevelLoggerOption(logger.LogLevel(cfg.Level)),
|
||||
}
|
||||
|
||||
var out io.Writer = os.Stderr
|
||||
switch cfg.Output {
|
||||
case "none", "null":
|
||||
return xlogger.Nop()
|
||||
case "stdout":
|
||||
out = os.Stdout
|
||||
case "stderr", "":
|
||||
out = os.Stderr
|
||||
default:
|
||||
if cfg.Rotation != nil {
|
||||
out = &lumberjack.Logger{
|
||||
Filename: cfg.Output,
|
||||
MaxSize: cfg.Rotation.MaxSize,
|
||||
MaxAge: cfg.Rotation.MaxAge,
|
||||
MaxBackups: cfg.Rotation.MaxBackups,
|
||||
LocalTime: cfg.Rotation.LocalTime,
|
||||
Compress: cfg.Rotation.Compress,
|
||||
}
|
||||
} else {
|
||||
os.MkdirAll(filepath.Dir(cfg.Output), 0755)
|
||||
f, err := os.OpenFile(cfg.Output, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
logger.Default().Warn(err)
|
||||
} else {
|
||||
out = f
|
||||
}
|
||||
}
|
||||
}
|
||||
opts = append(opts, xlogger.OutputLoggerOption(out))
|
||||
|
||||
return xlogger.NewLogger(opts...)
|
||||
}
|
||||
|
||||
func buildAPIService(cfg *config.APIConfig) (service.Service, error) {
|
||||
auther := auth_parser.ParseAutherFromAuth(cfg.Auth)
|
||||
if cfg.Auther != "" {
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"github.com/go-gost/core/logger"
|
||||
"github.com/go-gost/x/config"
|
||||
"github.com/go-gost/x/config/parsing"
|
||||
logger_parser "github.com/go-gost/x/config/parsing/logger"
|
||||
xmetrics "github.com/go-gost/x/metrics"
|
||||
"github.com/go-gost/x/registry"
|
||||
"github.com/judwhite/go-svc"
|
||||
@ -73,7 +74,11 @@ func (p *program) Init(env svc.Environment) error {
|
||||
}
|
||||
}
|
||||
|
||||
logger.SetDefault(logFromConfig(cfg.Log))
|
||||
logCfg := cfg.Log
|
||||
if logCfg == nil {
|
||||
logCfg = &config.LogConfig{}
|
||||
}
|
||||
logger.SetDefault(logger_parser.ParseLogger(&config.LoggerConfig{Log: logCfg}))
|
||||
|
||||
if outputFormat != "" {
|
||||
if err := cfg.Write(os.Stdout, outputFormat); err != nil {
|
||||
@ -171,6 +176,7 @@ func (p *program) mergeConfig(cfg1, cfg2 *config.Config) *config.Config {
|
||||
Limiters: append(cfg1.Limiters, cfg2.Limiters...),
|
||||
CLimiters: append(cfg1.CLimiters, cfg2.CLimiters...),
|
||||
RLimiters: append(cfg1.RLimiters, cfg2.RLimiters...),
|
||||
Loggers: append(cfg1.Loggers, cfg2.Loggers...),
|
||||
TLS: cfg1.TLS,
|
||||
Log: cfg1.Log,
|
||||
API: cfg1.API,
|
||||
|
Reference in New Issue
Block a user