Logging
Anthias uses the standard Python logging module.
Configuration
Logging is configured in the settings.py
file.
# Initiate logging
logging.basicConfig(level=logging.INFO,
format='%(message)s',
datefmt='%a, %d %b %Y %H:%M:%S')
This sets the default log level to INFO
.
Log Levels
You can control the log level for each component. The following levels are available:
DEBUG
: Most verbose, includes detailed information about the application’s execution.INFO
: Provides general information about the application’s state.WARNING
: Indicates potential problems that may not be critical.ERROR
: Reports errors that may disrupt the application’s operation.CRITICAL
: Signals serious errors that may require immediate attention.
The log level for each component can be set in the settings.py
file. For example, to set the log level for the requests
library to WARNING
, you can use the following code:
requests_log.setLevel(logging.WARNING)
Logging in Code
To log messages within your code, use the following functions:
logging.debug()
: Logs debug messages.logging.info()
: Logs informational messages.logging.warning()
: Logs warning messages.logging.error()
: Logs error messages.logging.critical()
: Logs critical messages.
For example, to log a debug message, you would use:
logging.debug('Starting viewer.py')
Disabling Logging
To disable logging for unit tests, use the logging.disable()
function:
# noqa: E402
logging.disable(logging.CRITICAL)
Examples
The following examples illustrate how logging is used in Anthias:
host_agent.py
:
logging.basicConfig()
logging.getLogger().setLevel(logging.INFO)
# Loop forever processing messages
subscriber_loop()
viewer.py
:
def load_settings():
"""
Load settings and set the log level.
"""
settings.load()
logging.getLogger().setLevel(
logging.DEBUG if settings['debug_logging'] else logging.INFO
)
try:
main()
except Exception:
logging.exception("Viewer crashed.")
raise
github.py
:
# in seconds
def handle_github_error(exc, action):
# After failing, dont retry until backoff timer expires
r.set('github-api-error', action)
r.expire('github-api-error', ERROR_BACKOFF_TTL)
# Print a useful error message
if exc.response:
errdesc = exc.response.content
else:
errdesc = 'no data'
logging.error(
'%s fetching %s from GitHub: %s', type(exc).__name__, action, errdesc
)
## Top-Level Directory Explanations
<a class='local-link directory-link' data-ref=".github/" href="#.github/">.github/</a> - This directory contains GitHub-specific configuration files and workflows for the project.
<a class='local-link directory-link' data-ref=".github/workflows/" href="#.github/workflows/">.github/workflows/</a> - This directory contains YAML files defining continuous integration and deployment workflows for GitHub Actions.
<a class='local-link directory-link' data-ref="ansible/" href="#ansible/">ansible/</a> - Ansible is an open-source configuration management and automation tool. This directory contains Ansible playbooks and roles for managing and configuring the project.
<a class='local-link directory-link' data-ref="ansible/roles/" href="#ansible/roles/">ansible/roles/</a> - This directory contains Ansible roles, which are reusable collections of tasks and configurations for managing specific aspects of a system.
<a class='local-link directory-link' data-ref="ansible/roles/network/" href="#ansible/roles/network/">ansible/roles/network/</a> - This role manages network configurations.
<a class='local-link directory-link' data-ref="ansible/roles/splashscreen/" href="#ansible/roles/splashscreen/">ansible/roles/splashscreen/</a> - This role manages the configuration of a splash screen.
<a class='local-link directory-link' data-ref="ansible/roles/system/" href="#ansible/roles/system/">ansible/roles/system/</a> - This role manages system-level configurations.
<a class='local-link directory-link' data-ref="anthias_app/" href="#anthias_app/">anthias_app/</a> - This directory contains the main application codebase for the project, likely written in Django.
<a class='local-link directory-link' data-ref="anthias_app/migrations/" href="#anthias_app/migrations/">anthias_app/migrations/</a> - This directory contains database migration files for the Django application.
<a class='local-link directory-link' data-ref="anthias_django/" href="#anthias_django/">anthias_django/</a> - This directory may contain additional Django-specific configuration files and code.
<a class='local-link directory-link' data-ref="api/" href="#api/">api/</a> - This directory contains the API codebase for the project.
<a class='local-link directory-link' data-ref="api/serializers/" href="#api/serializers/">api/serializers/</a> - This directory contains Django REST Framework serializers for converting data between Python objects and JSON.
<a class='local-link directory-link' data-ref="api/views/" href="#api/views/">api/views/</a> - This directory contains Django views for handling HTTP requests and rendering responses.
<a class='local-link directory-link' data-ref="bin/" href="#bin/">bin/</a> - This directory contains executable scripts for the project.
<a class='local-link directory-link' data-ref="lib/" href="#lib/">lib/</a> - This directory contains reusable Python modules and libraries for the project.
<a class='local-link directory-link' data-ref="static/" href="#static/">static/</a> - This directory contains static files, such as images, CSS, and JavaScript, that are served directly to the user by the web server.
<a class='local-link directory-link' data-ref="static/spec/" href="#static/spec/">static/spec/</a> - This directory contains test files for the static files.
<a class='local-link directory-link' data-ref="static/spec/jasmine/" href="#static/spec/jasmine/">static/spec/jasmine/</a> - This directory contains Jasmine test files.
<a class='local-link directory-link' data-ref="templates/" href="#templates/">templates/</a> - This directory contains HTML templates used to render dynamic content.
<a class='local-link directory-link' data-ref="tests/" href="#tests/">tests/</a> - This directory contains test files for the project.
<a class='local-link directory-link' data-ref="tools/" href="#tools/">tools/</a> - This directory contains tools and scripts used to develop and maintain the project.
<a class='local-link directory-link' data-ref="tools/image_builder/" href="#tools/image_builder/">tools/image_builder/</a> - This tool likely builds and optimizes images for the project.
<a class='local-link directory-link' data-ref="website/" href="#website/">website/</a> - This directory contains the website codebase.
<a class='local-link directory-link' data-ref="website/bin/" href="#website/bin/">website/bin/</a> - This directory contains website executable scripts.
<a class='local-link directory-link' data-ref="webview/" href="#webview/">webview/</a> - This directory likely contains configuration files and code for a webview component.
<a class='local-link directory-link' data-ref="webview/res/" href="#webview/res/">webview/res/</a> - This directory contains webview resources, such as images and XML files.
<a class='local-link directory-link' data-ref="webview/src/" href="#webview/src/">webview/src/</a> - This directory contains webview source code.