Skip to content

Commit

Permalink
feat(logging): custom logger
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Sep 30, 2022
1 parent e22a568 commit ce61721
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,30 @@ import (
// remote address, invoked command, TERM setting, window dimensions and if the
// auth was public key based. Disconnect will log the remote address and
// connection duration.
//
// The logger is set to the std default logger.
func Middleware() wish.Middleware {
return MiddlewareWithLogger(log.Default())
}

// Logger is the interface that wraps the basic Log method.
type Logger interface {
Printf(format string, v ...interface{})
}

// MiddlewareWithLogger provides basic connection logging. Connects are logged with the
// remote address, invoked command, TERM setting, window dimensions and if the
// auth was public key based. Disconnect will log the remote address and
// connection duration.
func MiddlewareWithLogger(l Logger) wish.Middleware {
return func(sh ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
ct := time.Now()
hpk := s.PublicKey() != nil
pty, _, _ := s.Pty()
log.Printf("%s connect %s %v %v %s %v %v\n", s.User(), s.RemoteAddr().String(), hpk, s.Command(), pty.Term, pty.Window.Width, pty.Window.Height)
l.Printf("%s connect %s %v %v %s %v %v\n", s.User(), s.RemoteAddr().String(), hpk, s.Command(), pty.Term, pty.Window.Width, pty.Window.Height)
sh(s)
log.Printf("%s disconnect %s\n", s.RemoteAddr().String(), time.Since(ct))
l.Printf("%s disconnect %s\n", s.RemoteAddr().String(), time.Since(ct))
}
}
}

0 comments on commit ce61721

Please sign in to comment.