Skip to content

Commit

Permalink
Allow to specify an instance name per logger
Browse files Browse the repository at this point in the history
The instance field can be set to extend the logger name with the string
'.__INSTANCE__', e.g. to specify the special purpose of a class in the
logger hierarchy for better controlled logging configuration.

* loggerbyclass.py: add instance parameter to get_logger_by_class
* setup.py: version bump to 0.2
  • Loading branch information
Johannes Wienke committed Feb 16, 2015
1 parent a6275ca commit 741e8c8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions loggerbyclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
import logging


def get_logger_by_class(klass):
def get_logger_by_class(klass, instance=None):
"""Gets a python logger instance based on a class instance. The logger name
will be a dotted string containing python module and class name, hence
being the full path to the class.
@param klass: class instance
@return: logger instance
"""
return logging.getLogger(klass.__module__ + "." + klass.__name__)
return logging.getLogger(klass.__module__ + "." + klass.__name__ +
(".__{}__".format(instance.replace('.', '_'))
if instance else ""))
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from setuptools import setup, find_packages

setup(name='loggerbyclass',
version='0.1.0',
version='0.2.0',
description='''
Provides a single helper function to simplify the common idiom
of getting one logger object from python standard logging per
Expand Down

0 comments on commit 741e8c8

Please sign in to comment.