Django settings module...not possible to run the logger? - python

I use the python logging module in my my_app/views.py file with no problem.
I tried to use it inside the staging_settings.py that I use for my configuration. But the messages never reach the error log, whilst the messages from the views.py work fine.
my staging_settings.py:
LOGGING= { ..redacted for space.. } # the logging config-dict
import logging
logger = logging.getLogger(__name__)
logger.debug(F"this is staging_ setup")
Is it just an impossibility to run a logger inside a setup.py? I guess the server actually isn't running yet, so there is nothing for the logger to send to? I'm running this on pythonanywhere, so I don't really have a console to simply print() to or some other hack alternative.

Logging isn't configured at the time the settings file is imported, so you can't do logging from the module level in it. The sequence is
Django imports the settings from your staging_settings.py
Django fetches the LOGGING dict with the configuration from that imported module
Django configures logging with the configuration
When you log anything after that, logging then starts to output stuff as per your configuration
You can't expect to log and get log output in step 1.
Update: AFAIK there's no Django-version-independent way of determining when logging has been configured. You could define your own filter which sets a flag when instantiated, which would happen during configuration. Anyway, that doesn't help you if you need to log from the settings module in module-level code. However, functions that are defined in the settings module and are called later (after logging configuration has happened) can do logging.

Related

Replace Flask's logger with a custom one imported from another module

In my program I have several custom loggers in a file called loggers.py, this file initializes them with custom settings and saves them as variables using logging.getLogger().
Then in each module of my system I import the corresponding logger as a variable, and use it to log to its respective file.
Now everything works fine this way, but I'm having trouble in my webapi.py script, which uses Flask to create some endpoints to handle some requests. I want Flask to use the logger I imported as a variable, but no matter what I try it still uses the custom one.
I tried to assign my custom logger to app.logger and also removing handlers from flask's logger with app.logger.removeHandler(default_handler), but none of those worked. The only solution I could find was to configure the logger in the same script, but it defeats the purpose of having a single script that contains all loggers and their settings.
Is there really no way to simply override Flask's internal logger with a custom one imported from another module?

Flask application - What is the difference between using logging and flask app.logger for a very basic flask application? [duplicate]

I'm building a website using Flask and I'm now in the process of adding some logging to it for which I found these docs. The basic example is as follows:
if not app.debug:
import logging
from themodule import TheHandlerYouWant
file_handler = TheHandlerYouWant(...)
file_handler.setLevel(logging.WARNING)
app.logger.addHandler(file_handler)
after which you can log using app.logger.error('An error occurred'). This works fine, but apart from the fact that I do not see any advantage over the regular python logging module I also see a major downside: if I want to log outside of a request context (when for example running some code with a cron job) I get errors because I'm using app outside of the request context.
So my main question; why would I use the Flask logger at all? What is the reason that it was ever built?
The Flask logger uses the "generic" Python logging, it's a logging.getLogger(name) like any other.
The logger exists so that Flask app and views can log things that happen during execution. For example, it will log tracebacks on 500 errors during debug mode. The configuration example is there to show how to enable these logs, which are still useful in production, when you are not in debug mode.
Having an internal logger is not unique to Flask, it's the standard way to use logging in Python. Every module defines it's own logger, but the configuration of the logging is only handled by the last link in the chain: your project.
You can also use app.logger for your own messages, although it's not required. You could also create a separate logger for your own messages. In the end, it's all Python logging.

Logging DEBUG logs are not shown when executing the Python Azure Functions

I have created a Python Azure Functions app. In this application I want to check log details like DEBUG, INFO etc. I have written some code for logging purpose, but I am not able to get any log after executing my azure function application.
I have written basic code for logging purpose as below but after executing Azure Functions I am not able to see the log on console.
import logging
import azure.functions as func
data = "Hello"
logging.basicConfig(level=logging.DEBUG)
logging.debug(data)
Is there any other solution or workaround for the above problem?
Most likely the root logger got messed with by azure and basicConfig only creates a root logger with some sane defaults. To work around this create your own independent logger.
import logging
logger = logging.getLogger('akshay')
logger.setLevel(logging.DEBUG)
sh = logging.StreamHandler()
sh.setLevel(logging.DEBUG)
logger.addHandler(sh)
logger.debug('Hello')
# Outputs 'Hello'
This GitHub issue explains the solution:
As for the original issue noted here, the desired behavior can be achieved with a combination of two steps:
Setting the root logger in user code to use the debug level (it is set to to info by default).
Configuring the host.json to log Debug messages: [...]
Specifically, this is the code that you need in your script:
import logging
logging.Logger.root.level = 10
logging.debug("Debug message here")

What is the advantage of flask.logger over the more generic python logging module?

I'm building a website using Flask and I'm now in the process of adding some logging to it for which I found these docs. The basic example is as follows:
if not app.debug:
import logging
from themodule import TheHandlerYouWant
file_handler = TheHandlerYouWant(...)
file_handler.setLevel(logging.WARNING)
app.logger.addHandler(file_handler)
after which you can log using app.logger.error('An error occurred'). This works fine, but apart from the fact that I do not see any advantage over the regular python logging module I also see a major downside: if I want to log outside of a request context (when for example running some code with a cron job) I get errors because I'm using app outside of the request context.
So my main question; why would I use the Flask logger at all? What is the reason that it was ever built?
The Flask logger uses the "generic" Python logging, it's a logging.getLogger(name) like any other.
The logger exists so that Flask app and views can log things that happen during execution. For example, it will log tracebacks on 500 errors during debug mode. The configuration example is there to show how to enable these logs, which are still useful in production, when you are not in debug mode.
Having an internal logger is not unique to Flask, it's the standard way to use logging in Python. Every module defines it's own logger, but the configuration of the logging is only handled by the last link in the chain: your project.
You can also use app.logger for your own messages, although it's not required. You could also create a separate logger for your own messages. In the end, it's all Python logging.

django logging: log is not created

I'm running my app on the GAE development server, with app-engine-patch to run Django.
One of my views is bugged , so I want to log everything that happens.
I added in myapp.views:
import logging
LOG_FILENAME = '/mylog.txt'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)
and my function is:
def function(string):
logging.debug('new call')
#do stuff
#logging.debug('log stuff')
My problem is that I can't find the log. When I run my app I get no errors, but the log is not created.
I also tried various paths: /mylog.txt ,mylog.txt , c:\mylog.txt, c:\complete\path\to \my\app\mylog.txt, but it doesn't work.
On the other hand I tried to create and run a separate test:
#test.py
import logging
LOG_FILENAME = '/mylog.txt'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)
logging.debug('test')
And the log is created without problems: c:\mylog.txt
I'm not familiar with logging so I don't know if there might be some issues with django, or appengine.
Thanks
You can't write to files on App Engine - thus, any attempt to log to text files is also doomed to failure. Log output will appear on the SDK console in development, or in the logs console in production.
I am guessing the problem is that you put your log configuration in a view. A general rule of thumb for django logging is to set the log configuration in your settings.py.
By default, dev_appserver suppresses the debug log. To turn it on, run it with the --debug option. Note that dev_appserver itself uses the same logger, and will spew lots of debugging stuff you might not care about.
Your logging will print to stderr.

Categories