Python Forum
keeping logs for every success fail attempt
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
keeping logs for every success fail attempt
#1
hi all,

made a flask-wtf password website but now i want to keep logs on everytime someone changes there password either success or a failed attempt

i have loads of if statements and try/except so il add the logging to that

as this is on a windows machine can i save the logs to a file and make the file name into the time/date variable, i want it to minitor 24/7 so the logs will pile up

thanks,
rob
Reply
#2
ok found it, il have a play

https://docs.python.org/3/howto/logging.html
Reply
#3
You may find this interesting.

https://docs.python.org/3/library/loggin...ilehandler
Reply
#4
I would adive to use loguru
from loguru import logger
logger.remove() # Only info to file,comment out to see live
logger.add("error.log", rotation="2 day")

@logger.catch
def foo():
    return 1 / 0

foo()
Output:
2024-07-02 18:43:12.613 | ERROR | __main__:<module>:9 - An error has been caught in function '<module>', process 'MainProcess' (17440), thread 'MainThread' (16908): Traceback (most recent call last): > File "C:\code\po_env\log_py.py", line 9, in <module> foo() â”” <function foo at 0x00000258C8B55BC0> File "C:\code\po_env\log_py.py", line 7, in foo return 1 / 0 ZeroDivisionError: division by zero
So simpler and better to use,error is logged with date and all info to error.log
Reply
#5
thanks guys

how does it know what is a debug, info, warning, error, critical message, or is it up to me to say

and i know i can save the file to linux but will it save it to windows as well
Reply
#6
(Jul-02-2024, 05:35 PM)robertkwild Wrote: how does it know what is a debug, info, warning, error, critical message, or is it up to me to say
Just do simple teste,eg info such as the start and end of processes.
Typically debug used to capture detailed information about the application’s behavior, variables,and internal processes.
from loguru import logger
logger.add("loop.log", rotation="1 week")
import time

start = logger.info('Start of loop')
targets = ["a", "b", "c"]
for target in targets:
    time.sleep(5)
    logger.debug(f"Log file: {target}")
end = logger.info('End of loop')
Output:
2024-07-02 20:24:34.158 | INFO | __main__:<module>:5 - Start of loop 2024-07-02 20:24:39.162 | DEBUG | __main__:<module>:9 - Log file: a 2024-07-02 20:24:44.163 | DEBUG | __main__:<module>:9 - Log file: b 2024-07-02 20:24:49.165 | DEBUG | __main__:<module>:9 - Log file: c 2024-07-02 20:24:49.165 | INFO | __main__:<module>:10 - End of loop
(Jul-02-2024, 05:35 PM)robertkwild Wrote: but will it save it to windows as well
I use Windows in these examples,loguru do of course works on all OS.
Reply
#7
thanks guys the logging is working really well, this is a snippet of my code

logger = logging.getLogger(__name__)
logging.basicConfig(filename='password.log', format='%(asctime)s:%(levelname)s:%(message)s', datefmt='%d/%m/%Y %H:%M:%S', encoding='utf-8', level=logging.DEBUG)
and ive made logs for all the if/try/except statements in my code like

logger.warning(f'{form.un.data}your current password is wrong, please click back and try again')
now i want to make new log file for every date ie 04/07/2024, 05/07/2024 etc etc, trying to do that now
Reply
#8
ok done this and no errors on the console or log

from flask import Flask, render_template, request
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, DecimalField, RadioField, SelectField, TextAreaField, FileField, SubmitField
from wtforms.validators import InputRequired, Length, DataRequired, EqualTo, Regexp, ValidationError
import re
import logging
from logging.handlers import TimedRotatingFileHandler
import time
import subprocess
import smtplib
from email.mime.text import MIMEText

logger = logging.getLogger(__name__)
logging.basicConfig(filename='password.log', format='%(asctime)s:%(levelname)s:%(message)s', datefmt='%d/%m/%Y %H:%M:%S', encoding='utf-8', level=logging.DEBUG)
handler = TimedRotatingFileHandler('password.log', when='D', interval=120)
basically i want to rotate logs every 3 months and start again ie overwrite old ones
Reply
#9
(Jul-04-2024, 12:18 PM)robertkwild Wrote: ok done this and no errors on the console or log

from flask import Flask, render_template, request
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, DecimalField, RadioField, SelectField, TextAreaField, FileField, SubmitField
from wtforms.validators import InputRequired, Length, DataRequired, EqualTo, Regexp, ValidationError
import re
import logging
from logging.handlers import TimedRotatingFileHandler
import time
import subprocess
import smtplib
from email.mime.text import MIMEText

logger = logging.getLogger(__name__)
logging.basicConfig(filename='password.log', format='%(asctime)s:%(levelname)s:%(message)s', datefmt='%d/%m/%Y %H:%M:%S', encoding='utf-8', level=logging.DEBUG)
handler = TimedRotatingFileHandler('password.log', when='D', interval=120)
basically i want to rotate logs every 3 months and start again ie overwrite old ones

doesnt work, changed the D to a S and made it 1 day instead of 120 and didnt rotate logs
Reply
#10
Use loguru as suggested by @snippsat. Much simpler to use.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  simple if fail, what am I missing? ajkrueger25 2 743 Nov-13-2024, 04:21 AM
Last Post: ajkrueger25
Question PDF Automation with Unique ID + Logs in xls File pollos 1 764 Jul-09-2024, 04:19 PM
Last Post: Pedroski55
  time difference bettwenn logs enkliy 14 3,341 Nov-21-2023, 04:51 PM
Last Post: rob101
  Why does [root.destroy, exit()]) fail after pyinstaller? Rpi Edward_ 4 1,759 Oct-18-2023, 11:09 PM
Last Post: Edward_
  How to calculated how many fail in each site(s) in csv files SamLiu 4 2,285 Sep-26-2022, 06:28 AM
Last Post: SamLiu
  Keeping up with IDEs and Virtual Environments... bytecrunch 7 6,748 Sep-05-2022, 08:04 PM
Last Post: snippsat
  Keeping a value the same despite changing the variable it was equated to TheTypicalDoge 2 2,278 Mar-13-2022, 10:50 PM
Last Post: Yoriz
  Imports that work with Python 3.8 fail with 3.9 and 3.10 4slam 1 3,448 Mar-11-2022, 01:50 PM
Last Post: snippsat
  Bot refuses to count logs. M1racle 0 1,727 Dec-13-2021, 06:42 PM
Last Post: M1racle
  [SOLVED] Why does regex fail cleaning line? Winfried 5 3,482 Aug-22-2021, 06:59 PM
Last Post: Winfried

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020