Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use circular buffer for zap logs #585

Merged
merged 80 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
9d3ebe4
init branch
fenogentov May 9, 2022
1c78d75
Merge branch 'FerretDB:main' into issue-564-BufferZap
fenogentov May 9, 2022
c96465e
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov May 9, 2022
0849a43
Merge branch 'issue-564-BufferZap' of github.com:fenogentov/FerretDB …
fenogentov May 9, 2022
b0b67ff
Merge branch 'FerretDB:main' into issue-564-BufferZap
fenogentov May 10, 2022
f2a731d
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov May 10, 2022
8a29207
Merge branch 'issue-564-BufferZap' of github.com:fenogentov/FerretDB …
fenogentov May 10, 2022
b0e5a57
fix
fenogentov May 10, 2022
634c492
Merge branch 'FerretDB:main' into issue-564-BufferZap
fenogentov May 10, 2022
81c13a4
Merge branch 'FerretDB:main' into issue-564-BufferZap
fenogentov May 11, 2022
2f887b7
fix
fenogentov May 11, 2022
599c327
small rollback
fenogentov May 11, 2022
e5d6e43
fix
fenogentov May 11, 2022
8acd12e
fix
fenogentov May 11, 2022
082dcf8
fix
fenogentov May 11, 2022
9740907
fix
fenogentov May 11, 2022
000f92f
fix
fenogentov May 11, 2022
01c2c58
fix
fenogentov May 11, 2022
727ec82
Merge branch 'main' into issue-564-BufferZap
seeforschauer May 12, 2022
c6c0046
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov May 12, 2022
3250303
added buffer size
fenogentov May 12, 2022
1ea7532
Merge branch 'issue-564-BufferZap' of github.com:fenogentov/FerretDB …
fenogentov May 12, 2022
db087bc
Merge branch 'main' into issue-564-BufferZap
AlekSi May 12, 2022
8eb7130
Merge branch 'main' into issue-564-BufferZap
AlekSi May 12, 2022
8caedb9
Merge branch 'main' into issue-564-BufferZap
AlekSi May 12, 2022
6899784
fix `getLogRAM`
fenogentov May 12, 2022
7de6cfb
Merge branch 'issue-564-BufferZap' of github.com:fenogentov/FerretDB …
fenogentov May 12, 2022
12c42ca
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov May 13, 2022
7b2aca7
fix
fenogentov May 13, 2022
07af1b1
fix
fenogentov May 13, 2022
e97e793
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov May 16, 2022
e362928
fix
fenogentov May 16, 2022
cdae5c9
Merge branch 'main' into issue-564-BufferZap
AlekSi May 16, 2022
a478c65
Merge branch 'main' into issue-564-BufferZap
seeforschauer May 18, 2022
8cad812
Merge branch 'main' into issue-564-BufferZap
May 18, 2022
ddfe1ce
Merge branch 'main' into issue-564-BufferZap
seeforschauer May 19, 2022
2290a67
refactoring logram
fenogentov May 19, 2022
c4ea95f
Merge branch 'issue-564-BufferZap' of github.com:fenogentov/FerretDB …
fenogentov May 19, 2022
63e7844
redone logram_test
fenogentov May 19, 2022
62ad583
fix
fenogentov May 19, 2022
6a670f8
fix
fenogentov May 23, 2022
2bc98a9
Merge branch 'main' into issue-564-BufferZap
seeforschauer May 23, 2022
0e3f9e9
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov May 26, 2022
34e3fc3
fix
fenogentov May 26, 2022
3eb53d1
Merge branch 'issue-564-BufferZap' of github.com:fenogentov/FerretDB …
fenogentov May 26, 2022
db998bd
Apply suggestions from code review
fenogentov May 26, 2022
c27deff
fix
fenogentov May 26, 2022
4254acc
Merge branch 'issue-564-BufferZap' of github.com:fenogentov/FerretDB …
fenogentov May 26, 2022
39653cf
fix
fenogentov May 26, 2022
3140c32
fix test
fenogentov May 26, 2022
2e8ad6c
Merge branch 'main' into issue-564-BufferZap
seeforschauer May 26, 2022
6458ed1
Merge branch 'FerretDB:main' into issue-564-BufferZap
fenogentov May 26, 2022
69e0935
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov May 26, 2022
8c5c33b
Merge branch 'issue-564-BufferZap' of github.com:fenogentov/FerretDB …
fenogentov May 26, 2022
15d2c60
Merge branch 'main' into issue-564-BufferZap
AlekSi May 30, 2022
26718ec
Apply suggestions from code review
fenogentov May 30, 2022
dc477ac
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov May 30, 2022
f5bcc10
Merge branch 'issue-564-BufferZap' of github.com:fenogentov/FerretDB …
fenogentov May 30, 2022
4ce0505
new names
fenogentov May 30, 2022
7da50d0
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov May 30, 2022
7b89880
fix commentary
fenogentov May 30, 2022
861055a
fix
fenogentov May 30, 2022
34a7b2e
fix test
fenogentov May 30, 2022
7d3d331
fix test
fenogentov May 30, 2022
e33a1b2
Merge branch 'main' into issue-564-BufferZap
seeforschauer May 30, 2022
af30675
Merge branch 'main' into issue-564-BufferZap
seeforschauer May 30, 2022
7d5578d
Merge branch 'main' into issue-564-BufferZap
AlekSi May 30, 2022
bf4535a
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov May 31, 2022
b54cec8
Merge branch 'issue-564-BufferZap' of github.com:fenogentov/FerretDB …
fenogentov May 31, 2022
55c998b
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov May 31, 2022
032c1b2
Apply suggestions from code review
fenogentov May 31, 2022
3be5927
Merge branch 'FerretDB:main' into issue-564-BufferZap
fenogentov Jun 2, 2022
80244ea
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov Jun 2, 2022
61694b3
Merge branch 'issue-564-BufferZap' of github.com:fenogentov/FerretDB …
fenogentov Jun 2, 2022
8937f69
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov Jun 3, 2022
9ba4109
Merge branch 'main' into issue-564-BufferZap
AlekSi Jun 6, 2022
8adf2f2
Merge branch 'main' of https://github.com/FerretDB/FerretDB into issu…
fenogentov Jun 6, 2022
6275b60
Merge branch 'issue-564-BufferZap' of github.com:fenogentov/FerretDB …
fenogentov Jun 6, 2022
8e3e578
Rename files
AlekSi Jun 6, 2022
9486c10
Add package description
AlekSi Jun 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions internal/util/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,18 @@ func Setup(level zapcore.Level) {
log.Fatal(err)
}

SetupWithLogger(logger)
logram := NewLogRAM()

logger = logger.WithOptions(zap.Hooks(func(entry zapcore.Entry) error {
logram.append(entry)
return nil
}))

setupWithLogger(logger)
}

// SetupWithLogger initializes logging with a given logger and its level.
func SetupWithLogger(logger *zap.Logger) {
// setupWithLogger initializes logging with a given logger and its level.
func setupWithLogger(logger *zap.Logger) {
zap.ReplaceGlobals(logger)

if _, err := zap.RedirectStdLogAt(logger, zap.InfoLevel); err != nil {
Expand Down
77 changes: 77 additions & 0 deletions internal/util/logging/logram.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package logging
seeforschauer marked this conversation as resolved.
Show resolved Hide resolved
AlekSi marked this conversation as resolved.
Show resolved Hide resolved

import (
"sync"
"time"

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

type record struct {
Level zapcore.Level
Time time.Time
LoggerName string
Message string
Stack string
}

// logRAM structure storage of log records in memory.
fenogentov marked this conversation as resolved.
Show resolved Hide resolved
type logRAM struct {
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
counter int64
fenogentov marked this conversation as resolved.
Show resolved Hide resolved
log [1024]*record
mu sync.RWMutex
}

// NewLogRAM is creating entries log in memory.
fenogentov marked this conversation as resolved.
Show resolved Hide resolved
func NewLogRAM() *logRAM {
return &logRAM{
mu: sync.RWMutex{},
}
}

// append adding entry in logram.
func (l *logRAM) append(entry zapcore.Entry) {
fenogentov marked this conversation as resolved.
Show resolved Hide resolved
l.mu.Lock()
defer l.mu.Unlock()

rec := &record{
Level: entry.Level,
Time: entry.Time,
LoggerName: entry.LoggerName,
Message: entry.Message,
Stack: entry.Stack,
}

l.log[l.counter] = rec
l.counter = (l.counter + 1) % 1024
}

func (l *logRAM) getLogRAM() (entrys []zapcore.Entry) {
for _, r := range l.log {
if r != nil {
fenogentov marked this conversation as resolved.
Show resolved Hide resolved
e := zapcore.Entry{
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
Level: r.Level,
Time: r.Time,
LoggerName: r.LoggerName,
Message: r.Message,
Stack: r.Stack,
}
entrys = append(entrys, e)
}
}
return
}