Skip to content

Commit

Permalink
Merge pull request #24 from twharmon/logger-iface
Browse files Browse the repository at this point in the history
Logger iface
  • Loading branch information
twharmon authored May 30, 2024
2 parents f91a039 + d2c260d commit 7c23f00
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 35 deletions.
38 changes: 19 additions & 19 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,39 @@ type Context interface {
// Response returns an http response with the given status code.
// An options response body can be provided as the second
// argument.
Response(status int, body ...interface{}) Responder
Response(status int, body ...any) Responder

// LogDebug logs the given message. Arguments are handled in the
// manner of fmt.Printf.
LogDebug(message string, args ...interface{})
LogDebug(message string, args ...any)

// LogInfo logs the given message. Arguments are handled in the
// manner of fmt.Printf.
LogInfo(message string, args ...interface{})
LogInfo(message string, args ...any)

// LogNotice logs the given message. Arguments are handled in the
// manner of fmt.Printf.
LogNotice(message string, args ...interface{})
LogNotice(message string, args ...any)

// LogWarning logs the given message. Arguments are handled in
// the manner of fmt.Printf.
LogWarning(message string, args ...interface{})
LogWarning(message string, args ...any)

// LogError logs the given message. Arguments are handled in the
// manner of fmt.Printf.
LogError(message string, args ...interface{})
LogError(message string, args ...any)

// LogCritical logs the given message. Arguments are handled in
// the manner of fmt.Printf.
LogCritical(message string, args ...interface{})
LogCritical(message string, args ...any)

// LogAlert logs the given message. Arguments are handled in the
// manner of fmt.Printf.
LogAlert(message string, args ...interface{})
LogAlert(message string, args ...any)

// LogEmergency logs the given message. Arguments are handled in
// the manner of fmt.Printf.
LogEmergency(message string, args ...interface{})
LogEmergency(message string, args ...any)
}

type handlerContext struct {
Expand All @@ -60,7 +60,7 @@ func (c *handlerContext) Request() Request {
return c.req
}

func (c *handlerContext) Response(status int, body ...interface{}) Responder {
func (c *handlerContext) Response(status int, body ...any) Responder {
r := response{
status: status,
headers: map[string]string{
Expand All @@ -73,62 +73,62 @@ func (c *handlerContext) Response(status int, body ...interface{}) Responder {
return &r
}

func (c *handlerContext) LogDebug(message string, args ...interface{}) {
func (c *handlerContext) LogDebug(message string, args ...any) {
if c.logLevel > LogLevelDebug {
return
}
c.log(LogLevelDebug, message, args...)
}

func (c *handlerContext) LogInfo(message string, args ...interface{}) {
func (c *handlerContext) LogInfo(message string, args ...any) {
if c.logLevel > LogLevelInfo {
return
}
c.log(LogLevelInfo, message, args...)
}

func (c *handlerContext) LogNotice(message string, args ...interface{}) {
func (c *handlerContext) LogNotice(message string, args ...any) {
if c.logLevel > LogLevelNotice {
return
}
c.log(LogLevelNotice, message, args...)
}

func (c *handlerContext) LogWarning(message string, args ...interface{}) {
func (c *handlerContext) LogWarning(message string, args ...any) {
if c.logLevel > LogLevelWarning {
return
}
c.log(LogLevelWarning, message, args...)
}

func (c *handlerContext) LogError(message string, args ...interface{}) {
func (c *handlerContext) LogError(message string, args ...any) {
if c.logLevel > LogLevelError {
return
}
c.log(LogLevelError, message, args...)
}

func (c *handlerContext) LogCritical(message string, args ...interface{}) {
func (c *handlerContext) LogCritical(message string, args ...any) {
if c.logLevel > LogLevelCritical {
return
}
c.log(LogLevelCritical, message, args...)
}

func (c *handlerContext) LogAlert(message string, args ...interface{}) {
func (c *handlerContext) LogAlert(message string, args ...any) {
if c.logLevel > LogLevelAlert {
return
}
c.log(LogLevelAlert, message, args...)
}

func (c *handlerContext) LogEmergency(message string, args ...interface{}) {
func (c *handlerContext) LogEmergency(message string, args ...any) {
if c.logLevel > LogLevelEmergency {
return
}
c.log(LogLevelEmergency, message, args...)
}

func (c *handlerContext) log(level LogLevel, message string, args ...interface{}) {
func (c *handlerContext) log(level LogLevel, message string, args ...any) {
c.logger.Log(level, fmt.Sprintf(message, args...))
}
18 changes: 9 additions & 9 deletions fakes/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (c *Context) Request() golamb.Request {
}

// Response implements the golamb.Context interface.
func (c *Context) Response(status int, body ...interface{}) golamb.Responder {
func (c *Context) Response(status int, body ...any) golamb.Responder {
c.response.response.StatusCode = status
if len(body) > 0 {
bs, err := json.Marshal(body[0])
Expand All @@ -75,41 +75,41 @@ func (c *Context) WithRequest(r *Request) *Context {
}

// LogDebug implements the golamb.Context interface.
func (c *Context) LogDebug(message string, args ...interface{}) {
func (c *Context) LogDebug(message string, args ...any) {

}

// LogInfo implements the golamb.Context interface.
func (c *Context) LogInfo(message string, args ...interface{}) {
func (c *Context) LogInfo(message string, args ...any) {

}

// LogNotice implements the golamb.Context interface.
func (c *Context) LogNotice(message string, args ...interface{}) {
func (c *Context) LogNotice(message string, args ...any) {

}

// LogWarning implements the golamb.Context interface.
func (c *Context) LogWarning(message string, args ...interface{}) {
func (c *Context) LogWarning(message string, args ...any) {

}

// LogError implements the golamb.Context interface.
func (c *Context) LogError(message string, args ...interface{}) {
func (c *Context) LogError(message string, args ...any) {

}

// LogCritical implements the golamb.Context interface.
func (c *Context) LogCritical(message string, args ...interface{}) {
func (c *Context) LogCritical(message string, args ...any) {

}

// LogAlert implements the golamb.Context interface.
func (c *Context) LogAlert(message string, args ...interface{}) {
func (c *Context) LogAlert(message string, args ...any) {

}

// LogEmergency implements the golamb.Context interface.
func (c *Context) LogEmergency(message string, args ...interface{}) {
func (c *Context) LogEmergency(message string, args ...any) {

}
4 changes: 2 additions & 2 deletions fakes/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (r *Request) RawPath() string {
}

// Body implements the golamb.Request interface.
func (r *Request) Body(v interface{}) error {
func (r *Request) Body(v any) error {
return json.Unmarshal(r.body, v)
}

Expand Down Expand Up @@ -92,7 +92,7 @@ func (r *Request) WithRawPath(rawPath string) *Request {
}

// WithBody sets the body of the fake Request.
func (r *Request) WithBody(v interface{}) error {
func (r *Request) WithBody(v any) error {
var err error
r.body, err = json.Marshal(v)
return err
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/twharmon/golamb

go 1.18
go 1.22

require (
github.com/aws/aws-lambda-go v1.29.0
Expand Down
2 changes: 1 addition & 1 deletion golamb.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ func getConfig(configs ...*Config) *Config {
}

func defaultPanicHandler(c Context, err error) Responder {
c.LogError("handler panic recovered: %s", err)
c.LogError(fmt.Sprintf("handler panic recovered: %s", err))
return c.Response(http.StatusInternalServerError)
}
4 changes: 2 additions & 2 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Request interface {

// Body parses the JSON-encoded data and stores the result in the
// value pointed to by v.
Body(v interface{}) error
Body(v any) error

// Cookie returns the cookie with the given name.
Cookie(name string) *http.Cookie
Expand Down Expand Up @@ -45,7 +45,7 @@ func (r *request) Path(key string) string {
return r.request.PathParameters[key]
}

func (r *request) Body(v interface{}) error {
func (r *request) Body(v any) error {
return json.Unmarshal([]byte(r.request.Body), v)
}

Expand Down
2 changes: 1 addition & 1 deletion response.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Responder interface {

type response struct {
status int
body interface{}
body any
headers map[string]string
cookies []string
}
Expand Down

0 comments on commit 7c23f00

Please sign in to comment.