Logging done better

This commit is contained in:
Alex 2020-02-27 10:35:09 +01:00
parent 6bbf08af8f
commit 19975e981e
5 changed files with 20 additions and 22 deletions

View file

@ -215,7 +215,7 @@ func (a *Account) joinedInternal(roomId RoomID) error {
return err
}
log.Debugf("Joined %s (%s)\n", roomId, a.MatrixUser)
log.Tracef("Joined %s (%s)\n", roomId, a.MatrixUser)
err = mx.RoomInvite(mx_room_id, a.MatrixUser)
if err != nil && strings.Contains(err.Error(), "already in the room") {
@ -248,7 +248,7 @@ func (a *Account) leftInternal(roomId RoomID) error {
return err
}
log.Printf("Left %s (%s)\n", roomId, a.MatrixUser)
log.Tracef("Left %s (%s)\n", roomId, a.MatrixUser)
err = mx.RoomKick(mx_room_id, a.MatrixUser, fmt.Sprintf("got leave room event on %s", a.Protocol))
if err != nil && strings.Contains(err.Error(), "not in the room") {
@ -366,7 +366,7 @@ func (a *Account) eventInternal(event *Event) error {
}
if event.Type == EVENT_JOIN {
log.Printf("%s join %s %s", a.Protocol, event.Author, event.Room)
log.Tracef("%s join %s %s", a.Protocol, event.Author, event.Room)
mx_room_id, err := dbGetMxRoom(a.Protocol, event.Room)
if err != nil {
return err
@ -382,7 +382,7 @@ func (a *Account) eventInternal(event *Event) error {
return mx.RoomJoinAs(mx_room_id, mx_user_id)
} else if event.Type == EVENT_LEAVE {
log.Printf("%s join %s %s", a.Protocol, event.Author, event.Room)
log.Tracef("%s join %s %s", a.Protocol, event.Author, event.Room)
mx_room_id, err := dbGetMxRoom(a.Protocol, event.Room)
if err != nil {
return err
@ -390,7 +390,7 @@ func (a *Account) eventInternal(event *Event) error {
return mx.RoomLeaveAs(mx_room_id, mx_user_id)
} else {
log.Printf("%s msg %s %s", a.Protocol, event.Author, event.Room)
log.Tracef("%s msg %s %s", a.Protocol, event.Author, event.Room)
mx_room_id := ""
if len(event.Room) > 0 {

View file

@ -109,8 +109,6 @@ func (mm *Mattermost) Configure(c Configuration) error {
go mm.conn.WsReceiver()
go mm.conn.StatusLoop()
fmt.Printf("CLIENT4: %#v\n", mm.conn.Client)
for i := 0; i < 42; i++ {
time.Sleep(time.Duration(1) * time.Second)
if mm.conn.WsConnected {
@ -166,7 +164,6 @@ func (mm *Mattermost) reverseRoomId(id string) (bool, RoomID) {
teamName := mm.conn.GetTeamName(team)
if u, ok := mm.teams[teamName]; ok && u {
name := mm.conn.GetChannelName(id)
fmt.Printf("CHANNEL NAME: %s TEAM: %s\n", name, teamName)
return true, RoomID(fmt.Sprintf("%s@%s@%s", name, teamName, mm.server))
} else {
return false, ""
@ -329,7 +326,7 @@ func (mm *Mattermost) handleConnected() {
mm.caches.sentjoined = make(map[string]bool)
mm.caches.displayname = make(map[UserID]string)
fmt.Printf("Connected to mattermost\n")
log.Debugf("Connected to mattermost: %s@%s\n", mm.username, mm.server)
// Handle incoming messages
go mm.handleLoop(mm.conn.MessageChan, mm.handlerStopChan)
@ -468,8 +465,8 @@ func (mm *Mattermost) handleLoop(msgCh chan *matterclient.Message, quitCh chan b
case <-quitCh:
break
case msg := <-msgCh:
fmt.Printf("Mattermost: %#v\n", msg)
fmt.Printf("Mattermost raw: %#v\n", msg.Raw)
log.Tracef("Mattermost: %#v\n", msg)
log.Tracef("Mattermost raw: %#v\n", msg.Raw)
err := mm.handlePosted(msg.Raw)
if err != nil {
log.Warnf("Mattermost error: %s", err)

10
db.go
View file

@ -180,7 +180,7 @@ func dbGetMxRoom(protocol string, roomId connector.RoomID) (string, error) {
mx_room_id, err = mx.CreateRoom(name, alias, []string{})
if err != nil {
log.Printf("Could not create room for %s: %s", name, err)
log.Warnf("Could not create room for %s: %s", name, err)
return "", err
}
}
@ -192,7 +192,7 @@ func dbGetMxRoom(protocol string, roomId connector.RoomID) (string, error) {
}
db.Create(&room)
}
log.Debugf("Got room id: %s", room.MxRoomID)
log.Tracef("Got room id for %s %s: %s", protocol, roomId, room.MxRoomID)
return room.MxRoomID, nil
}
@ -215,7 +215,7 @@ func dbGetMxPmRoom(protocol string, them connector.UserID, themMxId string, usMx
mx_room_id, err := mx.CreateDirectRoomAs([]string{usMxId}, themMxId)
if err != nil {
log.Printf("Could not create room for %s: %s", name, err)
log.Warnf("Could not create room for %s: %s", name, err)
return "", err
}
@ -234,7 +234,7 @@ func dbGetMxPmRoom(protocol string, them connector.UserID, themMxId string, usMx
}
db.Create(&room)
}
log.Debugf("Got PM room id: %s", room.MxRoomID)
log.Tracef("Got PM room id for %s %s %s %s: %s", usMxId, protocol, usAccount, them, room.MxRoomID)
return room.MxRoomID, nil
}
@ -256,7 +256,7 @@ func dbGetMxUser(protocol string, userId connector.UserID) (string, error) {
err := mx.RegisterUser(username)
if err != nil {
if mxE, ok := err.(*mxlib.MxError); !ok || mxE.ErrCode != "M_USER_IN_USE" {
log.Printf("Could not register %s: %s", username, err)
log.Warnf("Could not register %s: %s", username, err)
return "", err
}
}

View file

@ -50,7 +50,7 @@ func (mx *Client) PutApiCall(endpoint string, data interface{}, response interfa
return err
}
log.Debugf("Matrix PUT request: %s %s\n", endpoint, string(body))
log.Debugf("Matrix PUT request: %s\n", endpoint)
req, err := http.NewRequest("PUT", mx.Server+endpoint, bytes.NewBuffer(body))
if err != nil {
@ -67,7 +67,7 @@ func (mx *Client) PostApiCall(endpoint string, data interface{}, response interf
return err
}
log.Debugf("Matrix POST request: %s %s\n", endpoint, string(body))
log.Debugf("Matrix POST request: %s\n", endpoint)
req, err := http.NewRequest("POST", mx.Server+endpoint, bytes.NewBuffer(body))
if err != nil {
@ -94,7 +94,7 @@ func (mx *Client) DoAndParse(req *http.Request, response interface{}) error {
if err != nil {
return err
}
log.Debugf("Response (%d): %#v\n", resp.StatusCode, e)
log.Debugf("Response: %d %#v\n", resp.StatusCode, e)
return &e
}
@ -103,7 +103,7 @@ func (mx *Client) DoAndParse(req *http.Request, response interface{}) error {
return err
}
log.Debugf("Response: %#v\n", response)
log.Debugf("Response: 200 OK")
return nil
}

View file

@ -65,7 +65,7 @@ func checkTokenAndLog(handler http.Handler) http.Handler {
return
}
log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, strings.Split(r.URL, "?")[0])
handler.ServeHTTP(w, r)
})
}
@ -80,7 +80,8 @@ func handleTxn(w http.ResponseWriter, r *http.Request) {
return
}
log.Debugf("Got transaction %#v\n", txn)
log.Debugf("Processing transaction %s (%d events)", mux.Vars(r)["txnId"], len(txn.Events))
log.Tracef("Transaction content: %#v\n", txn)
for i := range txn.Events {
ev := &txn.Events[i]