Skip to content

Commit

Permalink
improved logging
Browse files Browse the repository at this point in the history
  • Loading branch information
cjlapao committed Jan 18, 2024
1 parent 75d24bf commit 10c918f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.4.6] -

### Fixed

- Fixed an issue where the api base context was not setting the log correctly
resulting in missing log lines

## [0.4.5] - 2024-01-16

### Added
Expand Down
22 changes: 22 additions & 0 deletions src/basecontext/api_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package basecontext

import (
"context"

"github.com/Parallels/pd-api-service/models"
)

type ApiContext interface {
Context() context.Context
GetAuthorizationContext() *AuthorizationContext
GetRequestId() string
GetUser() *models.ApiUser
Verbose() bool
EnableLog()
DisableLog()
ToggleLogTimestamps(value bool)
LogInfo(format string, a ...interface{})
LogError(format string, a ...interface{})
LogDebug(format string, a ...interface{})
LogWarn(format string, a ...interface{})
}
26 changes: 9 additions & 17 deletions src/basecontext/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,6 @@ import (
"github.com/Parallels/pd-api-service/models"
)

type ApiContext interface {
Context() context.Context
GetAuthorizationContext() *AuthorizationContext
GetRequestId() string
GetUser() *models.ApiUser
Verbose() bool
EnableLog()
DisableLog()
ToggleLogTimestamps(value bool)
LogInfo(format string, a ...interface{})
LogError(format string, a ...interface{})
LogDebug(format string, a ...interface{})
LogWarn(format string, a ...interface{})
}

type BaseContext struct {
shouldLog bool
ctx context.Context
Expand Down Expand Up @@ -55,7 +40,8 @@ func NewRootBaseContext() *BaseContext {

func NewBaseContextFromRequest(r *http.Request) *BaseContext {
baseContext := &BaseContext{
ctx: r.Context(),
shouldLog: true,
ctx: r.Context(),
}

authContext := baseContext.ctx.Value(constants.AUTHORIZATION_CONTEXT_KEY)
Expand All @@ -68,7 +54,8 @@ func NewBaseContextFromRequest(r *http.Request) *BaseContext {

func NewBaseContextFromContext(c context.Context) *BaseContext {
baseContext := &BaseContext{
ctx: c,
shouldLog: true,
ctx: c,
}

authContext := baseContext.ctx.Value(constants.AUTHORIZATION_CONTEXT_KEY)
Expand All @@ -88,10 +75,15 @@ func (c *BaseContext) Context() context.Context {
}

func (c *BaseContext) GetRequestId() string {
if c.ctx == nil {
return ""
}

id := c.ctx.Value(constants.REQUEST_ID_KEY)
if id == nil {
return ""
}

return id.(string)
}

Expand Down

0 comments on commit 10c918f

Please sign in to comment.