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 structured logging with slog #475

Merged
merged 1 commit into from
Oct 16, 2023

Conversation

hifi
Copy link
Collaborator

@hifi hifi commented Apr 30, 2023

Configuration file can set the logging handler between two: text, which is the default, and json. Default level is info which matches previous behavior.

A bit more info level logging is enabled by default to default restore to being verbose compared to quiet and snapshotting.

Trace logging has been converted to be on DEBUG level and always enabled if the configured level is high enough.

Bumps Go requirement to 1.20

Some example output with the default text handler and info level:

$ gow run ./cmd/litestream/ replicate -config config-file.yaml
time=2023-04-30T20:06:35.254+03:00 level=INFO msg=litestream version="(development build)"
time=2023-04-30T20:06:35.254+03:00 level=INFO msg="initialized db" path=/home/hifi/git/beeper/litestream/restore.db
time=2023-04-30T20:06:35.254+03:00 level=INFO msg="replicating to" name=file type=file sync-interval=1s path=/tmp/litestream-local
time=2023-04-30T20:06:36.258+03:00 level=INFO msg="init: cannot determine last wal position, clearing generation" db=/home/hifi/git/beeper/litestream/restore.db error="primary wal header: EOF"
time=2023-04-30T20:06:36.272+03:00 level=INFO msg="sync: new generation" db=/home/hifi/git/beeper/litestream/restore.db generation=9b0ed7758f1927b6 reason="no generation exists"
time=2023-04-30T20:06:36.274+03:00 level=INFO msg="write snapshot" db=/home/hifi/git/beeper/litestream/restore.db replica=file position=9b0ed7758f1927b6/00000000:4152
time=2023-04-30T20:06:36.890+03:00 level=INFO msg="snapshot written" db=/home/hifi/git/beeper/litestream/restore.db replica=file position=9b0ed7758f1927b6/00000000:4152 elapsed=615.685125ms sz=51092980
time=2023-04-30T20:06:36.890+03:00 level=INFO msg="write wal segment" db=/home/hifi/git/beeper/litestream/restore.db replica=file position=9b0ed7758f1927b6/00000000:0
time=2023-04-30T20:06:36.891+03:00 level=INFO msg="wal segment written" db=/home/hifi/git/beeper/litestream/restore.db replica=file position=9b0ed7758f1927b6/00000000:0 elapsed=1.606214ms sz=4152
time=2023-04-30T20:06:37.264+03:00 level=INFO msg="write wal segment" db=/home/hifi/git/beeper/litestream/restore.db replica=file position=9b0ed7758f1927b6/00000000:4152
time=2023-04-30T20:06:37.267+03:00 level=INFO msg="wal segment written" db=/home/hifi/git/beeper/litestream/restore.db replica=file position=9b0ed7758f1927b6/00000000:4152 elapsed=2.902074ms sz=4120

@hifi
Copy link
Collaborator Author

hifi commented Apr 30, 2023

@benbjohnson I whipped this together based on our conversation earlier. There's a few caveats like with Replica not having the Client on creation time requiring (for now) to use a function to build the logger each time it is used.

Some numbers like the WAL index could probably be improved by making it a type alias and having a log formatter function (automatically used by slog) to format it as hex without needing to call fmt.Sprintf manually.

Open for initial comments about the whole thing and suggestions what to improve.

@hifi hifi force-pushed the for-upstream/slog-logging branch 3 times, most recently from 340847e to 4f89a99 Compare October 14, 2023 09:33
@hifi hifi marked this pull request as ready for review October 14, 2023 09:54
@hifi
Copy link
Collaborator Author

hifi commented Oct 14, 2023

@benbjohnson rebased and cleaned up, let me know what you think!

Configuration file can set the logging handler between two: text,
which is the default, and json. Default level is info which matches
previous behavior.

A bit more info level logging is enabled by default to default
restore to being verbose compared to quiet and snapshotting.

Trace logging has been converted to be on DEBUG level and always
enabled if the configured level is high enough.

Bumps Go requirement to 1.21
@hifi hifi force-pushed the for-upstream/slog-logging branch from 4f89a99 to f6fe7c4 Compare October 14, 2023 11:04
@benbjohnson benbjohnson merged commit b1abd6b into benbjohnson:main Oct 16, 2023

package main

import (
"context"
"io"
"log"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"log" package is still used; see lines 38-41,49.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, the Windows build isn't built anymore but the code still exists. It's basically unmaintained so I didn't notice it since I didn't do a Windows build.

I guess it could be fixed but it doesn't really change much.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what it's worth, Litestream works just fine on Windows after adding imports.

@@ -63,13 +62,13 @@ func (s *windowsService) Execute(args []string, r <-chan svc.ChangeRequest, stat
// Instantiate replication command and load configuration.
c := NewReplicateCommand()
if c.Config, err = ReadConfigFile(DefaultConfigPath(), true); err != nil {
log.Printf("cannot load configuration: %s", err)
slog.Error("cannot load configuration", "error", err)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"slog" not imported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants