无法使用日志记录控制结构日志

时间:2021-01-11 01:45:07

标签: python selenium geckodriver python-logging structlog

我无法禁用默认情况下通过 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())

代码工作正常,只是我想去掉终端中显示的日志,因为我已经尝试了多种方法。

2 个答案:

答案 0 :(得分:0)

您的示例既不包含 structlog 也不包含 logging 配置,也不包含对两者的调用 - 因此很难为您提供帮助。我会胡乱猜测并假设日志条目来自砷。

如果你想通过 stdlib 控制 structlog 作为你的主题暗示,你必须将它配置为与 stdlib 集成。此处记录:https://www.structlog.org/en/stable/standard-library.html#suggested-configurations

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

答案 1 :(得分:-1)

使用以下函数解决:

style.display = 'block'

信用 - > Github Answer

相关问题