Posts: 88
Threads: 20
Joined: Jul 2023
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
Posts: 88
Threads: 20
Joined: Jul 2023
Posts: 6,779
Threads: 20
Joined: Feb 2020
Posts: 7,313
Threads: 123
Joined: Sep 2016
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
Posts: 88
Threads: 20
Joined: Jul 2023
Jul-02-2024, 05:35 PM
(This post was last modified: Jul-02-2024, 05:35 PM by robertkwild.)
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
Posts: 7,313
Threads: 123
Joined: Sep 2016
(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.
Posts: 88
Threads: 20
Joined: Jul 2023
Jul-04-2024, 10:48 AM
(This post was last modified: Jul-04-2024, 10:48 AM by robertkwild.)
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
Posts: 88
Threads: 20
Joined: Jul 2023
Jul-04-2024, 12:18 PM
(This post was last modified: Jul-04-2024, 12:18 PM by robertkwild.)
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
Posts: 88
Threads: 20
Joined: Jul 2023
Jul-04-2024, 04:52 PM
(This post was last modified: Jul-04-2024, 04:53 PM by robertkwild.)
(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
Posts: 6,779
Threads: 20
Joined: Feb 2020
Use loguru as suggested by @ snippsat. Much simpler to use.
|