main.py
logging works as expected, however in routes.py
logging is printed twice for some reason.Actor.log.info("STARTING A NEW CRAWL JOB")
[apify] INFO Checking item 17 [apify] INFO Checking item 17 ({"message": "Checking item 17"}) [apify] INFO Processing new item with index: 17 [apify] INFO Processing new item with index: 17 ({"message": "Processing new item with index: 17"})
main.py
(https://docs.apify.com/sdk/python/docs/concepts/logging) async def main() -> None: async with Actor: ##### SETUP LOGGING ##### handler = logging.StreamHandler() handler.setFormatter(ActorLogFormatter()) apify_logger = logging.getLogger('apify') apify_logger.setLevel(logging.DEBUG) apify_logger.addHandler(handler)
main.py
2x, and everything from routes.py
3x.[apify] INFO STARTING A NEW CRAWL JOB [apify] INFO STARTING A NEW CRAWL JOB ({"message": "STARTING A NEW CRAWL JOB"}) [apify] INFO STARTING A NEW CRAWL JOB ({"message": "STARTING A NEW CRAWL JOB"})
logging.getLogger().handlers.clear()
apify_logger = logging.getLogger("apify")
apify_logger.setLevel(logging.DEBUG)
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)
routes.py
2x... (it works in main.py
correctly)async def main() -> None: async with Actor: ##### SETUP LOGGING ##### await setup_logging() ...
from apify.log import ActorLogFormatter import logging async def setup_logging(): 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)
@router.default_handler async def default_handler(context: PlaywrightCrawlingContext) -> None: await setup_logging() Actor.log.info("STARTING A NEW CRAWL JOB")
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
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")
LOGGING_INITIALIZED
correctly and returns early)from apify import Actor from crawlee.crawlers import PlaywrightCrawler from src.routes import router from src.utils import setup_logging async def main() -> None: async with Actor: await setup_logging() crawler = PlaywrightCrawler(headless=False, request_handler=router) await crawler.run(["https://apify.com"])
from crawlee.crawlers import PlaywrightCrawlingContext from crawlee.router import Router from apify import Actor from src.utils import setup_logging router = Router[PlaywrightCrawlingContext]() @router.default_handler async def default_handler(context: PlaywrightCrawlingContext) -> None: await setup_logging() Actor.log.info("STARTING A NEW CRAWL JOB")
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
logging.getLogger().handlers.clear()
apify_logger = logging.getLogger("apify")
apify_logger.setLevel(logging.DEBUG)