Skip to main content

Easy-to-read display of logs with save to db redis and sending notifications in telegram

Project description

Simple framework for easy-to-read display of logs with save to db redis and sending notifications in telegram.

Once I couldn't find a simple framework that does 3 things that are important to me:

  • marking messages with color in the terminal,
  • saving logs by day to the database redis,
  • sending important notifications in telegram.

Then it was created LogsWeasel.


log = LogsWeasel(settings={
    'redis_connection': {
        'db': {{id_database, default that 0, int}},
        'host': {{redis_host, str}}),
        'port': {{number_of_port, int}},
        'username': {{username, str}},
        'password': {{password, str}},
    'redis_files': {
        'prefix_file': 'logs_',
        'frequency': 'day'},
    'telegram_token': {{you_tg_token, str}},
    'users_list': {{[tg_id_int_1, tg_id_int_2, ...], list with int}})

Example use:

from logsweasel import LogsWeasel

log = LogsWeasel(settings={
    'redis_connection': {
        'db': int(os.getenv('redis_db')),
        'host': os.getenv('redis_host'),
        'port': int(os.getenv('redis_port')),
        'username': os.getenv('redis_username'),
        'password': os.getenv('redis_password')},
    'redis_files': {
        'prefix_file': 'logs_',
        'frequency': 'day'},
    'telegram_token': os.getenv('telegram_token'),
    'users_list': [int(os.getenv('user_tgid'))]})


def hello_world(def_name='Hello World'):
    try:
        log.info_start(def_name) # message starting
        message = 'Hello World #1'
        print(message)
        time.sleep(3) # working...
        if message == 'Hello World #1':
            log.info_done(def_name, send=True) # done message
        else:
            log.warning(def_name) # not done message
    except Exception:
        log.critical(def_name) # finish with error


hello_world()

where:

  • redis_connection - settings for your database redis.

redis_files:

  • prefix_file - name of your save in db redis. Default key in db redis: {prefix_file}{date}

  • frequency - you can use 3 types frequency for save logs in new key: day, week, month.

  • telegram_token - token to initialize your bot to send notifications

  • users_list - list of user IDs to whom notifications will be sent

* You can find out your telegram ID, for example, using a bot @my_id_bot

* You can also save some logs in a separate file (for even better readability), for this you can use the following construction:

log.info(def_name, postfix='another_key')
You save the key like this: {prefix_file}{date}{postfix}

* If you do not want to use redis, write None in key redis_connection

* If you do not want to use telegram, write None in key telegram_token


Default methods for save logs:

Information records/notifications. Marked in green in the terminal
log.info() - default message
log.info_start() - usually used when starting a function
log.info_done() - usually used when a function has completed successfully
log.info_not_done() - used when a function has not completed

Notices to watch out for. Marked in yellow in the terminal.
log.warning() - important low
log.warning_not_done() - important medium
log.warning_error() - important high

Critical notices. They have the ability to save in the terminal full descriptions of the errors that have occurred.
log.critical() - error with important high
log.critical_fatal() - error with important very high, х_х

* default argument for warning/critical - exc=True, that add traceback message

** Use telegram bot notifications with great care. A large number of users in the send list will affect the performance of the program as a whole!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

logs_weasel-2.2.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

logs_weasel-2.2-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file logs_weasel-2.2.tar.gz.

File metadata

  • Download URL: logs_weasel-2.2.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for logs_weasel-2.2.tar.gz
Algorithm Hash digest
SHA256 8a20de41d85557f6104927987f2e65053ce4fa5d02dba48e26cd55e5b3ed984a
MD5 6839d14b5910aa0e38ceb99800f2c15c
BLAKE2b-256 851a9cb4ee199846ca2abd81457e8bfa36d5e71cfaa0413cd1028dc8d57c751a

See more details on using hashes here.

File details

Details for the file logs_weasel-2.2-py3-none-any.whl.

File metadata

  • Download URL: logs_weasel-2.2-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for logs_weasel-2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 da47cecb553ed449ca1d322be58d7508dbc4257c80a22867eb142a75ee8d1e95
MD5 4986b07bd6e124c6461c327daa0bddb0
BLAKE2b-256 fa881d74582910c259f1d397071a96513719a1eb0c2afa1d8d83338d9fa3ac85

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page