Welcome to alogging documentation!

Contents:

alogging

https://img.shields.io/pypi/v/alogging.svg https://img.shields.io/travis/alikins/alogging.svg Documentation Status Updates

Python logging tools and utils.

Usage

To use alogging in a project:

import alogging

Examples

Basic use of alogging:

import alogging

# create a logging.Logger object, will use the __name__ of the
# module by default. Equilivent to 'log = logging.getLogger(__name__)'
log = alogging.get_logger()

log.debug('created a Logger object so use it for a debug msg')

if __name__ = '__main__':
    main_log = alogging.app_setup(name='example.main')
    main_log.debug('started main')

More advanced:

import alogging

# local alias for alogging.a()
a = alogging.a

log = alogging.get_logger()

class ThingToDo(object):
    def __init__(self, requirement, priority=None, assigner=None):
        # get a Logger named 'example.ThingToDo'
        self.log = alogging.get_class_logger(self)

        self.log.info('Task as assigned: req=%s, pri=%s, ass=%s', requirement, priority, assigner)

        priority = priority or 'never'

        self.log.info('Task reprioritized: req=%s, pri=%s, ass=%s', requirement, priority, assigner')


# alogging.t decorator will log when the decorated method is called,
# what args it was passed, and what it's return value was

@alogging.t
def space_out_for_while(duration=None):
    # space out for 10 minutes by default
    duration = duration or 600

    # return the total amount of work accomplished
    return 0

def find_coffee(coffee_places):
    log.debug('looking for coffee')
    return None

def do_startup_stuff():
    coffee_places = ['piehole', 'mug_on_desk', 'coffee_machine', 'krankies']
    # log the the args to find_coffee as it is called
    has_coffee = a(find_coffee(coffee_places))

    work_accomplished = space_out_for_while(duration=300)

def do_work():
    next_task = TaskToDo('finish TODO list', assigner='Lumberg')
    if not next_task:
        return

    # oh no, work...
    log.error("I'm slammed at the moment, I can't do %s', next_task)
    raise Exception()

if __name__ = '__main__':
    # use some reasonable defaults for setting up logging.
    # - log to stderr
    # - use a default format:
    #   """%(asctime)s,%(msecs)03d %(levelname)-0.1s %(name)s %(processName)s:%(process)d %(funcName)s:%(lineno)d - %(message)s"""
    main_log = alogging.app_setup(name='example.main')
    main_log.debug('Log to logging "example.main"')

    do_startup_stuff()

    try:
        do_work()
    except Exception as exc:
        # gruntle a bit and continue
        log.exception(exc)

    return 0

License

  • Free software: MIT license

Features

  • TODO

Authors

  • Adrian Likins

Installation

Stable release

To install alogging, run this command in your terminal:

$ pip install alogging

This is the preferred method to install alogging, as it will always install the most recent stable release.

If you don’t have pip installed, this Python installation guide can guide you through the process.

From sources

The sources for alogging can be downloaded from the Github repo.

You can either clone the public repository:

$ git clone git://github.com/alikins/alogging

Or download the tarball:

$ curl  -OL https://github.com/alikins/alogging/tarball/master

Once you have a copy of the source, you can install it with:

$ python setup.py install

Usage

To use alogging in a project:

import alogging

Examples

Basic use of alogging:

import alogging

# local alias for alogging.a()
a = alogging.a

# create a logging.Logger object, will use the __name__ of the
# module by default. Equilivent to 'log = logging.getLogger(__name__)'
log = alogging.get_logger()

log.debug('created a Logger object so use it for a debug msg')


class ThingToDo(object):
    def __init__(self, requirement, priority=None, assigner=None):
        # get a Logger named 'example.ThingToDo'
        self.log = alogging.get_class_logger(self)

        self.log.info('Task as assigned: req=%s, pri=%s, ass=%s', requirement, priority, assigner)

        priority = priority or 'never'

        self.log.info('Task reprioritized: req=%s, pri=%s, ass=%s', requirement, priority, assigner')


# alogging.t decorator will log when the decorated method is called,
# what args it was passed, and what it's return value was

@alogging.t
def space_out_for_while(duration=None):
    # space out for 10 minutes by default
    duration = duration or 600

    # return the total amount of work accomplished
    return 0

def find_coffee(coffee_places):
    log.debug('looking for coffee')
    return None

def do_startup_stuff():
    coffee_places = ['piehole', 'mug_on_desk', 'coffee_machine', 'krankies']
    # log the the args to find_coffee as it is called
    has_coffee = a(find_coffee(coffee_places))

    work_accomplished = space_out_for_while(duration=300)

def do_work():
    next_task = TaskToDo('finish TODO list', assigner='Lumberg')
    if not next_task:
        return

    # oh no, work...
    log.error("I'm slammed at the moment, I can't do %s', next_task)
    raise Exception()

if __name__ = '__main__':
    # use some reasonable defaults for setting up logging.
    # - log to stderr
    # - use a default format:
    #   """%(asctime)s,%(msecs)03d %(levelname)-0.1s %(name)s %(processName)s:%(process)d %(funcName)s:%(lineno)d - %(message)s"""
    main_log = alogging.app_setup(name='example.main')
    main_log.debug('Log to logging "example.main"')

    do_startup_stuff()

    try:
        do_work()
    except Exception as exc:
        # gruntle a bit and continue
        log.exception(exc)

alogging

alogging package

alogging.pf(obj)[source]
alogging.pp(obj)[source]
alogging.echo(value)[source]
alogging.a(*args)[source]

Log the args of ‘a’ and returns the args.

Basically, log info about whatever it wraps, but returns it so it can continue to be callled.

Parameters:args (tuple) – The args to pass through to whatever is wrapped

Returns: (tuple): The args that were passed in.

alogging.t(func)[source]

Decorate a callable (class or method) and log it’s args and return values

The loggers created and used should reflect where the object is defined/used.

ie, ‘mycode.utils.math.Summer.total’ for calling ‘total’ method on an instance of mycode.utils.math.Summer

alogging.app_setup(name=None)[source]

Call this to setup a default logging setup in a script or apps __main__

This will create a root logger with some default handlers, as well as a logger for ‘name’ if provided.

Parameters:name – (str): If provided, create a logging.Logger with this name
alogging.module_setup(name=None, use_root_logger=False)[source]

Call this to setup a default log setup from a library or module.

ie, where the app itself may be setting up handlers, root logger, etc

alogging.setup(name=None, stream_handler=None, file_handler=None, use_root_logger=False)[source]
alogging.setup_root_logger(root_level=None, handlers=None)[source]
alogging.get_class_logger(obj, depth=2)[source]

Use to get a logger with name equiv to module.Class

in a regular class __init__, use like:

self.log = alogging.get_class_logger(self)

In a metaclass __new__, use like:

log = alogging.get_class_logger(cls)
alogging.get_class_logger_name(obj, depth=None)[source]

Use to get a logger name equiv to module.Class

alogging.get_logger(name=None, depth=2)[source]

Use to get a logger with name of callers __name__

Can be used in place of:

import logging log = logging.getLogger(__name__)

That can be replaced with

import alogging log = alogging.get_logger()
Parameters:
  • name (str) – Optional logger name to use to override the default one chosen automatically.
  • depth (int) – Optional depth of stack to influence where get_logger looks to automatically choose a logger name. Default is 2.
Returns:

A logger

Return type:

logging.Logger

alogging.get_logger_name(depth=None)[source]
alogging.get_method_logger(depth=2)[source]
alogging.get_method_logger_name(depth=None)[source]
alogging.get_stack_size()[source]

Get stack size for caller’s frame.

alogging.env_log_level(var_name)[source]

Subpackages

alogging.filters package
Submodules
alogging.filters.default_fields module
class alogging.filters.default_fields.DefaultFieldsFilter(name='', defaults=None)[source]

Bases: logging.Filter

Make sure log records have a default value for the provided field/attribute

ie, if you want to use a default format string with a ‘request_id’ or ‘sql’ attribute, but not all records get those attributes added, then you could add this filter to add them

filter(record)[source]

Determine if the specified record is to be logged.

Is the specified record to be logged? Returns 0 for no, nonzero for yes. If deemed appropriate, the record may be modified in-place.

alogging.filters.django_sql_celery module
class alogging.filters.django_sql_celery.DjangoDbSqlCeleryFilter[source]

Bases: object

Filter to prevent logging celery periodtasks

filter(record)[source]
alogging.filters.django_sql_excludes module
class alogging.filters.django_sql_excludes.DjangoDbSqlExcludeFilter(name='', excludes=None)[source]

Bases: logging.Filter

Filter to prevent logging misc queries

filter(record)[source]

Determine if the specified record is to be logged.

Is the specified record to be logged? Returns 0 for no, nonzero for yes. If deemed appropriate, the record may be modified in-place.

alogging.formatters package
Submodules
alogging.formatters.django_sql module
class alogging.formatters.django_sql.DjangoDbSqlPlainFormatter(fmt=None, datefmt=None, options=None, style='%')[source]

Bases: logging.Formatter

pretty print django.db sql

format(record)[source]

Format the specified record as text.

The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.

alogging.formatters.django_sql_color module
class alogging.formatters.django_sql_color.DjangoDbSqlColorFormatter(fmt=None, datefmt=None, style='%', options=None, pygments_lexer='postgres-console', pygments_formatter='terminal256', pygments_style='default')[source]

Bases: logging.Formatter

Pretty print django.db sql with color by pyments

Parameters:
  • fmt – (str): The logging.Formatter format string
  • datefmt (str) – The logging.Formatter date format string
  • style (str) – The logging.Formatter format string type
  • options (dict) – Dict of options to pass to sqlparse.format()
  • pygments_lexer (str) – The name of the pygments lexer to use. Examples include: ‘postgres-console’, ‘postgres’, ‘rql’, ‘sql’, ‘sqlite3’, ‘mysql’, ‘plpgsql’, ‘tsql’
  • pygments_formatter (str) – The name of the pygments formatter to use. Examples include: ‘terminal256’, ‘terminal’, ‘terminal16m’, ‘text’
  • pygments_style (str) – The name of the pygments formatter style to use.
format(record)[source]

Format the specified record as text.

The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.

alogging.formatters.pprint module
class alogging.formatters.pprint.PPrintRecordFormatter(fmt=None, datefmt=None, options=None, indent=1, style='%')[source]

Bases: logging.Formatter

Pretty print the __dict__ of the log record.

format(record)[source]

Format the specified record as text.

The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.

Submodules

alogging.echo module
alogging.echo.echo(value)[source]
alogging.echo.echo_format(value, depth=1, caller_name='echo_format')[source]
alogging.logger module
alogging.logger.a(*args)[source]

Log the args of ‘a’ and returns the args.

Basically, log info about whatever it wraps, but returns it so it can continue to be callled.

Parameters:args (tuple) – The args to pass through to whatever is wrapped

Returns: (tuple): The args that were passed in.

alogging.logger.app_setup(name=None)[source]

Call this to setup a default logging setup in a script or apps __main__

This will create a root logger with some default handlers, as well as a logger for ‘name’ if provided.

Parameters:name – (str): If provided, create a logging.Logger with this name
alogging.logger.env_log_level(var_name)[source]
alogging.logger.env_var(var_name)[source]

See if ‘Var_Name’, ‘VAR_NAME’, or ‘var_name’ is an enviroment variable

alogging.logger.get_class_logger(obj, depth=2)[source]

Use to get a logger with name equiv to module.Class

in a regular class __init__, use like:

self.log = alogging.get_class_logger(self)

In a metaclass __new__, use like:

log = alogging.get_class_logger(cls)
alogging.logger.get_class_logger_name(obj, depth=None)[source]

Use to get a logger name equiv to module.Class

alogging.logger.get_file_handler(name)[source]
alogging.logger.get_logger(name=None, depth=2)[source]

Use to get a logger with name of callers __name__

Can be used in place of:

import logging log = logging.getLogger(__name__)

That can be replaced with

import alogging log = alogging.get_logger()
Parameters:
  • name (str) – Optional logger name to use to override the default one chosen automatically.
  • depth (int) – Optional depth of stack to influence where get_logger looks to automatically choose a logger name. Default is 2.
Returns:

A logger

Return type:

logging.Logger

alogging.logger.get_logger_name(depth=None)[source]
alogging.logger.get_method_logger(depth=2)[source]
alogging.logger.get_method_logger_name(depth=None)[source]
alogging.logger.get_stack_size()[source]

Get stack size for caller’s frame.

alogging.logger.get_stream_handler(name=None)[source]
alogging.logger.module_setup(name=None, use_root_logger=False)[source]

Call this to setup a default log setup from a library or module.

ie, where the app itself may be setting up handlers, root logger, etc

alogging.logger.setup(name=None, stream_handler=None, file_handler=None, use_root_logger=False)[source]
alogging.logger.setup_root_logger(root_level=None, handlers=None)[source]
alogging.logger.t(func)[source]

Decorate a callable (class or method) and log it’s args and return values

The loggers created and used should reflect where the object is defined/used.

ie, ‘mycode.utils.math.Summer.total’ for calling ‘total’ method on an instance of mycode.utils.math.Summer

alogging.pp module
alogging.pp.pf(obj)[source]
alogging.pp.pp(obj)[source]

Credits

Development Lead

History

0.4.3 (2020-05-28)

  • Docs improvements
  • Setup readthedocs

0.4.2 (2020-04-25)

  • Add pygments options to django_sql_color formatter
  • Minor docs improvements

0.4.1 (2020-04-24)

  • Split ‘default_setup’ to ‘app_setup’ and ‘module_setup’
  • Add docs and examples

0.3.1 (2019-10-13)

  • Add django_sql_color formatter

Indices and tables