2022-02-07 09:42:07 +00:00
|
|
|
package supervisor
|
2021-11-08 15:43:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/connection"
|
|
|
|
"github.com/cloudflare/cloudflared/tunnelstate"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ConnAwareLogger struct {
|
|
|
|
tracker *tunnelstate.ConnTracker
|
|
|
|
logger *zerolog.Logger
|
|
|
|
}
|
|
|
|
|
2022-08-11 12:31:35 +00:00
|
|
|
func NewConnAwareLogger(logger *zerolog.Logger, tracker *tunnelstate.ConnTracker, observer *connection.Observer) *ConnAwareLogger {
|
2021-11-08 15:43:36 +00:00
|
|
|
connAwareLogger := &ConnAwareLogger{
|
2022-08-11 12:31:35 +00:00
|
|
|
tracker: tracker,
|
2021-11-08 15:43:36 +00:00
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
|
|
|
|
observer.RegisterSink(connAwareLogger.tracker)
|
|
|
|
|
|
|
|
return connAwareLogger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConnAwareLogger) ReplaceLogger(logger *zerolog.Logger) *ConnAwareLogger {
|
|
|
|
return &ConnAwareLogger{
|
|
|
|
tracker: c.tracker,
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConnAwareLogger) ConnAwareLogger() *zerolog.Event {
|
|
|
|
if c.tracker.CountActiveConns() == 0 {
|
|
|
|
return c.logger.Error()
|
|
|
|
}
|
|
|
|
return c.logger.Warn()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConnAwareLogger) Logger() *zerolog.Logger {
|
|
|
|
return c.logger
|
|
|
|
}
|