From 8821506fb1602389c10e529ae48d97d6b6e389a0 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sat, 7 Dec 2024 22:18:07 +0900 Subject: [PATCH] let http.Server use the main app logger --- client.go | 17 +++++++++++++++++ cmd/config.go | 11 +++++++---- cmd/main.go | 8 ++++++-- hodu.go | 1 + server.go | 17 +++++++++++++++++ 5 files changed, 48 insertions(+), 6 deletions(-) diff --git a/client.go b/client.go index 09ad307..fd99aff 100644 --- a/client.go +++ b/client.go @@ -4,6 +4,7 @@ import "context" import "crypto/tls" import "errors" import "fmt" +import "log" import "math/rand" import "net" import "net/http" @@ -970,9 +971,21 @@ func (cts *ClientConn) ReportEvent (route_id uint32, pts_id uint32, event_type P // -------------------------------------------------------------------- +type client_ctl_log_writer struct { + cli *Client +} + +func (hlw *client_ctl_log_writer) Write(p []byte) (n int, err error) { + // the standard http.Server always requires *log.Logger + // use this iowriter to create a logger to pass it to the http server. + hlw.cli.log.Write("", LOG_INFO, string(p)) + return len(p), nil +} + func NewClient(ctx context.Context, ctl_addrs []string, logger Logger, ctltlscfg *tls.Config, rpctlscfg *tls.Config) *Client { var c Client var i int + var hs_log *log.Logger c.ctx, c.ctx_cancel = context.WithCancel(ctx) c.ctltlscfg = ctltlscfg @@ -996,11 +1009,15 @@ func NewClient(ctx context.Context, ctl_addrs []string, logger Logger, ctltlscfg c.ctl_addr = make([]string, len(ctl_addrs)) c.ctl = make([]*http.Server, len(ctl_addrs)) copy(c.ctl_addr, ctl_addrs) + + hs_log = log.New(&client_ctl_log_writer{cli: &c}, "", 0); + for i = 0; i < len(ctl_addrs); i++ { c.ctl[i] = &http.Server{ Addr: ctl_addrs[i], Handler: c.ctl_mux, TLSConfig: c.ctltlscfg, + ErrorLog: hs_log, // TODO: more settings } } diff --git a/cmd/config.go b/cmd/config.go index 323e42d..a2e727a 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -42,20 +42,23 @@ type ClientTLSConfig struct { type ServerConfig struct { CTL struct { - TLS ServerTLSConfig `yaml:"tls"` + TLS ServerTLSConfig `yaml:"tls"` + ServiceAddrs []string `yaml:"service-addrs"` } `yaml:"ctl"` RPC struct { - TLS ServerTLSConfig `yaml:"tls"` + TLS ServerTLSConfig `yaml:"tls"` + ServiceAddrs []string `yaml:"service-addrs"` } `yaml:"rpc"` } type ClientConfig struct { CTL struct { - TLS ServerTLSConfig `yaml:"tls"` + TLS ServerTLSConfig `yaml:"tls"` + ServiceAddrs []string `yaml:"service-addrs"` } `yaml:"ctl"` RPC struct { - TLS ClientTLSConfig `yaml:"tls"` + TLS ClientTLSConfig `yaml:"tls"` } `yaml:"rpc"` } diff --git a/cmd/main.go b/cmd/main.go index 54c98f3..41ff135 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -251,7 +251,7 @@ func main() { goto wrong_usage } - if len(rpc_addrs) < 1 || flgs.NArg() > 0 { + if len(rpc_addrs) <= 0 || flgs.NArg() > 0 { goto wrong_usage } @@ -263,6 +263,8 @@ func main() { } } + if len(ctl_addrs) <= 0 { ctl_addrs = cfg.CTL.ServiceAddrs } + err = server_main(ctl_addrs, rpc_addrs, cfg) if err != nil { fmt.Fprintf(os.Stderr, "ERROR: server error - %s\n", err.Error()) @@ -297,7 +299,7 @@ func main() { goto wrong_usage } - if len(rpc_addrs) < 1 { + if len(rpc_addrs) <= 0 { goto wrong_usage } @@ -309,6 +311,8 @@ func main() { } } + if len(ctl_addrs) < 1 { ctl_addrs = cfg.CTL.ServiceAddrs } + err = client_main(ctl_addrs, rpc_addrs[0], flgs.Args(), cfg) if err != nil { fmt.Fprintf(os.Stderr, "ERROR: client error - %s\n", err.Error()) diff --git a/hodu.go b/hodu.go index 97b2bdb..bb4ef60 100644 --- a/hodu.go +++ b/hodu.go @@ -24,3 +24,4 @@ type Service interface { WaitForTermination() // blocking. must wait until all services are stopped WriteLog(id string, level LogLevel, fmtstr string, args ...interface{}) } + diff --git a/server.go b/server.go index b9061ca..63b8ee4 100644 --- a/server.go +++ b/server.go @@ -5,6 +5,7 @@ import "crypto/tls" import "errors" import "fmt" import "io" +import "log" import "math/rand" import "net" import "net/http" @@ -794,6 +795,18 @@ func unaryInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServ return v, err } + +type server_ctl_log_writer struct { + svr *Server +} + +func (hlw *server_ctl_log_writer) Write(p []byte) (n int, err error) { + // the standard http.Server always requires *log.Logger + // use this iowriter to create a logger to pass it to the http server. + hlw.svr.log.Write("", LOG_INFO, string(p)) + return len(p), nil +} + func NewServer(ctx context.Context, ctl_addrs []string, rpc_addrs []string, logger Logger, ctltlscfg *tls.Config, rpctlscfg *tls.Config) (*Server, error) { var s Server var l *net.TCPListener @@ -803,6 +816,7 @@ func NewServer(ctx context.Context, ctl_addrs []string, rpc_addrs []string, logg var gl *net.TCPListener var i int var cwd string + var hs_log *log.Logger if len(rpc_addrs) <= 0 { return nil, fmt.Errorf("no server addresses provided") @@ -871,11 +885,14 @@ func NewServer(ctx context.Context, ctl_addrs []string, rpc_addrs []string, logg s.ctl_addr = make([]string, len(ctl_addrs)) s.ctl = make([]*http.Server, len(ctl_addrs)) copy(s.ctl_addr, ctl_addrs) + hs_log = log.New(&server_ctl_log_writer{svr: &s}, "", 0); + for i = 0; i < len(ctl_addrs); i++ { s.ctl[i] = &http.Server{ Addr: ctl_addrs[i], Handler: s.ctl_mux, TLSConfig: s.ctltlscfg, + ErrorLog: hs_log, // TODO: more settings } }