簡體   English   中英

無法使用日志記錄控制 structlog

[英]Unable to control structlog using logging

我無法禁用默認情況下通過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())

代碼工作正常,除了我希望擺脫終端中顯示的日志,因為我嘗試了多種方法。

您的示例既不包含structlog也不包含logging配置,也不包含對兩者的調用——因此很難為您提供幫助。 我會大膽猜測並假設日志條目來自砷。

如果您想通過 stdlib 控制 structlog,因為您的主題暗示,您必須將其配置為與 stdlib 集成。 這是記錄在這里: https://www.structlog.org/en/stable/standard-library.html#suggested-configurations

之后,structlog 將通過 stdlib 進行記錄,從而尊重 stdlib 的日志級別。

使用以下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)

歸功於 - >Github 答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM