简体   繁体   English

记录错误回溯(最近一次调用最后):文件“/usr/lib/python3.7/logging/__init__.py”,第 1025 行,在发出 msg = self.format(record)

[英]Logging error Traceback (most recent call last):File "/usr/lib/python3.7/logging/__init__.py", line 1025, in emit msg = self.format(record)

I am trying to make a custom logging class.我正在尝试制作一个自定义日志记录类。

import logging

logging_level = 'INFO'

class Logger:
    def __init__(self):
        self.logger = logging.getLogger(__name__)
        self.logger.setLevel(logging.getLevelName(logging_level))
        formatter = logging.Formatter('%(asctime)s - %(name)s%(levelname)s: %(message)s', datefmt='%m/%d/%Y %I:%M:%S%p')
        file_handler = logging.FileHandler('logFile.log')
        file_handler.setFormatter(format)
        stream_handler = logging.StreamHandler() #if we want to print to consol
        stream_handler.setFormatter(format)
        self.logger.addHandler(file_handler)
        self.logger.addHandler(stream_handler)

    
    def logInfo(self):
        self.logger.info("Hi")

log = Logger()
log.logInfo()

I keep receiving this error我不断收到此错误

--- Logging error ---
Traceback (most recent call last):
  File "/usr/lib/python3.7/logging/__init__.py", line 1025, in emit
    msg = self.format(record)
  File "/usr/lib/python3.7/logging/__init__.py", line 869, in format
    return fmt.format(record)
AttributeError: 'builtin_function_or_method' object has no attribute 'format'
Call stack:
  File "/home/usr/.../python_scripts/Logger.py", line 22, in <module>
    log.logInfo()
  File "/home/usr/.../python_scripts/Logger.py", line 19, in logInfo
    self.logger.info("Hi")
Message: 'Hi'
Arguments: ()

I must be doing something wrong the the oo programming in python.我在 python 中的 oo 编程一定是做错了什么。 IT makes the log file but it is empty. IT 制作了日志文件,但它是空的。

You had a typo in your logInfo method and in the "format" paramater which should have been formatter , I also changed a bit the setLevel method to use getLevelName so you could change it dynamically:您的logInfo方法和应该是formatter的“格式”参数中有错字,我还更改了一些setLevel方法以使用getLevelName以便您可以动态更改它:

import logging

logging_level = 'INFO'

class Logger:
    def __init__(self):
        self.logger = logging.getLogger(__name__)
        self.logger.setLevel(logging.getLevelName(logging_level))
        formatter = logging.Formatter('%(asctime)s - %(name)s%(levelname)s: %(message)s', datefmt='%m/%d/%Y %I:%M:%S%p')
        file_handler = logging.FileHandler('logFile.log')
        file_handler.setFormatter(formatter)
        stream_handler = logging.StreamHandler() #if we want to print to consol
        stream_handler.setFormatter(formatter)
        self.logger.addHandler(file_handler)
        self.logger.addHandler(stream_handler)

    
    def logInfo(self):
        self.logger.info("Hi")

log = Logger()
log.logInfo()

Output输出

07/18/2022 06:24:39PM - __main__INFO: Hi

暂无
暂无

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

相关问题 ImportError: 无法从 'urllib' (/usr/lib/python3.7/urllib/__init__.py) 导入名称 'quote' - ImportError: cannot import name 'quote' from 'urllib' (/usr/lib/python3.7/urllib/__init__.py) ImportError:无法从“变压器”(/usr/local/lib/python3.7/dist-packages/transformers/__init__.py)导入名称“BigBirdTokenizer” - ImportError: cannot import name 'BigBirdTokenizer' from 'transformers' (/usr/local/lib/python3.7/dist-packages/transformers/__init__.py) 未知的 Python Selenium 错误:Traceback(最近一次调用最后一次):文件“D:\pythonProject\webscraping.py”,第 17 行,在<module> - Unknown Python Selenium error:Traceback (most recent call last): File "D:\pythonProject\webscraping.py", line 17, in <module> 回溯(最近一次调用最后一次):文件“D:\\Python\\PLAY.PY”,第 29 行,在<module> - Traceback (most recent call last): File "D:\Python\PLAY.PY", line 29, in <module> 导入错误:无法从“neo4j.v1”(/usr/local/lib/python3.7/site-packages/neo4j/v1/__init__.py)导入名称“CypherError” - ImportError: cannot import name 'CypherError' from 'neo4j.v1' (/usr/local/lib/python3.7/site-packages/neo4j/v1/__init__.py) ImportError:无法从“utils”(/usr/local/lib/python3.7/dist-packages/utils/__init__.py)导入名称“translate_sentence” - ImportError: cannot import name 'translate_sentence' from 'utils' (/usr/local/lib/python3.7/dist-packages/utils/__init__.py) Python追踪(最近一次通话)错误 - Python Traceback (most recent call last) error Traceback(最近一次通话最后一次)Python 错误 - Traceback (most recent call last) Python Error Python 错误回溯(最后一次调用): - Python Error Traceback (most recent call last): gitosis-init错误| 追溯(最近一次通话) - gitosis-init error | Traceback (most recent call last)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM