0

我无法禁用默认情况下通过以下方式打开的日志记录structlog

下面是我的代码:

输入文件包含:

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())

代码工作正常,除了我希望摆脱终端中显示的日志,因为我尝试了多种方法。

4

2 回答 2

0

您的示例既不包含structlog配置logging也不包含对任何一个的调用——因此很难为您提供帮助。我会大胆猜测并假设日志条目来自砷。

如果您想通过 stdlib 控制 structlog,因为您的主题暗示,您必须将其配置为与 stdlib 集成。这是记录在这里:https ://www.structlog.org/en/stable/standard-library.html#suggested-configurations

之后,structlog 将通过 stdlib 进行记录,从而尊重 stdlib 的日志级别。

于 2021-01-11T06:39:30.853 回答
-1

通过使用以下函数解决:

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)

归功于-> Github 答案

于 2021-01-12T00:08:54.963 回答