Skip to main content

Your goto Python logging without panic on context swtich

Project description

Alog

https://travis-ci.com/keitheis/alog.svg?branch=master https://codecov.io/gh/keitheis/alog/branch/master/graph/badge.svg http://img.shields.io/pypi/v/alog.svg?style=flat

Your goto Python logging without panic on context swtich.

Warning: No more logger = logging.getLogger(__name__) in your every file.

>>> import alog
>>> alog.info("Hi.")
2016-12-18 20:44:30 INFO  <stdin> Hi.
>>> def test():
...     alog.info("Test 1")
...     alog.error("Test 2")
...
>>> test()
2016-12-18 20:45:19 INFO  <stdin:2> Test 1
2016-12-18 20:45:19 ERROR <stdin:3> Test 2
>>> alog.set_level("ERROR")
>>> test()
2016-12-18 20:45:41 ERROR <stdin:3> Test 2

If you’re new to logging, see Why should you use logging instead of print.

Installation

pip install alog

Features

  • Instant logging with expected defaults.

    You can do logging instantly by reading a small piece of README. Alog comes with useful defaults:

    • A default logger.

    • Logging level: logging.INFO

    • Logging format:

      "%(asctime)s %(levelname)-5.5s [parent_module.current_module:%(lineno)s]%(message)s",
      "%Y-%m-%d %H:%M:%S"
  • No more __name__ whenever you start to do logging in a module.

    Alog builds the default module names on the fly.

  • Compatible with default Python logging module.

    Alog is built upon default Python logging module. You can configure it by the same way of default Python logging module when it’s needed.

Comparing alog with Python default logging module

Comparing alog :

In [1]: import alog

In [2]: alog.info("Hello alog!")
2016-11-23 12:20:34 INFO  <IPython> Hello alog!

with logging module:

In [1]: import logging

In [2]: logging.basicConfig(
   ...:     level=logging.INFO,
   ...:     format="%(asctime)s %(levelname)-5.5s "
   ...:            "[%(name)s:%(lineno)s] %(message)s")

In [3]: # In every file you want to do log, otherwise %(names)s won't work.
In [4]: logger = logging.getLogger(__name__)

In [5]: logger.info("Hello log!")
2016-11-23 12:16:30 INFO  [__main__:1] Hello log!

Tips

import alog

a_complex_json_dict = {...}  # or a_complex_dict
alog.info(alog.pformat(a_complex_dict))

restaurant = Restaurant(...)
alog.info(alog.pdir(restaurant))
# or just skip attributes starts with "__":
alog.info(alog.pdir(restaurant, str_not_startswith="__"))
# instead of
alog.info([attr for attr in dir(restaurant) if attr.startswith("_")])

# Play threads?
alog.turn_logging_thread_name(on=True)
# Processes?
alog.turn_logging_process_id(on=True)
# No datetime wanted?
alog.turn_logging_datetime(on=False)

Why should you use logging instead of print

The main goal of logging is to figure out what was going on and to get the insight. print, by default, does simply pure string output. No timestamp, no module hint, and no level control, comparing to a pretty logging record.

Lets start with aproject/models/user.py :

class User:
    def __init__(self, user_id, username):
        ...
        print(username)
        ...

What you got output of print :

>>> admin = User(1, "admin")
"admin"

Now use alog :

import alog

class User:
    def __init__(self, user_id, username):
        ...
        alog.info(username)
        ...

What you got output of alog.info :

>>> admin = User(1, "admin")
2016-11-23 11:32:58 INFO  [models.user:6] admin

In the output of hundreds of lines, it helps (a lot).

What if you have used print a log? That’s as easy:

import alog

print = alog.info

... # A lot of print code no needed to change

1.2.0 (2021-08-09)

  • Support Python 3.9

  • Remove Python 3.4 and 3.5 support.

1.1.0 (2020-02-10)

  • Support Python 3.8

  • Fix broken set_format function when formatter argument is given.

1.0.0 (2019-04-03)

  • Renamed:

    • turn_logging_datetime(on=True)

    • turn_logging_thread_name(on=False)

    • turn_logging_process_id(on=False)

  • Support most same APIs between alog and Alogger.

  • Add alog.pdir() for handy replacing [attr for attr in dir(obj) if not attr.startswith("_")].

0.9.13 (2017-06-18)

  • Fix not able to turn_log_datetime(on=False).

0.9.12 (2017-06-16)

  • Support not showing_log_datetime by turn_log_datetime(on=False).

0.9.11 (2017-04-07)

  • Add alog.getLogger() for handy replacing logging.getLogger.

0.9.10 (2017-03-27)

  • Default logging format asctime to “%Y-%m-%d %H:%M:%S” instead of “%Y-%m-%d,%H:%M:%S.%f”.

  • Update package info and usage (setup.py, README, …).

0.9.9 (2016-08-28)

  • Update to turn_thread_name and turn_process_id.

0.9.8 (2016-08-27)

  • Support showing_thread_name and showing_process_id.

  • Support global reset.

0.9.7 (2016-08-17)

  • Better paths log for None default root name.

0.9.6 (2016-08-16)

  • First public release.

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

alog-1.2.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

alog-1.2.0-py2.py3-none-any.whl (6.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file alog-1.2.0.tar.gz.

File metadata

  • Download URL: alog-1.2.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.5

File hashes

Hashes for alog-1.2.0.tar.gz
Algorithm Hash digest
SHA256 c389a511f0a61137f01eb06a7646be338313a49a11384cf86d059bc0d76e6a91
MD5 ada1f84931af92ef9fd0b99cdbded32e
BLAKE2b-256 13fb4fbdc1f58db31e98a52c4e05c6aca423f4b41086cd40db086a04d149b82d

See more details on using hashes here.

File details

Details for the file alog-1.2.0-py2.py3-none-any.whl.

File metadata

  • Download URL: alog-1.2.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.5

File hashes

Hashes for alog-1.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8c1ad957531f7b78e6f881a078116c3966300a96d03d55b4d0e90cc8a2b0fe8d
MD5 76937b4a0e43899f8776e372298f68e4
BLAKE2b-256 45dd9778ad06c608fd73a1d6e2238b5a28e5119824876a404465b1414bf7327c

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