Add Logger.Debugf

Makes it easy to print debugging messages which aren't targeted at
the user. See [1] for motivation.

[1]: https://dave.cheney.net/2015/11/05/lets-talk-about-logging
This commit is contained in:
Simon Ser 2021-12-06 18:35:49 +01:00
parent 7dd884d3da
commit d829d2ab12
3 changed files with 29 additions and 10 deletions

View File

@ -97,7 +97,6 @@ func loadConfig() (*config.Server, *soju.Config, error) {
MaxUserNetworks: raw.MaxUserNetworks, MaxUserNetworks: raw.MaxUserNetworks,
MultiUpstream: raw.MultiUpstream, MultiUpstream: raw.MultiUpstream,
UpstreamUserIPs: raw.UpstreamUserIPs, UpstreamUserIPs: raw.UpstreamUserIPs,
Debug: debug,
MOTD: motd, MOTD: motd,
} }
return raw, cfg, nil return raw, cfg, nil
@ -140,6 +139,7 @@ func main() {
srv := soju.NewServer(db) srv := soju.NewServer(db)
srv.SetConfig(serverCfg) srv.SetConfig(serverCfg)
srv.Logger = soju.NewLogger(log.Writer(), debug)
for _, listen := range cfg.Listen { for _, listen := range cfg.Listen {
listenURI := listen listenURI := listen

View File

@ -156,9 +156,7 @@ func newConn(srv *Server, ic ircConn, options *connOptions) *conn {
break break
} }
if c.srv.Config().Debug { c.logger.Debugf("sent: %v", msg)
c.logger.Printf("sent: %v", msg)
}
c.conn.SetWriteDeadline(time.Now().Add(writeTimeout)) c.conn.SetWriteDeadline(time.Now().Add(writeTimeout))
if err := c.conn.WriteMessage(msg); err != nil { if err := c.conn.WriteMessage(msg); err != nil {
c.logger.Printf("failed to write message: %v", err) c.logger.Printf("failed to write message: %v", err)
@ -210,10 +208,7 @@ func (c *conn) ReadMessage() (*irc.Message, error) {
return nil, err return nil, err
} }
if c.srv.Config().Debug { c.logger.Debugf("received: %v", msg)
c.logger.Printf("received: %v", msg)
}
return msg, nil return msg, nil
} }

View File

@ -38,6 +38,26 @@ var backlogLimit = 4000
type Logger interface { type Logger interface {
Printf(format string, v ...interface{}) Printf(format string, v ...interface{})
Debugf(format string, v ...interface{})
}
type logger struct {
*log.Logger
debug bool
}
func (l logger) Debugf(format string, v ...interface{}) {
if !l.debug {
return
}
l.Logger.Printf(format, v...)
}
func NewLogger(out io.Writer, debug bool) Logger {
return logger{
Logger: log.New(log.Writer(), "", log.LstdFlags),
debug: debug,
}
} }
type prefixLogger struct { type prefixLogger struct {
@ -52,6 +72,11 @@ func (l *prefixLogger) Printf(format string, v ...interface{}) {
l.logger.Printf("%v"+format, v...) l.logger.Printf("%v"+format, v...)
} }
func (l *prefixLogger) Debugf(format string, v ...interface{}) {
v = append([]interface{}{l.prefix}, v...)
l.logger.Debugf("%v"+format, v...)
}
type int64Gauge struct { type int64Gauge struct {
v int64 // atomic v int64 // atomic
} }
@ -72,7 +97,6 @@ type Config struct {
Hostname string Hostname string
Title string Title string
LogPath string LogPath string
Debug bool
HTTPOrigins []string HTTPOrigins []string
AcceptProxyIPs config.IPSet AcceptProxyIPs config.IPSet
MaxUserNetworks int MaxUserNetworks int
@ -109,7 +133,7 @@ type Server struct {
func NewServer(db Database) *Server { func NewServer(db Database) *Server {
srv := &Server{ srv := &Server{
Logger: log.New(log.Writer(), "", log.LstdFlags), Logger: NewLogger(log.Writer(), true),
db: db, db: db,
listeners: make(map[net.Listener]struct{}), listeners: make(map[net.Listener]struct{}),
users: make(map[string]*user), users: make(map[string]*user),