-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogging_setup.py
More file actions
108 lines (77 loc) · 3.69 KB
/
logging_setup.py
File metadata and controls
108 lines (77 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
Logging configuration — works with both MobileClient and WebClient.
Both clients use StdLoggingInstagramClientLogger by default, which routes
all output through Python's standard `logging` module under the logger name
"insta_wizard.client".
Three options depending on what you need:
1. Default logger — control verbosity via the standard logging API.
No changes to the client required.
2. Custom logger — pass your own logger=... to the client constructor.
Useful to integrate with structlog, loguru, etc.
3. Silent (no logs) — pass NoOpInstagramClientLogger() to disable output.
"""
import asyncio
import logging
from insta_wizard import (
InstagramClientLogger,
MobileClient,
NoOpInstagramClientLogger,
WebClient,
)
from insta_wizard.common.transport.models import TransportResponse
# =============================================================================
# Option 1 — default logger
#
# No logger= argument needed. The client writes to "insta_wizard.client"
# via stdlib logging. Set the level to control what you see
# =============================================================================
logging.basicConfig(level=logging.INFO)
logging.getLogger("insta_wizard.client").setLevel(logging.DEBUG)
async def option_1_default_logger() -> None:
# No logger= passed — StdLoggingInstagramClientLogger is used automatically.
async with MobileClient() as client:
await client.login("YOUR_USERNAME", "YOUR_PASSWORD")
await client.account.get_current_user()
# =============================================================================
# Option 2 — custom logger
#
# Subclass InstagramClientLogger and pass it via logger=.
# The same instance can be shared across multiple clients.
# =============================================================================
class MyLogger(InstagramClientLogger):
def info(self, msg: object, *args, **kwargs) -> None:
print(f"[INFO] {msg % args if args else msg}")
def error(self, msg: object, *args, **kwargs) -> None:
print(f"[ERROR] {msg % args if args else msg}")
def debug(self, msg: object, *args, **kwargs) -> None:
pass # suppress debug output
def warning(self, msg: object, *args, **kwargs) -> None:
print(f"[WARN] {msg % args if args else msg}")
def request(self, msg: object, *args, **kwargs) -> None:
print(f"[-->] {msg % args if args else msg}")
def response(self, resp: TransportResponse, body: str | dict | None) -> None:
print(f"[<--] HTTP {resp.status} {resp.url}")
async def option_2_custom_logger() -> None:
logger = MyLogger()
async with MobileClient(logger=logger) as client:
await client.login("YOUR_USERNAME", "YOUR_PASSWORD")
await client.account.get_current_user()
# The same logger instance works with WebClient too.
async with WebClient(logger=logger) as client:
await client.login("YOUR_USERNAME", "YOUR_PASSWORD")
await client.account.get_edit_form_data()
# =============================================================================
# Option 3 — no logging
#
# Pass NoOpInstagramClientLogger() to suppress all client output entirely.
# =============================================================================
async def option_3_no_logging() -> None:
async with MobileClient(logger=NoOpInstagramClientLogger()) as client:
await client.login("YOUR_USERNAME", "YOUR_PASSWORD")
await client.account.get_current_user()
async def main() -> None:
await option_1_default_logger()
await option_2_custom_logger()
await option_3_no_logging()
if __name__ == "__main__":
asyncio.run(main())