forked from 0xPolygonHermez/cdk-erigon
-
Notifications
You must be signed in to change notification settings - Fork 7
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
Ns/202 standard oklog format #210
Open
Nauman-S
wants to merge
20
commits into
dev
Choose a base branch
from
ns/202-standard-oklog-format
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
ec1e573
enable json logging in test
Nauman-S 3fdd87d
ok_log_json_keys (resolves #202)
Nauman-S 77cbf9e
formatting for directory logs (resolves #202)
Nauman-S 3fcdd13
formatting for directory logs (resolves #202)
Nauman-S 92ac9e4
Disable pretty formatting and newline (resolves #202)
Nauman-S d133384
Enable new line (resolves #202)
Nauman-S 5721c82
log.Debug -> log.Info
Nauman-S e3f63a6
remove from local testing environment log.json and log.dir.json
Nauman-S 18d78e2
Revert "Enable new line (resolves #202)"
Nauman-S 966cdf4
enable new line
Nauman-S dc854ea
Revert "log.Debug -> log.Info"
Nauman-S a3f45f1
update formatting
Nauman-S d9b97c1
async IO
Nauman-S c76987d
async_buffered_handler
Nauman-S 20e3c04
format imports
Nauman-S e2d218c
oklog formatting
Nauman-S a2e5c93
oklog formatting
Nauman-S 1f5a306
oklog formatting
Nauman-S e9f4dd3
oklog formatting
Nauman-S dd53d1a
log formatting and
Nauman-S File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package logging | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"io" | ||
"sync" | ||
"time" | ||
|
||
"github.com/ledgerwatch/log/v3" | ||
) | ||
|
||
const ( | ||
_BufferSize = 1 << 18 // 256 KiB | ||
_FlushInterval = time.Second * 2 | ||
) | ||
|
||
type AsyncBufferedWriter struct { | ||
Size int | ||
FlushInterval time.Duration | ||
ctx context.Context | ||
|
||
mu sync.Mutex | ||
|
||
bufferedWriter *bufio.Writer | ||
ticker *time.Ticker | ||
done chan struct{} | ||
stop chan struct{} | ||
|
||
initialized bool | ||
stopped bool | ||
} | ||
|
||
func AsyncHandler(wr io.Writer, format log.Format, ctx context.Context) log.Handler { | ||
asyncBufferedWriter := &AsyncBufferedWriter{ctx: ctx} | ||
asyncBufferedWriter.initialize(wr) | ||
|
||
h := log.FuncHandler(func(r *log.Record) error { | ||
_, err := asyncBufferedWriter.write(format.Format(r)) | ||
return err | ||
}) | ||
return h | ||
} | ||
|
||
func (s *AsyncBufferedWriter) initialize(wr io.Writer) { | ||
if s.initialized { | ||
return | ||
} | ||
|
||
s.Size = _BufferSize | ||
s.FlushInterval = _FlushInterval | ||
|
||
s.ticker = time.NewTicker(s.FlushInterval) | ||
s.bufferedWriter = bufio.NewWriterSize(wr, s.Size) | ||
|
||
s.done = make(chan struct{}) | ||
s.stop = make(chan struct{}) | ||
|
||
s.initialized = true | ||
|
||
go s.flushLoop() | ||
} | ||
|
||
func (s *AsyncBufferedWriter) write(b []byte) (int, error) { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
if len(b) >= s.bufferedWriter.Available() && s.bufferedWriter.Buffered() > 0 { | ||
if err := s.bufferedWriter.Flush(); err != nil { | ||
return 0, err | ||
} | ||
} | ||
|
||
return s.bufferedWriter.Write(b) | ||
} | ||
|
||
func (s *AsyncBufferedWriter) flush() error { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
return s.bufferedWriter.Flush() | ||
} | ||
|
||
func (s *AsyncBufferedWriter) flushLoop() { | ||
defer close(s.done) | ||
for { | ||
select { | ||
case <-s.ticker.C: | ||
_ = s.flush() | ||
case <-s.stop: | ||
return | ||
case <-s.ctx.Done(): | ||
s.Stop() | ||
} | ||
} | ||
} | ||
|
||
func (s *AsyncBufferedWriter) Stop() (err error) { | ||
var stopped bool | ||
func() { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
if !s.initialized { | ||
return | ||
} | ||
stopped = s.stopped | ||
|
||
if stopped { | ||
return | ||
} | ||
|
||
s.stopped = true | ||
|
||
s.ticker.Stop() | ||
close(s.stop) | ||
<-s.done | ||
}() | ||
|
||
if !stopped { | ||
err = s.bufferedWriter.Flush() | ||
} | ||
return err | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This format of code should be adjusted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay, I changed the ordering of imports