Skip to content

Commit

Permalink
feat(zap): silent zap logging (#308)
Browse files Browse the repository at this point in the history
* feat(silent_zap): allow logging only if there is an error

* feat(silent_zap): lint fixes

* feat(silent_zap): rename silentlogger to silenzap, add custom logger functionality

---------

Co-authored-by: Ivan Maidurov <ivan.maidurov@omnevo.net>
  • Loading branch information
IvanMaidurov and IvanMaidurov authored Mar 24, 2023
1 parent dc92fff commit c91a24b
Show file tree
Hide file tree
Showing 7 changed files with 588 additions and 7 deletions.
16 changes: 13 additions & 3 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
"time"

"flamingo.me/dingo"
"github.com/spf13/cobra"
"go.opencensus.io/plugin/ochttp"

"flamingo.me/flamingo/v3/core/runtime"
"flamingo.me/flamingo/v3/core/zap"
"flamingo.me/flamingo/v3/framework"
Expand All @@ -21,8 +24,6 @@ import (
"flamingo.me/flamingo/v3/framework/flamingo"
"flamingo.me/flamingo/v3/framework/opencensus"
"flamingo.me/flamingo/v3/framework/web"
"github.com/spf13/cobra"
"go.opencensus.io/plugin/ochttp"
)

type (
Expand All @@ -33,6 +34,7 @@ type (
area *config.Area
args []string
routesModules []web.RoutesModule
loggerModule dingo.Module
defaultContext string
eagerSingletons bool
flagset *flag.FlagSet
Expand Down Expand Up @@ -84,6 +86,13 @@ func WithRoutes(routesModule web.RoutesModule) ApplicationOption {
}
}

// WithCustomLogger allows to use custom logger modules for flamingo app, if nothing available default will be used
func WithCustomLogger(logger dingo.Module) ApplicationOption {
return func(config *Application) {
config.loggerModule = logger
}
}

type eventRouterProvider func() flamingo.EventRouter

type arrayFlags []string
Expand All @@ -103,6 +112,7 @@ func NewApplication(modules []dingo.Module, options ...ApplicationOption) (*Appl
configDir: "config",
args: os.Args[1:],
defaultContext: "root",
loggerModule: new(zap.Module),
}

for _, option := range options {
Expand All @@ -128,7 +138,7 @@ func NewApplication(modules []dingo.Module, options ...ApplicationOption) (*Appl

modules = append([]dingo.Module{
new(framework.InitModule),
new(zap.Module),
app.loggerModule,
new(runtime.Module),
new(cmd.Module),
}, modules...)
Expand Down
66 changes: 66 additions & 0 deletions core/silentzap/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package silentzap

import (
"sync"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

type (
SilentContext struct {
mu sync.RWMutex
storedEntries []storedEntry
willWrite bool
}

storedEntry struct {
CheckedLogEntry *zapcore.CheckedEntry
Fields []zap.Field
}
)

//nolint:unparam // taking zap into account we should have possibilities to store fields
func (c *SilentContext) store(entry *zapcore.CheckedEntry, fields ...zap.Field) {
if c == nil {
return
}

go func() {
c.mu.Lock()
defer c.mu.Unlock()

c.storedEntries = append(
c.storedEntries,
storedEntry{
CheckedLogEntry: entry,
Fields: fields,
},
)
}()
}

// get returns stored entries and allows direct writing
func (c *SilentContext) get() []storedEntry {
if c == nil {
return nil
}

c.mu.RLock()
defer c.mu.RUnlock()

c.willWrite = true

return c.storedEntries
}

func (c *SilentContext) isWritingAllowed() bool {
if c == nil {
return true
}

c.mu.RLock()
defer c.mu.RUnlock()

return c.willWrite
}
Loading

0 comments on commit c91a24b

Please sign in to comment.