I've been reading the docs and blogs all day trying to make sure I understand how to correctly log in django and I believe you were the one to bring it all together for me. Great examples, thank you so much!
Thanks... very informative. One question - there is a specific requirement in one of my project to implement dynamic log level change without restarting Django app. Can you please share information around that as well?
@@technologyfornoobs can we do that in a production deployed app? or should we use some file like file.conf where we will update at runtime? explored a lot but did not find any concrete answers... could you please guide? the ask is supoose an app is running at INFO level, due to some issues there needs to be a change to type DEBUG but app should not be restarted.
@@ShailajaKaustav Now I understand, I believe you are thinking of this to be done while launching the django server, I am saying you do not worry about it how to launch the server app, but have your logs accordingly and do it in the logging level rather than server start settings.
hello sir, i am doing a project in Django on E-Banking Log System in my college. I am facing some problem while generating the logs. Could you please help me with it sir.
Hi! I am trying to create a history table that records all updates to the Product model. For example, if I have a model Product (name, price, description, status) and a history model (user,field,old_value, new_value, timestamp). I would like to be able to keep a history of this product's field changes overtime from creation up to now My history model would save the following values into history On create it saves as follow user = currentuser, field=name, old_value=None, new_value='Product', timestamp= 09/20/2020 10:12:02am user = currentuser, field=price, old_value=None, new_value=1, timestamp= 09/20/2020 10:12:02am user = currentuser, field=description, old_value=None, new_value="Product description", timestamp= 09/20/2020 10:12:02am user = currentuser, field=status, old_value=None, new_value='New;, timestamp= 09/20/2020 10:12:02am I then decide to update some fields fields price changes to 1 to 2 and status changes from new to old user = currentuser, field=price, old_value=1, new_value=2, timestamp= 09/21/2020 10:12:02am user = currentuser, field=status,old_value=new, new_value=old, timestamp=09/21/2020 10:12:02am I did some research and people recommend doing this by modifying the save method and some recommend using signals (pre_save, post_save). Do you have any recommendations? Or better yet, can you create a tutorial? I feel like this would be very useful :) Thanks!
What I understood from your question is that you want to save all the info as soon as any new info comes you still want to save the old value. The biggest problem I see is how you are going to tell which record is most latest, as they are not clubbed together for creating single row info (name, product, price, etc) with the latest info of all the field, You need to get the latest for all the fields and then club them together which is an overkill for the task I guess. My suggestion > just simply have previous and current as a separate column in your model ('previous_name, current_name, previous_price, current_price, previous_descp, current_descp) and just simply update the current with new and previous with old. How you will do it? user will send the form, you take the filled value and just add the previous in "previous" and latest in the current field column.
@@technologyfornoobs I think the request is looking for is some kind of audit trail or audit log in Django project. The following links from PHP framework could be useful to adopt to the Python/Django project: www.radicore.org/viewarticle.php?article_id=4 and www.tonymarston.net/php-mysql/auditlog.html His recommendation of a separate database to host an audit logging facility at an application level is good but the challenge (disclaimer: I hear other ppl and had not confirmed) due to current Django version not supporting multiple databases. Lastly, with a few months with Django, the use of Django Signals (esp. post_save) may come handy here by implementing separately from the Product app. In any case, @Technology for Noobs for a great tutorial very clear. In production, I have been wondering how to go around the Server Error (500) in a website especially when DEBUG=False. Thanx..
Hi, I tried your way for logging but i am getting this error raise ValueError('Unable to configure handler ' ValueError: Unable to configure handler 'file'
Hi, I would still suggest cloning the project from github (github.com/sharmasw/DjangoLoggingApp) and run in your system, and you would be able to check the missing pieces in your code. If you look at the end of the dictionary below which is in the settings of the project, you will find the 'simpleRe' defined. this is missing in your settings for logging. LOGGING ={ 'version':1, 'loggers':{ 'django':{ 'handlers':['file','file2'], 'level':'DEBUG' } }, 'handlers':{ 'file':{ 'level':'INFO', 'class': 'logging.FileHandler', 'filename':'./logs/debug5.log', 'formatter':'simpleRe', }, 'file2':{ 'level':'DEBUG', 'class': 'logging.FileHandler', 'filename':'./logs/debug6.log', 'formatter':'simpleRe', } }, 'formatters':{ 'simpleRe': { 'format': '{levelname} {asctime} {module} {process:d} {thread:d} {message}', 'style': '{', } } }
Hi, thanks for this video, though I tried and played around with logging configuration, still not able to log server errors in a file or in admin mails. What could be the reason for this?? I am not getting any errors for this code but whenever server sends 500, it is not logged. LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'console': { 'format': '%(name)-12s %(levelname)-8s %(message)s', 'style': '{' }, 'file': { 'format': '%(asctime)s %(name)-12s %(levelname)-8s %(message)s', 'style': '{' } }, 'filters': { 'require_debug_true': { '()': 'django.utils.log.RequireDebugTrue', }, 'require_debug_false': { '()': 'django.utils.log.RequireDebugFalse', } }, 'handlers': { 'console': { 'class': 'logging.StreamHandler', 'formatter': 'console' }, 'file': { 'level': 'INFO', 'class': 'logging.handlers.RotatingFileHandler', 'formatter': 'file', 'filename': os.path.join(BASE_DIR, 'server_error.log'), }, 'mail_admins': { 'level': 'INFO', 'class': 'django.utils.log.AdminEmailHandler', 'filters': [], 'include_html': True, } }, 'loggers': { '' :{ 'handlers': ['file','mail_admins'], 'level': 'INFO', 'propagate': True, }, 'django': { 'handlers': ['file','mail_admins'], 'level': 'INFO', 'propagate': True, }, }, }
I've been reading the docs and blogs all day trying to make sure I understand how to correctly log in django and I believe you were the one to bring it all together for me. Great examples, thank you so much!
Great to hear!
11:57 had me cracking up! "I'm in the logger, if you care...." bahahaha
Best explaination i found on youtube till now, you know how to teach things, you got teaching skills, never stop this...Thanks for amazing video
You're very welcome!
Thank you for your unique explaination, I came for this video by accident.😍
You're welcome 😊
@@technologyfornoobs Keep doing such stuff and motivate us to learn & grow with you🤞
Really helpful brother❤
Glad to hear that
What a explationation, never seen ever. 👌👌
Thanks!
Best out there sir, good job
Thanks!
Great teacher. Thanks bro.
Welcome
Bravo !!! It was an awesome explanation... thanks
Glad it was helpful!
This is a great video. Thanks for your example and your explanation.
You're very welcome!
Thank you for such a wonderful explanation about Django logging module.👏
You are welcome!
Good explanation sir 🎉🎉❤
Thanks and welcome
Superb bro great work help me a lot..🥰🥰🥰💝💝
Welcome 😊
Superb !!! 🔥🔥🔥👌
Thank you so much 😀
Great explanation! Thanks a heap!
You're welcome!
Not a stack?
Thanks... very informative. One question - there is a specific requirement in one of my project to implement dynamic log level change without restarting Django app. Can you please share information around that as well?
You would just use if else statements
@@technologyfornoobs can we do that in a production deployed app? or should we use some file like file.conf where we will update at runtime? explored a lot but did not find any concrete answers... could you please guide? the ask is supoose an app is running at INFO level, due to some issues there needs to be a change to type DEBUG but app should not be restarted.
@@ShailajaKaustav Now I understand, I believe you are thinking of this to be done while launching the django server, I am saying you do not worry about it how to launch the server app, but have your logs accordingly and do it in the logging level rather than server start settings.
@@technologyfornoobs Thanks a lot
Good explanation
Thanks and welcome
tysm sir for this video and make video on emaillogger
Will check about it.
Nice
Thanks
is there any video to call a python script from html page with input parameters using django?
Not exactly, but it's possible u need to use "exec" and eval
hello sir,
i am doing a project in Django on E-Banking Log System in my college. I am facing some problem while generating the logs. Could you please help me with it sir.
What I can do here?
@@technologyfornoobs Thank you for your reply sir, Could you please provide me with any means of personal contact with you (like your email info etc.)
techfornoobs01@gmail.com
@@technologyfornoobs Sir, I have mailed you
can you do authentication with otp in django
OK, there are so many things which can do that like oauth and oauth2 , will update in a while
thank u so much...i tried but many errors are occurred.
can we store loggers and print them in some file
yes, that's what is in the video, or I am missing something.
Great!
Yo!
why is this not working when I run the server with >> mod_wsgi-express start ??
Thanks sir
All the best
How to rotate log in django. Same as java log4j2 is working. TimedRotation is not working. It truncate file while rotating
How to add only the django logs in to text file, I tried the same but the logs are not storing in the text file
That's the video is about follow from 11:20
I followed the same sir, but the logs are not printing in text file, do I need to install any additional module for this?
Pull from github and check of the code works in yours and then check the diff what you are missing
how you can pass variables from django views.py to jupyter notebook ???
Not possible!
@@technologyfornoobs can i make code of jupyter notebook in my django project ? and where ? please i need help
Code of jupyter notebook in django, you need to write the code of jupyter notebook into views or else but inside the project
how can i get user name in log?
logger.info('add user')
If you make more videos like this!
Sure!
@@technologyfornoobs thanks.
waiting for that..
the logger is unable to write on file .although file is created but after re-deploying still it create the file but not writing and 'level':'DEBUG',
can you tell me what can be reasons?
might be you are looking for Level INFO and level DEBUG is filtering it, change it to INFO and you would see all the logs
@@technologyfornoobs Actually I am looking for default generate logs at the development server and writing back to that file.
Possible reason, you have missed the handler in logging setup:
'loggers':{
'django':{
'handlers':['file','file2'],
'level':'DEBUG'
}
},
LOGGING = {
'version': 1 ,
'disable_existing_loggers': True ,
'loggers': {
'Django': {
'handlers': ['pathd'] ,
'level': 'DEBUG'
}
} ,
'handlers': {
'pathd': {
'level': 'DEBUG' ,
'class': 'logging.FileHandler',
'filename': './log/maverick/m.log' ,
}
},
}
here is my file .Have a look
Hi! I am trying to create a history table that records all updates to the Product model.
For example, if I have a model Product (name, price, description, status) and a history model (user,field,old_value, new_value, timestamp).
I would like to be able to keep a history of this product's field changes overtime from creation up to now
My history model would save the following values into history
On create it saves as follow
user = currentuser, field=name, old_value=None, new_value='Product', timestamp= 09/20/2020 10:12:02am
user = currentuser, field=price, old_value=None, new_value=1, timestamp= 09/20/2020 10:12:02am
user = currentuser, field=description, old_value=None, new_value="Product description", timestamp= 09/20/2020 10:12:02am
user = currentuser, field=status, old_value=None, new_value='New;, timestamp= 09/20/2020 10:12:02am
I then decide to update some fields
fields price changes to 1 to 2 and status changes from new to old
user = currentuser, field=price, old_value=1, new_value=2, timestamp= 09/21/2020 10:12:02am
user = currentuser, field=status,old_value=new, new_value=old, timestamp=09/21/2020 10:12:02am
I did some research and people recommend doing this by modifying the save method and some recommend using signals (pre_save, post_save).
Do you have any recommendations? Or better yet, can you create a tutorial? I feel like this would be very useful :)
Thanks!
What I understood from your question is that you want to save all the info as soon as any new info comes you still want to save the old value. The biggest problem I see is how you are going to tell which record is most latest, as they are not clubbed together for creating single row info (name, product, price, etc) with the latest info of all the field, You need to get the latest for all the fields and then club them together which is an overkill for the task I guess. My suggestion > just simply have previous and current as a separate column in your model ('previous_name, current_name, previous_price, current_price, previous_descp, current_descp) and just simply update the current with new and previous with old.
How you will do it?
user will send the form, you take the filled value and just add the previous in "previous" and latest in the current field column.
@@technologyfornoobs I think the request is looking for is some kind of audit trail or audit log in Django project. The following links from PHP framework could be useful to adopt to the Python/Django project: www.radicore.org/viewarticle.php?article_id=4 and www.tonymarston.net/php-mysql/auditlog.html
His recommendation of a separate database to host an audit logging facility at an application level is good but the challenge (disclaimer: I hear other ppl and had not confirmed) due to current Django version not supporting multiple databases.
Lastly, with a few months with Django, the use of Django Signals (esp. post_save) may come handy here by implementing separately from the Product app.
In any case, @Technology for Noobs for a great tutorial very clear. In production, I have been wondering how to go around the Server Error (500) in a website especially when DEBUG=False.
Thanx..
Hi,
I tried your way for logging but i am getting this error
raise ValueError('Unable to configure handler '
ValueError: Unable to configure handler 'file'
If you have not created the log folder in your root path, this could create the issue, can you clone the repo and check where you are making mistake.
@@technologyfornoobs it is giving formatter error also
raise ValueError('Unable to set formatter '
ValueError: Unable to set formatter 'simpleRe'
Hi, I would still suggest cloning the project from github (github.com/sharmasw/DjangoLoggingApp) and run in your system, and you would be able to check the missing pieces in your code.
If you look at the end of the dictionary below which is in the settings of the project, you will find the 'simpleRe' defined. this is missing in your settings for logging.
LOGGING ={
'version':1,
'loggers':{
'django':{
'handlers':['file','file2'],
'level':'DEBUG'
}
},
'handlers':{
'file':{
'level':'INFO',
'class': 'logging.FileHandler',
'filename':'./logs/debug5.log',
'formatter':'simpleRe',
},
'file2':{
'level':'DEBUG',
'class': 'logging.FileHandler',
'filename':'./logs/debug6.log',
'formatter':'simpleRe',
}
},
'formatters':{
'simpleRe': {
'format': '{levelname} {asctime} {module} {process:d} {thread:d} {message}',
'style': '{',
}
}
}
How can we contact you sir.
Email me @ techfornoobs01@gmail.com
Hi, thanks for this video, though I tried and played around with logging configuration, still not able to log server errors in a file or in admin mails. What could be the reason for this?? I am not getting any errors for this code but whenever server sends 500, it is not logged.
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'console': {
'format': '%(name)-12s %(levelname)-8s %(message)s',
'style': '{'
},
'file': {
'format': '%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
'style': '{'
}
},
'filters': {
'require_debug_true': {
'()': 'django.utils.log.RequireDebugTrue',
},
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse',
}
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'console'
},
'file': {
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'file',
'filename': os.path.join(BASE_DIR, 'server_error.log'),
},
'mail_admins': {
'level': 'INFO',
'class': 'django.utils.log.AdminEmailHandler',
'filters': [],
'include_html': True,
}
},
'loggers': {
'' :{
'handlers': ['file','mail_admins'],
'level': 'INFO',
'propagate': True,
},
'django': {
'handlers': ['file','mail_admins'],
'level': 'INFO',
'propagate': True,
},
},
}
you need to handle 500 error responses, check for default 500 error in django
You didn't explain the filter concept
I think I forgot, I will see when can I include it.
logger.info('some message') i am not seeing this in debug3.log file
check the settings.