-
-
Notifications
You must be signed in to change notification settings - Fork 162
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
feat: Add logger override option, use slog
and capture driver as logs
#497
Open
GuyGoldenberg
wants to merge
7
commits into
playwright-community:main
Choose a base branch
from
GuyGoldenberg:add-logger-override-support
base: main
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
7 commits
Select commit
Hold shift + click to select a range
f83ccec
Add stream logger support for driver stdout and stderr
GuyGoldenberg a6bd644
Use `slog` as the main logger
GuyGoldenberg 9b85d51
Change all log calls to use `slog`
GuyGoldenberg 116ff74
Rename to log source
GuyGoldenberg e08235e
Rename to `pwlogger` and use `ErrAttr`
GuyGoldenberg e3a6a5f
Add context to stream logger
GuyGoldenberg 36d88c0
Fix formatting
GuyGoldenberg 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
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
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
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,60 @@ | ||
package pwlogger | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"strings" | ||
) | ||
|
||
// StreamType represents the type of output stream | ||
type StreamType int | ||
|
||
const ( | ||
StdoutStream StreamType = iota | ||
StderrStream | ||
) | ||
|
||
func (st StreamType) String() string { | ||
switch st { | ||
case StdoutStream: | ||
return "stdout" | ||
case StderrStream: | ||
return "stderr" | ||
default: | ||
return "unknown" | ||
} | ||
} | ||
|
||
func (st StreamType) LogValue() slog.Value { | ||
return slog.StringValue(st.String()) | ||
} | ||
|
||
// SlogWriter is a custom type that implements io.Writer | ||
type SlogWriter struct { | ||
logger *slog.Logger | ||
stream StreamType | ||
cmdAttrs []slog.Attr | ||
} | ||
|
||
// Write implements the io.Writer interface | ||
func (sw *SlogWriter) Write(p []byte) (n int, err error) { | ||
message := strings.TrimSpace(string(p)) | ||
attrs := append(sw.cmdAttrs, | ||
slog.String("stream", sw.stream.String()), | ||
) | ||
sw.logger.LogAttrs(context.Background(), slog.LevelInfo, message, attrs...) | ||
return len(p), nil | ||
} | ||
|
||
// NewSlogWriter creates a new SlogWriter with the specified stream type and command attributes | ||
func NewSlogWriter(logger *slog.Logger, stream StreamType, cmdAttrs ...slog.Attr) *SlogWriter { | ||
return &SlogWriter{ | ||
logger: logger, | ||
stream: stream, | ||
cmdAttrs: cmdAttrs, | ||
} | ||
} | ||
|
||
func ErrAttr(err error) slog.Attr { | ||
return slog.Any("error", 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
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 |
---|---|---|
|
@@ -8,6 +8,8 @@ import ( | |
"io" | ||
"os" | ||
|
||
"github.com/playwright-community/playwright-go/internal/pwlogger" | ||
|
||
"github.com/go-jose/go-jose/v3/json" | ||
) | ||
|
||
|
@@ -44,7 +46,7 @@ func (t *pipeTransport) Poll() (*message, error) { | |
if os.Getenv("DEBUGP") != "" { | ||
fmt.Fprint(os.Stdout, "\x1b[33mRECV>\x1b[0m\n") | ||
if err := json.NewEncoder(os.Stdout).Encode(msg); err != nil { | ||
logger.Printf("could not encode json: %v\n", err) | ||
logger.Error("could not encode json", pwlogger.ErrAttr(err)) | ||
} | ||
} | ||
return msg, nil | ||
|
@@ -72,7 +74,7 @@ func (t *pipeTransport) Send(msg map[string]interface{}) error { | |
if os.Getenv("DEBUGP") != "" { | ||
fmt.Fprint(os.Stdout, "\x1b[32mSEND>\x1b[0m\n") | ||
if err := json.NewEncoder(os.Stdout).Encode(msg); err != nil { | ||
logger.Printf("could not encode json: %v\n", err) | ||
logger.Error("could not encode json", pwlogger.ErrAttr(err)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only for debugging when developing playwright-go. No errors are printed in the new PR. https://github.com/playwright-community/playwright-go/pull/500/files#diff-5982a4ca350449c8a5deba675bc4bc4bb0f1823c45e6c7ba0076cc6b159fef8fR73 |
||
} | ||
} | ||
lengthPadding := make([]byte, 4) | ||
|
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.
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.
Hi @GuyGoldenberg,
Output is redirected tostderr
of playwrightDriver is already redirected tooptions.Stderr
innewPipTransport
, andstdout
only contains one-way communication protocol data. Is there a special reason to log the protocol output?options.stdout
andoptions.stderr.
It is recommended that the caller handle logging itself.