简体   繁体   English

无法使用日志记录控制 structlog

[英]Unable to control structlog using logging

I'm unable to disable logging which is by default turned on via structlog我无法禁用默认情况下通过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.代码工作正常,除了我希望摆脱终端中显示的日志,因为我尝试了多种方法。

Your example contains neither structlog nor logging configuration nor a call to either – so it's kinda difficult to help you.您的示例既不包含structlog也不包含logging配置,也不包含对两者的调用——因此很难为您提供帮助。 I'm gonna take a wild guess and assume that the log entries are coming from arsenic.我会大胆猜测并假设日志条目来自砷。

If you want to control structlog via stdlib as your subject insinuates, you have to configure it to integrate with stdlib.如果您想通过 stdlib 控制 structlog,因为您的主题暗示,您必须将其配置为与 stdlib 集成。 That is documented here: https://www.structlog.org/en/stable/standard-library.html#suggested-configurations这是记录在这里: https://www.structlog.org/en/stable/standard-library.html#suggested-configurations

Afterwards, structlog will log through stdlib and thus respect stdlib's log levels.之后,structlog 将通过 stdlib 进行记录,从而尊重 stdlib 的日志级别。

Solved by using the following function:使用以下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归功于 - >Github 答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM