python - 如何使“日志”功能起作用,以便只有 log("The log message") 可以工作?
问题描述
我现在正在尝试做一段时间,我真的不知道了。我想要的基本上是做一个记录功能,这样它就可以像 log("{the log message}")
它不一定是一种方法,只是一种记录消息的简化方法,因此更容易阅读。
谢谢!
这是代码:
# file one
from functions import *
try:
open('key.txt', "r")
key = True
except FileNotFoundError:
key = False
if check_internet():
x = status_boot[0]
elif not check_internet():
x = status_boot[1]
log("Hi")
# file two
from datetime import datetime
import requests
status_boot = ["online", "offline", "key invalid", "error", "Error: Key not accessible!"]
x = status_boot[3]
def time():
now = datetime.now()
return now.strftime("%H:%M:%S")
def check_internet():
url = 'http://www.google.com/'
timeout = 5
try:
_ = requests.get(url, timeout=timeout)
return True
except requests.ConnectionError:
return False
def log(m):
write = open('archerlog.txt', 'w+')
return write.write(f"[{time}] {m}")
解决方案
试试这个:
文件一(未更改)
from functions import *
try:
open('key.txt', "r")
key = True
except FileNotFoundError:
key = False
if check_internet():
x = status_boot[0]
elif not check_internet():
x = status_boot[1]
log("Hi")
档案二
from datetime import datetime
import requests
status_boot = ["online", "offline", "key invalid", "error", "Error: Key not accessible!"]
x = status_boot[3]
def time():
now = datetime.now()
return now.strftime("%H:%M:%S")
def check_internet():
url = 'http://www.google.com/'
timeout = 5
try:
_ = requests.get(url, timeout=timeout)
return True
except requests.ConnectionError:
return False
def log(m):
write = open('archerlog.txt', 'w+')
return write.write(f"[{time()}] {m}") # add ()
推荐阅读
- javascript - Preserve parameter value in setTimeout inside loop
- python - Constructing `telethon.tl.custom.message.Message` from message dumped as dict
- android - Material Slider 的默认布局高度(dp)是多少?
- python - Pytest 因 AttributeError 失败:“str”对象没有属性“x”
- javascript - React setState 没有设置正确的值
- mysql - csv 中的日期格式没有变化。该怎么办?
- javascript - 计算 eth_signTypedData 的消息哈希
- php - 无法在 Wordpress 中使用 REST API 获得响应
- php - woocommerce 在按钮单击时更新购物车商品数量
- sql - 如何在 oracle sql 查询中包含 LOB 大小以创建 rowid 范围