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:
parent
7dd884d3da
commit
d829d2ab12
@ -97,7 +97,6 @@ func loadConfig() (*config.Server, *soju.Config, error) {
|
||||
MaxUserNetworks: raw.MaxUserNetworks,
|
||||
MultiUpstream: raw.MultiUpstream,
|
||||
UpstreamUserIPs: raw.UpstreamUserIPs,
|
||||
Debug: debug,
|
||||
MOTD: motd,
|
||||
}
|
||||
return raw, cfg, nil
|
||||
@ -140,6 +139,7 @@ func main() {
|
||||
|
||||
srv := soju.NewServer(db)
|
||||
srv.SetConfig(serverCfg)
|
||||
srv.Logger = soju.NewLogger(log.Writer(), debug)
|
||||
|
||||
for _, listen := range cfg.Listen {
|
||||
listenURI := listen
|
||||
|
9
conn.go
9
conn.go
@ -156,9 +156,7 @@ func newConn(srv *Server, ic ircConn, options *connOptions) *conn {
|
||||
break
|
||||
}
|
||||
|
||||
if c.srv.Config().Debug {
|
||||
c.logger.Printf("sent: %v", msg)
|
||||
}
|
||||
c.logger.Debugf("sent: %v", msg)
|
||||
c.conn.SetWriteDeadline(time.Now().Add(writeTimeout))
|
||||
if err := c.conn.WriteMessage(msg); err != nil {
|
||||
c.logger.Printf("failed to write message: %v", err)
|
||||
@ -210,10 +208,7 @@ func (c *conn) ReadMessage() (*irc.Message, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if c.srv.Config().Debug {
|
||||
c.logger.Printf("received: %v", msg)
|
||||
}
|
||||
|
||||
c.logger.Debugf("received: %v", msg)
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
|
28
server.go
28
server.go
@ -38,6 +38,26 @@ var backlogLimit = 4000
|
||||
|
||||
type Logger 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 {
|
||||
@ -52,6 +72,11 @@ func (l *prefixLogger) Printf(format string, v ...interface{}) {
|
||||
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 {
|
||||
v int64 // atomic
|
||||
}
|
||||
@ -72,7 +97,6 @@ type Config struct {
|
||||
Hostname string
|
||||
Title string
|
||||
LogPath string
|
||||
Debug bool
|
||||
HTTPOrigins []string
|
||||
AcceptProxyIPs config.IPSet
|
||||
MaxUserNetworks int
|
||||
@ -109,7 +133,7 @@ type Server struct {
|
||||
|
||||
func NewServer(db Database) *Server {
|
||||
srv := &Server{
|
||||
Logger: log.New(log.Writer(), "", log.LstdFlags),
|
||||
Logger: NewLogger(log.Writer(), true),
|
||||
db: db,
|
||||
listeners: make(map[net.Listener]struct{}),
|
||||
users: make(map[string]*user),
|
||||
|
Loading…
Reference in New Issue
Block a user