-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(zap): silent zap logging (#308)
* 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
1 parent
dc92fff
commit c91a24b
Showing
7 changed files
with
588 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.