Skip to content

Commit e43da61

Browse files
authored
feat(logger): add pw:api logger (microsoft#386)
1 parent 30c5b52 commit e43da61

File tree

8 files changed

+5170
-1874
lines changed

8 files changed

+5170
-1874
lines changed

client.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@
1818

1919
def main(playwright: Playwright) -> None:
2020
browser = playwright.chromium.launch(headless=False)
21-
page = browser.new_page(viewport=0)
22-
page.set_content(
23-
"<button id=button onclick=\"window.open('http://webkit.org', '_blank')\">Click me</input>"
24-
)
25-
26-
with page.expect_popup() as popup_info:
27-
page.click("#button")
28-
print(popup_info.value)
2921

3022
print("Contexts in browser: %d" % len(browser.contexts))
3123
print("Creating context...")

playwright/_impl/_driver.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from pathlib import Path
1919

2020
import playwright
21+
from playwright._impl._logger import init_logger
2122

2223

2324
def compute_driver_executable() -> Path:
@@ -37,3 +38,5 @@ def compute_driver_executable() -> Path:
3738
# RuntimeError: Cannot add child handler, the child watcher does not have a loop attached
3839
asyncio.get_event_loop()
3940
asyncio.get_child_watcher()
41+
42+
init_logger()

playwright/_impl/_logger.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright (c) Microsoft Corporation.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import sys
17+
18+
debug_enabled = os.environ.get("PWDEBUG") or (
19+
"DEBUG" in os.environ and "pw:api" in os.environ["DEBUG"]
20+
)
21+
22+
23+
def init_logger() -> None:
24+
if os.environ.get("PWDEBUG"):
25+
os.environ["DEBUG"] = (
26+
os.environ["DEBUG"] + ",pw:api" if "DEBUG" in os.environ else "pw:api"
27+
)
28+
29+
30+
def log_api(text: str) -> None:
31+
if debug_enabled:
32+
print(f" \033[1m\033[96mpw:api\033[0m {text}", file=sys.stderr)

0 commit comments

Comments
 (0)