absent-sapphire•8mo ago
Double log output
in
main.py
logging works as expected, however in routes.py
logging is printed twice for some reason.
I did not setup any custom logging, I just use
Actor.log.info("STARTING A NEW CRAWL JOB")
example:
If I add this in my main.py
(https://docs.apify.com/sdk/python/docs/concepts/logging)
it prints everything from main.py
2x, and everything from routes.py
3x.
10 Replies
Someone will reply to you shortly. In the meantime, this might help:
-# This post was marked as solved by DuxSec. View answer.
Hi, modify your logging setup in main.py
async def main() -> None:
async with Actor:
apify_logger = logging.getLogger('apify')
if not apify_logger.hasHandlers():
handler = logging.StreamHandler()
handler.setFormatter(ActorLogFormatter())
apify_logger.setLevel(logging.DEBUG)
apify_logger.addHandler(handler)
absent-sapphireOP•8mo ago
Thanks for the reply, if I use your code, it still logs everything in
routes.py
2x... (it works in main.py
correctly)If you set up logging in a separate file (logging_setup.py), then import it everywhere, it ensures consistency.
absent-sapphireOP•8mo ago
still the same... main printed OK, but routes printed 2x
main.py
utils.py
routes.py
The issue is that each time default_handler is called in routes.py, it re-adds a new log handler. Since setup_logging() runs inside default_handler, new handlers keep stacking up, leading to duplicated logs.
fix utils.py
from apify.log import ActorLogFormatter
import logging
LOGGING_INITIALIZED = False
async def setup_logging():
global LOGGING_INITIALIZED
if LOGGING_INITIALIZED:
return
apify_logger = logging.getLogger('apify')
if not apify_logger.hasHandlers():
handler = logging.StreamHandler()
handler.setFormatter(ActorLogFormatter())
apify_logger.setLevel(logging.DEBUG)
apify_logger.addHandler(handler)
LOGGING_INITIALIZED = True
Fix routes.py
from utils import setup_logging
from apify import Actor
@router.default_handler
async def default_handler(context: PlaywrightCrawlingContext) -> None:
await setup_logging()
Actor.log.info("STARTING A NEW CRAWL JOB")
absent-sapphireOP•8mo ago
ah I see, I implemented your code, but still same issue... (routes.py does see
LOGGING_INITIALIZED
correctly and returns early)
----
I created a sample project with the minimal code to reproduce the issue
main.py
routes.py
utils.py
Add following code in utils.py
logging.getLogger().handlers.clear()
apify_logger = logging.getLogger("apify")
apify_logger.setLevel(logging.DEBUG)
absent-sapphireOP•8mo ago
that fixed it! thank you so much
I am glad it's resolved