From 52fa083eaef13e86446953866743d9fa059f6a40 Mon Sep 17 00:00:00 2001 From: Geno Date: Tue, 14 Sep 2021 09:31:45 +0200 Subject: [PATCH] gateway: improve logging to be more privat --- gateway/config_example.toml | 7 ++++++- gateway/main.go | 11 +++++++++++ gateway/xmpp.go | 29 +++++++++-------------------- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/gateway/config_example.toml b/gateway/config_example.toml index beaf098..d044b42 100644 --- a/gateway/config_example.toml +++ b/gateway/config_example.toml @@ -1,8 +1,13 @@ endpoint_url = "https://up.chat.sum7.eu" +jwt_secret = "CHANGEME" + +[log] +level = 40 +timestamp = false [webserver] listen = ":8080" -access_log = true +access_log = false webroot = "" # if ACME / Let's Encrypt used, uncomment listen diff --git a/gateway/main.go b/gateway/main.go index c9b8a1c..7cef80d 100644 --- a/gateway/main.go +++ b/gateway/main.go @@ -8,6 +8,7 @@ import ( apiStatus "dev.sum7.eu/genofire/golang-lib/web/api/status" webM "dev.sum7.eu/genofire/golang-lib/web/metrics" "github.com/bdlm/log" + "github.com/bdlm/std/logger" ) var VERSION = "development" @@ -17,6 +18,10 @@ type configData struct { EndpointURL string `toml:"endpoint_url"` XMPP XMPPService `toml:"xmpp"` Webserver web.Service `toml:"webserver"` + Log struct { + Level uint32 `toml:"level"` + Timestamp bool `toml:"timestamp"` + } `toml:"log"` } func main() { @@ -41,6 +46,12 @@ func main() { if err := file.ReadTOML(configPath, config); err != nil { log.Panicf("open config file: %s", err) } + + log.SetLevel(logger.Level(config.Log.Level)) + log.SetFormatter(&log.TextFormatter{ + DisableTimestamp: !config.Log.Timestamp, + }) + // just for more beautiful config file - jere config.XMPP.EndpointURL = config.EndpointURL config.XMPP.JWTSecret = config.JWTSecret diff --git a/gateway/xmpp.go b/gateway/xmpp.go index 1eb0564..f2d44a9 100644 --- a/gateway/xmpp.go +++ b/gateway/xmpp.go @@ -42,20 +42,15 @@ func (s *XMPPService) Run() error { return err } defer func() { - log.Info("Closing session…") + log.Info("closing xmpp connection") if err := s.session.Close(); err != nil { log.Errorf("Error closing session: %q", err) } - log.Println("Closing conn…") + log.Info("closing xmpp connection") if err := s.session.Conn().Close(); err != nil { log.Errorf("Error closing connection: %q", err) } }() - /* Send initial presence to let the server know we want to receive messages. - err = s.session.Send(context.TODO(), stanza.Presence{Type: stanza.AvailablePresence}.Wrap(nil)) - if err != nil { - return err - }*/ log.Infof("connected with %s", s.session.LocalAddr()) return s.session.Serve(mux.New( // register - get + set @@ -66,12 +61,6 @@ func (s *XMPPService) Run() error { mux.IQFunc(stanza.GetIQ, xml.Name{Local: messages.LocalUnregister, Space: messages.Space}, s.handleUnregister), // mux.IQFunc("", xml.Name{}, s.handleDisco), )) - /* - - return s.session.Serve(xmpp.HandlerFunc(func(t xmlstream.TokenReadEncoder, start *xml.StartElement) error { - log.Info(start) - return nil - })) - */ } func (s *XMPPService) handleRegister(iq stanza.IQ, t xmlstream.TokenReadEncoder, start *xml.StartElement) error { @@ -93,13 +82,13 @@ func (s *XMPPService) handleRegister(iq stanza.IQ, t xmlstream.TokenReadEncoder, tokenData := messages.TokenData{} err := xml.NewTokenDecoder(t).Decode(&tokenData) if err != nil && err != io.EOF { - log.Errorf("decoding message: %q", err) + log.Warnf("decoding message: %q", err) reply.Register.Error = &messages.ErrorData{Body: "unable decode"} return nil } token := tokenData.Body if token == "" { - log.Errorf("no token found: %v", token) + log.Warnf("no token found: %v", token) reply.Register.Error = &messages.ErrorData{Body: "no token"} return nil } @@ -112,7 +101,7 @@ func (s *XMPPService) handleRegister(iq stanza.IQ, t xmlstream.TokenReadEncoder, endpoint := s.EndpointURL + "/UP?token=" + jwt reply.IQ.Type = stanza.ResultIQ reply.Register.Endpoint = &messages.EndpointData{Body: endpoint} - log.Infof("generate respone: %v", endpoint) + log.Debugf("generate respone: %v", endpoint) return nil } func (s *XMPPService) handleUnregister(iq stanza.IQ, t xmlstream.TokenReadEncoder, start *xml.StartElement) error { @@ -129,7 +118,7 @@ func (s *XMPPService) handleUnregister(iq stanza.IQ, t xmlstream.TokenReadEncode log.Errorf("sending unregister response: %v", err) } }() - log.Infof("unregistered unhandled: %v", start) + log.Debugf("unregistered unhandled: %v", start) reply.Unregister.Error = "not implemented" return nil @@ -147,16 +136,16 @@ func (s *XMPPService) handleDisco(iq stanza.IQ, t xmlstream.TokenReadEncoder, st log.Errorf("sending response: %v", err) } }() - log.Infof("recieved iq: %v", iq) + log.Debugf("recieved iq: %v", iq) return nil } // SendMessage of an UP Notification func (s *XMPPService) SendMessage(to jid.JID, token, content string) error { log.WithFields(map[string]interface{}{ - "to": to, + "to": to.String(), "token": token, - }).Info("forward message to xmpp") + }).Debug("forward message to xmpp") return s.session.Encode(context.TODO(), messages.Message{ Message: stanza.Message{ To: to,