mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-11-01 06:50:00 +00:00
[bugfix] Trim log entries to 1700 chars before they enter syslog (#493)
* start implementing trimming hook * add test with very long test * test syslog w/ unix socket + long (trimmed) msg * trim long entries with trimhook * trim to 1700 chars instead
This commit is contained in:
parent
2259838108
commit
728c4a5e38
4 changed files with 122 additions and 3 deletions
|
@ -73,7 +73,7 @@ func Initialize() error {
|
|||
return err
|
||||
}
|
||||
|
||||
logrus.AddHook(hook)
|
||||
logrus.AddHook(&trimHook{hook})
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
File diff suppressed because one or more lines are too long
53
internal/log/trimhook.go
Normal file
53
internal/log/trimhook.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
GoToSocial
|
||||
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package log
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// trimHook is a wrapper round a logrus hook that trims the *entry.Message
|
||||
// to no more than 1700 characters before sending it through to the wrapped hook,
|
||||
// to avoid spamming syslog with messages that are too long for it.
|
||||
type trimHook struct {
|
||||
wrappedHook logrus.Hook
|
||||
}
|
||||
|
||||
func (t *trimHook) Fire(e *logrus.Entry) error {
|
||||
// only copy/truncate if we need to
|
||||
if len(e.Message) < 1700 {
|
||||
return t.wrappedHook.Fire(e)
|
||||
}
|
||||
|
||||
// it's too long, truncate + fire a copy of the entry so we don't meddle with the original
|
||||
return t.wrappedHook.Fire(&logrus.Entry{
|
||||
Logger: e.Logger,
|
||||
Data: e.Data,
|
||||
Time: e.Time,
|
||||
Level: e.Level,
|
||||
Caller: e.Caller,
|
||||
Message: e.Message[:1696] + "...", // truncate
|
||||
Buffer: e.Buffer,
|
||||
Context: e.Context,
|
||||
})
|
||||
}
|
||||
|
||||
func (t *trimHook) Levels() []logrus.Level {
|
||||
return t.wrappedHook.Levels()
|
||||
}
|
|
@ -53,3 +53,26 @@ func InitTestSyslog() (*syslog.Server, chan format.LogParts, error) {
|
|||
|
||||
return server, channel, nil
|
||||
}
|
||||
|
||||
// InitTestSyslog returns a test syslog running on a unix socket, and a channel for reading
|
||||
// messages sent to the server, or an error if something goes wrong.
|
||||
//
|
||||
// Callers of this function should call Kill() on the server when they're finished with it!
|
||||
func InitTestSyslogUnixgram(address string) (*syslog.Server, chan format.LogParts, error) {
|
||||
channel := make(syslog.LogPartsChannel)
|
||||
handler := syslog.NewChannelHandler(channel)
|
||||
|
||||
server := syslog.NewServer()
|
||||
server.SetFormat(syslog.Automatic)
|
||||
server.SetHandler(handler)
|
||||
|
||||
if err := server.ListenUnixgram(address); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if err := server.Boot(); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return server, channel, nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue