cookiecutter-python-app | Cookiecutter template for a Python application
kandi X-RAY | cookiecutter-python-app Summary
kandi X-RAY | cookiecutter-python-app Summary
Cookiecutter template for a Python application project.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Parse command line arguments
- Handles hello command
- Load config from file
- Add resolver
cookiecutter-python-app Key Features
cookiecutter-python-app Examples and Code Snippets
Community Discussions
Trending Discussions on cookiecutter-python-app
QUESTION
I'm somewhat new to Python, especially in writing modules and functions in several files and not just raw scripts.
I'm writing a command line app and I would like to have a single function (I call it argpconf
) that will parse the command line arguments and set a log level accordingly. Most importantly, I'd like the log level to be set once in this function and to be the same across all modules with minimum overhead when creating their loggers. Additionally, I would like to be able to identify the module from which the message came while using the common formatter:
ANSWER
Answered 2018-Oct-17 at 18:28This approach seems to be making things more complicated than they need to be. I realise this is coming from the cookiecutter template you used, and it's just my opinion, but the approach to logging taken in that template isn't what I would regard as best practice. You know your use case best, but if all I wanted was to
have a single function that will parse the command line arguments and set a log level accordingly. Most importantly, I'd like the log level to be set once in this function and to be the same across all modules with minimum overhead when creating their loggers. Additionally, I would like to be able to identify the module from which the message came while using the common formatter
then the simplest approach is to import argparse
and logging
in your main script, process command line arguments and set the logging level accordingly, call basicConfig()
(as suggested in Brian M. Sheldon's comment) and then dispatch to your application endpoint as determined by command line arguments. Every module you use that needs to log something just needs to import logging
and logger = logging.getLogger(__name__)
and then logger.debug(...)
or whatever, wherever needed in that module. If you stick to this, all modules will use the logging level set in basicConfig()
automatically, and if you use the fragment %(name)s
in the format=
argument to basicConfig()
, then you will see the full module name in that place in the logged messages, as a fully-qualified dotted name (like api.some_functionality
). This approach will certainly have less overhead in creating loggers than the cookiecutter template does.
Update: I'll update the Python Logging Cookbook with an example. For now, here's a Gist with just the code.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install cookiecutter-python-app
You can use cookiecutter-python-app like any standard Python library. You will need to make sure that you have a development environment consisting of a Python distribution including header files, a compiler, pip, and git installed. Make sure that your pip, setuptools, and wheel are up to date. When using pip it is generally recommended to install packages in a virtual environment to avoid changes to the system.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page