add logger component
This commit is contained in:
parent
79c15f2c37
commit
c87faa2017
@ -79,6 +79,11 @@ type LogRotationConfig struct {
|
|||||||
Compress bool `yaml:"compress,omitempty" json:"compress,omitempty"`
|
Compress bool `yaml:"compress,omitempty" json:"compress,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type LoggerConfig struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Log *LogConfig `yaml:",omitempty" json:"log,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
type ProfilingConfig struct {
|
type ProfilingConfig struct {
|
||||||
Addr string `json:"addr"`
|
Addr string `json:"addr"`
|
||||||
}
|
}
|
||||||
@ -396,6 +401,7 @@ type ServiceConfig struct {
|
|||||||
Limiter string `yaml:",omitempty" json:"limiter,omitempty"`
|
Limiter string `yaml:",omitempty" json:"limiter,omitempty"`
|
||||||
CLimiter string `yaml:"climiter,omitempty" json:"climiter,omitempty"`
|
CLimiter string `yaml:"climiter,omitempty" json:"climiter,omitempty"`
|
||||||
RLimiter string `yaml:"rlimiter,omitempty" json:"rlimiter,omitempty"`
|
RLimiter string `yaml:"rlimiter,omitempty" json:"rlimiter,omitempty"`
|
||||||
|
Logger string `yaml:",omitempty" json:"logger,omitempty"`
|
||||||
Recorders []*RecorderObject `yaml:",omitempty" json:"recorders,omitempty"`
|
Recorders []*RecorderObject `yaml:",omitempty" json:"recorders,omitempty"`
|
||||||
Handler *HandlerConfig `yaml:",omitempty" json:"handler,omitempty"`
|
Handler *HandlerConfig `yaml:",omitempty" json:"handler,omitempty"`
|
||||||
Listener *ListenerConfig `yaml:",omitempty" json:"listener,omitempty"`
|
Listener *ListenerConfig `yaml:",omitempty" json:"listener,omitempty"`
|
||||||
@ -468,6 +474,7 @@ type Config struct {
|
|||||||
Limiters []*LimiterConfig `yaml:",omitempty" json:"limiters,omitempty"`
|
Limiters []*LimiterConfig `yaml:",omitempty" json:"limiters,omitempty"`
|
||||||
CLimiters []*LimiterConfig `yaml:"climiters,omitempty" json:"climiters,omitempty"`
|
CLimiters []*LimiterConfig `yaml:"climiters,omitempty" json:"climiters,omitempty"`
|
||||||
RLimiters []*LimiterConfig `yaml:"rlimiters,omitempty" json:"rlimiters,omitempty"`
|
RLimiters []*LimiterConfig `yaml:"rlimiters,omitempty" json:"rlimiters,omitempty"`
|
||||||
|
Loggers []*LoggerConfig `yaml:",omitempty" json:"loggers,omitempty"`
|
||||||
TLS *TLSConfig `yaml:",omitempty" json:"tls,omitempty"`
|
TLS *TLSConfig `yaml:",omitempty" json:"tls,omitempty"`
|
||||||
Log *LogConfig `yaml:",omitempty" json:"log,omitempty"`
|
Log *LogConfig `yaml:",omitempty" json:"log,omitempty"`
|
||||||
Profiling *ProfilingConfig `yaml:",omitempty" json:"profiling,omitempty"`
|
Profiling *ProfilingConfig `yaml:",omitempty" json:"profiling,omitempty"`
|
||||||
|
55
config/parsing/logger/parse.go
Normal file
55
config/parsing/logger/parse.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
package logger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/go-gost/core/logger"
|
||||||
|
"github.com/go-gost/x/config"
|
||||||
|
xlogger "github.com/go-gost/x/logger"
|
||||||
|
"gopkg.in/natefinch/lumberjack.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ParseLogger(cfg *config.LoggerConfig) logger.Logger {
|
||||||
|
if cfg == nil || cfg.Log == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
opts := []xlogger.Option{
|
||||||
|
xlogger.NameOption(cfg.Name),
|
||||||
|
xlogger.FormatOption(logger.LogFormat(cfg.Log.Format)),
|
||||||
|
xlogger.LevelOption(logger.LogLevel(cfg.Log.Level)),
|
||||||
|
}
|
||||||
|
|
||||||
|
var out io.Writer = os.Stderr
|
||||||
|
switch cfg.Log.Output {
|
||||||
|
case "none", "null":
|
||||||
|
return xlogger.Nop()
|
||||||
|
case "stdout":
|
||||||
|
out = os.Stdout
|
||||||
|
case "stderr", "":
|
||||||
|
out = os.Stderr
|
||||||
|
default:
|
||||||
|
if cfg.Log.Rotation != nil {
|
||||||
|
out = &lumberjack.Logger{
|
||||||
|
Filename: cfg.Log.Output,
|
||||||
|
MaxSize: cfg.Log.Rotation.MaxSize,
|
||||||
|
MaxAge: cfg.Log.Rotation.MaxAge,
|
||||||
|
MaxBackups: cfg.Log.Rotation.MaxBackups,
|
||||||
|
LocalTime: cfg.Log.Rotation.LocalTime,
|
||||||
|
Compress: cfg.Log.Rotation.Compress,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
os.MkdirAll(filepath.Dir(cfg.Log.Output), 0755)
|
||||||
|
f, err := os.OpenFile(cfg.Log.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.OutputOption(out))
|
||||||
|
|
||||||
|
return xlogger.NewLogger(opts...)
|
||||||
|
}
|
@ -41,7 +41,12 @@ func ParseService(cfg *config.ServiceConfig) (service.Service, error) {
|
|||||||
Type: "auto",
|
Type: "auto",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
serviceLogger := logger.Default().WithFields(map[string]any{
|
|
||||||
|
log := registry.LoggerRegistry().Get(cfg.Logger)
|
||||||
|
if log == nil {
|
||||||
|
log = logger.Default()
|
||||||
|
}
|
||||||
|
serviceLogger := log.WithFields(map[string]any{
|
||||||
"kind": "service",
|
"kind": "service",
|
||||||
"service": cfg.Name,
|
"service": cfg.Name,
|
||||||
"listener": cfg.Listener.Type,
|
"listener": cfg.Listener.Type,
|
||||||
|
2
go.mod
2
go.mod
@ -10,7 +10,7 @@ require (
|
|||||||
github.com/go-gost/core v0.0.0-20231119081403-abc73f2ca2b7
|
github.com/go-gost/core v0.0.0-20231119081403-abc73f2ca2b7
|
||||||
github.com/go-gost/gosocks4 v0.0.1
|
github.com/go-gost/gosocks4 v0.0.1
|
||||||
github.com/go-gost/gosocks5 v0.4.0
|
github.com/go-gost/gosocks5 v0.4.0
|
||||||
github.com/go-gost/plugin v0.0.0-20231119081435-96a9cabbf6b6
|
github.com/go-gost/plugin v0.0.0-20231119084331-d49a1cb23b3b
|
||||||
github.com/go-gost/relay v0.4.1-0.20230916134211-828f314ddfe7
|
github.com/go-gost/relay v0.4.1-0.20230916134211-828f314ddfe7
|
||||||
github.com/go-gost/tls-dissector v0.0.2-0.20220408131628-aac992c27451
|
github.com/go-gost/tls-dissector v0.0.2-0.20220408131628-aac992c27451
|
||||||
github.com/go-redis/redis/v8 v8.11.5
|
github.com/go-redis/redis/v8 v8.11.5
|
||||||
|
2
go.sum
2
go.sum
@ -105,6 +105,8 @@ github.com/go-gost/plugin v0.0.0-20231119062132-d959ab54847f h1:V5m5plmwkIt16B25
|
|||||||
github.com/go-gost/plugin v0.0.0-20231119062132-d959ab54847f/go.mod h1:qXr2Zm9Ex2ATqnWuNUzVZqySPMnuIihvblYZt4MlZLw=
|
github.com/go-gost/plugin v0.0.0-20231119062132-d959ab54847f/go.mod h1:qXr2Zm9Ex2ATqnWuNUzVZqySPMnuIihvblYZt4MlZLw=
|
||||||
github.com/go-gost/plugin v0.0.0-20231119081435-96a9cabbf6b6 h1:nkeo0TCEZVz74eZlVBDM6xiqDjS3DGWFRBOJ6kiDudU=
|
github.com/go-gost/plugin v0.0.0-20231119081435-96a9cabbf6b6 h1:nkeo0TCEZVz74eZlVBDM6xiqDjS3DGWFRBOJ6kiDudU=
|
||||||
github.com/go-gost/plugin v0.0.0-20231119081435-96a9cabbf6b6/go.mod h1:qXr2Zm9Ex2ATqnWuNUzVZqySPMnuIihvblYZt4MlZLw=
|
github.com/go-gost/plugin v0.0.0-20231119081435-96a9cabbf6b6/go.mod h1:qXr2Zm9Ex2ATqnWuNUzVZqySPMnuIihvblYZt4MlZLw=
|
||||||
|
github.com/go-gost/plugin v0.0.0-20231119084331-d49a1cb23b3b h1:ZmnYutflq+KOZK+Px5RDckorDSxTYlkT4aQbjTC8/C4=
|
||||||
|
github.com/go-gost/plugin v0.0.0-20231119084331-d49a1cb23b3b/go.mod h1:qXr2Zm9Ex2ATqnWuNUzVZqySPMnuIihvblYZt4MlZLw=
|
||||||
github.com/go-gost/relay v0.4.1-0.20230916134211-828f314ddfe7 h1:qAG1OyjvdA5h221CfFSS3J359V3d2E7dJWyP29QoDSI=
|
github.com/go-gost/relay v0.4.1-0.20230916134211-828f314ddfe7 h1:qAG1OyjvdA5h221CfFSS3J359V3d2E7dJWyP29QoDSI=
|
||||||
github.com/go-gost/relay v0.4.1-0.20230916134211-828f314ddfe7/go.mod h1:lcX+23LCQ3khIeASBo+tJ/WbwXFO32/N5YN6ucuYTG8=
|
github.com/go-gost/relay v0.4.1-0.20230916134211-828f314ddfe7/go.mod h1:lcX+23LCQ3khIeASBo+tJ/WbwXFO32/N5YN6ucuYTG8=
|
||||||
github.com/go-gost/tls-dissector v0.0.2-0.20220408131628-aac992c27451 h1:xj8gUZGYO3nb5+6Bjw9+tsFkA9sYynrOvDvvC4uDV2I=
|
github.com/go-gost/tls-dissector v0.0.2-0.20220408131628-aac992c27451 h1:xj8gUZGYO3nb5+6Bjw9+tsFkA9sYynrOvDvvC4uDV2I=
|
||||||
|
@ -10,28 +10,35 @@ import (
|
|||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type LoggerOptions struct {
|
type Options struct {
|
||||||
|
Name string
|
||||||
Output io.Writer
|
Output io.Writer
|
||||||
Format logger.LogFormat
|
Format logger.LogFormat
|
||||||
Level logger.LogLevel
|
Level logger.LogLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
type LoggerOption func(opts *LoggerOptions)
|
type Option func(opts *Options)
|
||||||
|
|
||||||
func OutputLoggerOption(out io.Writer) LoggerOption {
|
func NameOption(name string) Option {
|
||||||
return func(opts *LoggerOptions) {
|
return func(opts *Options) {
|
||||||
|
opts.Name = name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func OutputOption(out io.Writer) Option {
|
||||||
|
return func(opts *Options) {
|
||||||
opts.Output = out
|
opts.Output = out
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func FormatLoggerOption(format logger.LogFormat) LoggerOption {
|
func FormatOption(format logger.LogFormat) Option {
|
||||||
return func(opts *LoggerOptions) {
|
return func(opts *Options) {
|
||||||
opts.Format = format
|
opts.Format = format
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func LevelLoggerOption(level logger.LogLevel) LoggerOption {
|
func LevelOption(level logger.LogLevel) Option {
|
||||||
return func(opts *LoggerOptions) {
|
return func(opts *Options) {
|
||||||
opts.Level = level
|
opts.Level = level
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -40,8 +47,8 @@ type logrusLogger struct {
|
|||||||
logger *logrus.Entry
|
logger *logrus.Entry
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLogger(opts ...LoggerOption) logger.Logger {
|
func NewLogger(opts ...Option) logger.Logger {
|
||||||
var options LoggerOptions
|
var options Options
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
opt(&options)
|
opt(&options)
|
||||||
}
|
}
|
||||||
|
13
registry/logger.go
Normal file
13
registry/logger.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package registry
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/go-gost/core/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
type loggerRegistry struct {
|
||||||
|
registry[logger.Logger]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *loggerRegistry) Register(name string, v logger.Logger) error {
|
||||||
|
return r.registry.Register(name, v)
|
||||||
|
}
|
@ -15,6 +15,7 @@ import (
|
|||||||
"github.com/go-gost/core/limiter/conn"
|
"github.com/go-gost/core/limiter/conn"
|
||||||
"github.com/go-gost/core/limiter/rate"
|
"github.com/go-gost/core/limiter/rate"
|
||||||
"github.com/go-gost/core/limiter/traffic"
|
"github.com/go-gost/core/limiter/traffic"
|
||||||
|
"github.com/go-gost/core/logger"
|
||||||
"github.com/go-gost/core/recorder"
|
"github.com/go-gost/core/recorder"
|
||||||
reg "github.com/go-gost/core/registry"
|
reg "github.com/go-gost/core/registry"
|
||||||
"github.com/go-gost/core/resolver"
|
"github.com/go-gost/core/resolver"
|
||||||
@ -49,6 +50,8 @@ var (
|
|||||||
ingressReg reg.Registry[ingress.Ingress] = new(ingressRegistry)
|
ingressReg reg.Registry[ingress.Ingress] = new(ingressRegistry)
|
||||||
routerReg reg.Registry[router.Router] = new(routerRegistry)
|
routerReg reg.Registry[router.Router] = new(routerRegistry)
|
||||||
sdReg reg.Registry[sd.SD] = new(sdRegistry)
|
sdReg reg.Registry[sd.SD] = new(sdRegistry)
|
||||||
|
|
||||||
|
loggerReg reg.Registry[logger.Logger] = new(loggerRegistry)
|
||||||
)
|
)
|
||||||
|
|
||||||
type registry[T any] struct {
|
type registry[T any] struct {
|
||||||
@ -175,3 +178,7 @@ func RouterRegistry() reg.Registry[router.Router] {
|
|||||||
func SDRegistry() reg.Registry[sd.SD] {
|
func SDRegistry() reg.Registry[sd.SD] {
|
||||||
return sdReg
|
return sdReg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func LoggerRegistry() reg.Registry[logger.Logger] {
|
||||||
|
return loggerReg
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user