go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/web/logger.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package web
     9  
    10  import (
    11  	"fmt"
    12  
    13  	"go.charczuk.com/sdk/logutil"
    14  )
    15  
    16  // Logger is a type that emits log messages.
    17  type Logger interface {
    18  	Output(int, string) error
    19  }
    20  
    21  // LogOnListen returns an OnListen handler that logs when the server begins listening.
    22  func LogOnListen(app *App, log Logger) func() {
    23  	return func() {
    24  		log.Output(logutil.SDKStackDepth(), fmt.Sprintf("WEB: listening on %s", app.Server.Listener.Addr().String()))
    25  	}
    26  }
    27  
    28  // LogOnRequest returns an OnRequest handler that logs requests.
    29  func LogOnRequest(log Logger) func(RequestEvent) {
    30  	return func(re RequestEvent) {
    31  		log.Output(logutil.SDKStackDepth(), "WEB: "+re.String())
    32  	}
    33  }
    34  
    35  // LogOnError returns an OnError handler that logs errors.
    36  func LogOnError(log Logger) func(Context, error) {
    37  	return func(_ Context, err error) {
    38  		log.Output(logutil.SDKStackDepth(), fmt.Sprintf("WEB ERROR: %+v", err))
    39  	}
    40  }