首页 > 解决方案 > 如何使“日志”功能起作用,以便只有 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}")

标签: pythonpython-3.x

解决方案


试试这个:

文件一(未更改)

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 ()

推荐阅读