Skip to main content

Custom Logging Module

Project description

All code should use Python standard log module instead of print() function.

Install

python3 setup.py install

Usage

Basic usage.

from alogs import get_logger

logger = get_logger('module_name')

logger.debug('Debug message')
logger.info('Info message')
logger.warning('Warning message')
logger.error('Error message')
logger.critical('Critical message')

Log file

Store logs into a file.

from alogs import get_logger

logger = get_logger('module_name', 'log_file.log')

logger.debug('Debug message')  # Debug messages *are not* logged into the file
logger.info('Info message')

Disable existing loggers

Prevent existing loggers to log.

from alogs import get_logger

logger = get_logger('module_name', disable_existing_loggers=True)

logger.debug('Debug message')

Change default log level

The default log level is DEBUG, but it can be changed:

from alogs import get_logger

logger = get_logger('module_name', root_level='warning')

logger.debug('Debug message')  # won't appear
logger.warning('Info message')

Test

To test it just execute the test.py:

~ python3 test.py

You should see:

*** Simple logs ***
DEBUG 2021-02-02 12:58:18,672 MainProcess:16581 test:test_simple_logs() test.py:7 [alogs] = Debug simple message
INFO 2021-02-02 12:58:18,672 MainProcess:16581 test:test_simple_logs() test.py:8 [alogs] = Info simple message
WARNING 2021-02-02 12:58:18,672 MainProcess:16581 test:test_simple_logs() test.py:9 [alogs] = Warning simple message
ERROR 2021-02-02 12:58:18,672 MainProcess:16581 test:test_simple_logs() test.py:10 [alogs] = Error simple message
CRITICAL 2021-02-02 12:58:18,672 MainProcess:16581 test:test_simple_logs() test.py:11 [alogs] = Critical simple message

*** Module logs ***
DEBUG 2021-02-02 12:58:18,672 MainProcess:16581 test:test_module_name_logs() test.py:16 [module_name] = Debug module message
INFO 2021-02-02 12:58:18,672 MainProcess:16581 test:test_module_name_logs() test.py:17 [module_name] = Info module message
WARNING 2021-02-02 12:58:18,673 MainProcess:16581 test:test_module_name_logs() test.py:18 [module_name] = Warning module message
ERROR 2021-02-02 12:58:18,673 MainProcess:16581 test:test_module_name_logs() test.py:19 [module_name] = Error module message
CRITICAL 2021-02-02 12:58:18,673 MainProcess:16581 test:test_module_name_logs() test.py:20 [module_name] = Critical module message

*** File logs ***
DEBUG 2021-02-02 12:58:18,673 MainProcess:16581 test:test_file_logs() test.py:25 [file_logs] = THIS MESSAGE SHOULD NOT APPEAR ON THE FILE
INFO 2021-02-02 12:58:18,673 MainProcess:16581 test:test_file_logs() test.py:26 [file_logs] = Info file message
WARNING 2021-02-02 12:58:18,715 MainProcess:16581 test:test_file_logs() test.py:27 [file_logs] = Warning file message
ERROR 2021-02-02 12:58:18,715 MainProcess:16581 test:test_file_logs() test.py:28 [file_logs] = Error file message
CRITICAL 2021-02-02 12:58:18,715 MainProcess:16581 test:test_file_logs() test.py:29 [file_logs] = Critical file message
* Logs written on file "test.log" *
INFO 02/Feb/2021:12:58:18 -0300 MainProcess:16581 test:test_file_logs() test.py:26 [file_logs] = Info file message
WARNING 02/Feb/2021:12:58:18 -0300 MainProcess:16581 test:test_file_logs() test.py:27 [file_logs] = Warning file message
ERROR 02/Feb/2021:12:58:18 -0300 MainProcess:16581 test:test_file_logs() test.py:28 [file_logs] = Error file message
CRITICAL 02/Feb/2021:12:58:18 -0300 MainProcess:16581 test:test_file_logs() test.py:29 [file_logs] = Critical file message

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

alogs-3.3.tar.gz (3.6 kB view details)

Uploaded Source

File details

Details for the file alogs-3.3.tar.gz.

File metadata

  • Download URL: alogs-3.3.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.6.8

File hashes

Hashes for alogs-3.3.tar.gz
Algorithm Hash digest
SHA256 5edcb637ff9b82f061955c9597ae79c215ce2f0d86f0c2e90526c37af2e203cb
MD5 892b87a0409ed8e6ee0752b9843f7a43
BLAKE2b-256 7cf12e4b8e03b2ca2fe81ca4a75efb0cb676352e7ca22404123e1b8e8083bd73

See more details on using hashes here.

Supported by

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