吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 328|回复: 10
收起左侧

[其他求助] python代码求助

[复制链接]
angelb 发表于 2024-2-2 15:06
25吾爱币

为啥logding 写入文件

import pywifi
from pywifi import const
import time
import logging

logging.basicConfig(filename='wifi3.log', level=logging.NOTSET, format='%(asctime)s  [%(levelname)s]  %(message)s',
                    datefmt='%m-%d %H:%M:%S')

logging.info("已连接到 WiFi")

logger = logging.getLogger(__name__)  # 获取日志记录器对象
logger.info("已连接到 WiFi")

wifi = pywifi.PyWiFi()

# 获取第一个无线网卡
iface = wifi.interfaces()[0]
profile1 = iface.status()

if profile1 == const.IFACE_CONNECTED:
    print("赐福")
    try:
        print('开始输出')
        print(logger.info("已连接到 WiFi"))
    except Exception as e:
        print(f"写入日志时出错: {e}")

麻烦指导一下,我单独拿出去 logging 模块没有任何问题
他连 wifi3.log 都没有生成

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| angelb 发表于 2024-2-2 15:10
        print(logger.info("已连接到 WiFi"))
这个是 改成   logger.info("已连接到 WiFi")
小小小微笑 发表于 2024-2-2 15:17
你运行的电脑连接wifi了吗?如果没连接的话就获取不到wifi信息,更加不会输出信息了。
 楼主| angelb 发表于 2024-2-2 15:33
小小小微笑 发表于 2024-2-2 15:17
你运行的电脑连接wifi了吗?如果没连接的话就获取不到wifi信息,更加不会输出信息了。

debug 我看已经到 这一行了,但是还不行
T4DNA 发表于 2024-2-2 15:48
因为pywifi设置了全局logging

https://github.com/awkman/pywifi/issues/63

如果你也要用basicConfig,可以在引用他之前,或者别用,我不用logging不知道什么其他方式和level是否有影响。
[Python] 纯文本查看 复制代码
import logging

logging.basicConfig(filename='wifi3.log', level=logging.NOTSET, format='%(asctime)s  [%(levelname)s]  %(message)s', datefmt='%m-%d %H:%M:%S')

logging.info("已连接到 WiFi")

import pywifi
小小小微笑 发表于 2024-2-2 15:57
from pywifi import const
这个模块导入有问题,会影响他的日志生成,不知道为什么。
我去掉这个新建一个文件日志是正常输出的
freelive 发表于 2024-2-3 12:05
可以尝试把引用的模块重新安装一下,再进行测试。
g1ave 发表于 2024-2-3 14:29
[Python] 纯文本查看 复制代码
import logging
import pywifi


logger = logging.getLogger('pywifi')
log_file = 'file.log'  # 替换为实际的日志文件路径
file_handler = logging.FileHandler(log_file)
logger.addHandler(file_handler)
logger.info('test')

T4DNA说的对的,这个库已经定义了log对象了,导致你引用的时候再定义是没用的
用我上面代码试试?
g1ave 发表于 2024-2-3 14:36
g1ave 发表于 2024-2-3 14:29
[mw_shl_code=python,true]
import logging
import pywifi

我测试了下,发现这只是增加了个日志文件,但是输出不了日志。。。请忽略
g1ave 发表于 2024-2-3 14:46
g1ave 发表于 2024-2-3 14:36
我测试了下,发现这只是增加了个日志文件,但是输出不了日志。。。请忽略

[Python] 纯文本查看 复制代码
import logging
import pywifi


# 引用pywifi的logger
logger = logging.getLogger('pywifi')
handlers = logger.handlers
log_file_path = 'test.log'
# 将原logger内的配置删掉
for handler in handlers:
    logger.removeHandler(handler)
# 增加自己的配置,我这里只增加了日志输出路径
file_handler = logging.FileHandler(log_file_path)
logger.addHandler(file_handler)
logger.setLevel(logging.INFO)  # 设置为所需的级别

logger.info('test')

这样OK了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-17 00:54

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表