记录错误,运行记录程序时参数错误TypeError

问题描述 投票:0回答:1

[当我尝试制作键盘记录器时,每次按下键时都会出现此错误(在本例中为d)。下面是我的代码,这是我的错误日志:

--- Logging error ---
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\logging\__init__.py", line 1025, in emit
    msg = self.format(record)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\logging\__init__.py", line 869, in format
    return fmt.format(record)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\logging\__init__.py", line 611, in format
    s = self.formatMessage(record)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\logging\__init__.py", line 580, in formatMessage
    return self._style.format(record)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\logging\__init__.py", line 422, in format
    return self._fmt % record.__dict__
TypeError: not enough arguments for format string
Call stack:
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\threading.py", line 890, in _bootstrap
    self._bootstrap_inner()
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\threading.py", line 926, in _bootstrap_inner
    self.run()
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\pynput\_util\__init__.py", line 144, in run
    self._run()
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\pynput\_util\win32.py", line 353, in _run
    self._process(msg.wParam, msg.lParam)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\pynput\_util\__init__.py", line 162, in inner
    return f(self, *args, **kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\pynput\keyboard\_win32.py", line 240, in _process
    self.on_press(key)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\pynput\_util\__init__.py", line 78, in inner
    if f(*args) is False:
  File "C:\Users\User\Desktop\Random programs\Keyloggar.pyw", line 11, in on_press
    logging.info(key)
Message: 'd'
Arguments: ()

这是我的代码:

from pynput.keyboard import Key, Listener
import logging

#Defining the directory as well as the key strokes
log_dir = "C:/Users/User/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup/"
logging.basicConfig(filename=(log_dir+"key_logger.txt"), level=logging.DEBUG, format = '% 
(asctime)s:%s(messages)')

def on_press(key):
    logging.info(key)
#Giving access of me to myself
with Listener(on_press=on_press) as Listener:
    Listener.join()

有人可以告诉我我做错了什么吗?我导入模块错误吗?

python python-3.x logging arguments typeerror
1个回答
0
投票

您的日志记录格式错误。

只需将format = '% (asctime)s:%s(messages)'替换为format='%(asctime)s - %(message)s'

© www.soinside.com 2019 - 2024. All rights reserved.