34π
I think this may work for you, assuming youβre only using one(or default) handler:
>>> import logging
>>> logger = logging.getLogger()
>>> logging.debug('wat wat')
>>> logger.handlers[0].flush()
Itβs kind of frowned upon in the documentation, though.
Application code should not directly instantiate and use instances of Handler. Instead, the Handler class is a base class that defines the interface that all handlers should have and establishes some default behavior that child classes can use (or override).
http://docs.python.org/2/howto/logging.html#handler-basic
And it could be a performance drain, but if youβre really stuck, this may help with your debugging.
26π
If the use case is that you have a python program that should flush its logs when exiting, use logging.shutdown()
.
From the python documentation:
logging.shutdown()
Informs the logging system to perform an orderly
shutdown by flushing and closing all handlers. This should be called
at application exit and no further use of the logging system should be
made after this call. [β¦]
- [Django]-Django: Open uploaded file while still in memory; In the Form Clean method?
- [Django]-Django dump data for a single model?
- [Django]-TypeError: data.forEach is not a function
7π
Django logging relies on the standard python logging module.
This module has a module-level method: logging.shutdown()
which flushes all of the handlers and shuts down the logging system (i.e. logging can not longer be used after it is called)
Inspecting the code of this function shows that currently (python 2.7) the logging module holds a list of weak references to all handlers in a module-level variable called _handlerList so all of the handlers can be flushed by doing something like
[h_weak_ref().flush() for h_weak_ref in logging._handlerList]
because this solution uses the internals of the module @Mikes solution above is better, but it relies on having access to a logger, it can be generalized as follows:
[h.flush() for h in my_logger.handlerList]
- [Django]-Create django super user in a docker container without inputting password
- [Django]-Is there a HAML implementation for use with Python and Django
- [Django]-Where's my JSON data in my incoming Django request?
1π
In Python3.8:
logging.shutdown( )
Check: https://docs.python.org/3.8/library/logging.html#logging.shutdown
- [Django]-Pypi see older versions of package
- [Django]-Django and Bootstrap: What app is recommended?
- [Django]-Django: using <select multiple> and POST
0π
It could be that the logger is not logging (seemingly not flushing) because an exception is being raised in a thread. The thread will need to catch its own exceptions and log them itself.
You cannot rely on a main thread to catch and log thread exceptions.
For example, a timer that runs a function periodically will have this behavior and that function will need to catch and log its exceptions.
- [Django]-Testing email sending in Django
- [Django]-Django authentication and Ajax β URLs that require login
- [Django]-Django Cannot set values on a ManyToManyField which specifies an intermediary model. Use Manager instead
0π
I had the same problem where I wanted my log.info
to be flushed to the std:out
. Turns out I just had to add a stream handler to stdout and it does the job.
logger.addHandler(logging.StreamHandler(sys.stdout)).
Also, add the right level for logging but thatβs obvious.
- [Django]-Form field description in django admin
- [Django]-Django project root self discovery
- [Django]-Django auto_now and auto_now_add
-3π
a simple function that always working for register you debug messsages while programming. dont use it for production, since it will not rotate:
def make_log(message):
import datetime
with open('mylogfile.log','a') as f:
f.write(f"{datetime.datetime.now()} {message}\n")
then use as
make_log('my message to register')
when to put on production, just comment the last 2 lines
def make_log(message):
import datetime
#with open('mylogfile.log','a') as f:
# f.write(f"{datetime.datetime.now()} {message}\n")
- [Django]-Django : Can't import 'module'. Check that module AppConfig.name is correct
- [Django]-Request.POST.get('sth') vs request.POST['sth'] β difference?
- [Django]-What is the SQL ''LIKE" equivalent on Django ORM queries?