Search code examples
pythonseleniumgeckodriverpython-loggingstructlog

Unable to control structlog using logging


I'm unable to disable logging which is by default turned on via structlog

Here's my code below:

Input file contain:

5418531366
5418531367
import asyncio
import pathlib
import sys
from arsenic import get_session, errors
from arsenic.browsers import Firefox
from arsenic.services import Geckodriver
from aiofile import async_open
from termcolor import colored
import os


async def browse(numbers, baseurl):
    limit = asyncio.Semaphore(3)

    async def call(num):
        async with limit, get_session(Geckodriver(log_file=os.devnull), Firefox(**{'moz:firefoxOptions': {'args': ['-headless']}})) as session:
            await session.get(baseurl)
            fixednum = await session.wait_for_element(10, '#attContent_txtAccessNumber')
            await fixednum.send_keys('8778791867')
            dynnum = await session.wait_for_element(10, '#attContent_txtPIN')
            await dynnum.send_keys(num)
            goal = await session.get_element('#attContent_btnSubmit')
            await asyncio.sleep(0.3)
            await goal.click()

            try:
                await session.wait_for_element_gone(10, '#attContent_btnSubmit')
                return str(num)
            except errors.ArsenicTimeout:
                return False

    tasks = [call(n) for n in numbers]
    async with async_open('result.txt', 'w') as f:
        for task in asyncio.as_completed(tasks):
            task = await task
            if task:
                print(f"\nAvailable -- > {colored(task,'green')}\n")
                await f.write(task + "\n")


async def amain():
    baseurl = "https://www.virtualprepaidminutes.com/ATT_prepaid_calling_cards_refill_online.aspx"
    if len(sys.argv) != 2:
        return f"Usage: python {pathlib.Path(__file__).name} `InputFile`"

    try:
        numbers = pathlib.Path(sys.argv[1]).read_text(
            encoding="utf8").splitlines()
        await browse(numbers, baseurl)

    except FileNotFoundError as e:
        print(f"File {e.filename} is not exist!")


def main():
    return asyncio.run(amain())


if __name__ == "__main__":
    sys.exit(main())

The code works fine except that i wish to get rid of the log displayed in terminal as I've tried multiple ways.


Solution

  • Solved by using the following function:

    def set_arsenic_log_level(level = logging.WARNING):
        logger = logging.getLogger('arsenic')
    
    
        def logger_factory():
            return logger
    
        structlog.configure(logger_factory=logger_factory)
        logger.setLevel(level)
    

    Credit to - > Github Answer