diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2006-09-24 23:59:22 +0000 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2006-09-24 23:59:22 +0000 |
commit | e812785c2d7985382458ca8dd7b2409b58c38eb2 (patch) | |
tree | 02ccb8af8dee7d987738d35eeefbb5106dc67927 /lib/sqlalchemy/logging.py | |
parent | 7daa9e1d6a0bddf46cbd47115b5f5a4ef56ce478 (diff) | |
download | sqlalchemy-e812785c2d7985382458ca8dd7b2409b58c38eb2.tar.gz |
- logging is now implemented via standard python "logging" module.
"echo" keyword parameters are still functional but set/unset
log levels for their respective classes/instances. all logging
can be controlled directly through the Python API by setting
INFO and DEBUG levels for loggers in the "sqlalchemy" namespace.
class-level logging is under "sqlalchemy.<module>.<classname>",
instance-level logging under "sqlalchemy.<module>.<classname>.<hexid>".
Test suite includes "--log-info" and "--log-debug" arguments
which work independently of --verbose/--quiet. Logging added
to orm to allow tracking of mapper configurations, row iteration
fixes [ticket:229] [ticket:79]
Diffstat (limited to 'lib/sqlalchemy/logging.py')
-rw-r--r-- | lib/sqlalchemy/logging.py | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/lib/sqlalchemy/logging.py b/lib/sqlalchemy/logging.py new file mode 100644 index 000000000..bba1e0764 --- /dev/null +++ b/lib/sqlalchemy/logging.py @@ -0,0 +1,69 @@ +# logging.py - adapt python logging module to SQLAlchemy +# Copyright (C) 2006 Michael Bayer mike_mp@zzzcomputing.com +# +# This module is part of SQLAlchemy and is released under +# the MIT License: http://www.opensource.org/licenses/mit-license.php + +"""provides a few functions used by instances to turn on/off their logging, including support +for the usual "echo" parameter. Control of logging for SA can be performed from the regular +python logging module. The regular dotted module namespace is used, starting at 'sqlalchemy'. +For class-level logging, the class name is appended, and for instance-level logging, the hex +id of the instance is appended. + +The "echo" keyword parameter which is available on some SA objects corresponds to an instance-level +logger for that instance. + +E.g.: + + engine.echo = True + +is equivalent to: + + import logging + logging.getLogger('sqlalchemy.engine.ComposedSQLEngine.%s' % hex(id(engine))).setLevel(logging.DEBUG) + +""" + +import sys + +# py2.5 absolute imports will fix.... +logging = __import__('logging') + +default_enabled = False +def default_logging(): + global default_enabled + if not default_enabled: + default_enabled = True + rootlogger = logging.getLogger('sqlalchemy') + rootlogger.setLevel(logging.NOTSET) + handler = logging.StreamHandler(sys.stdout) + handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(name)s %(message)s')) + rootlogger.addHandler(handler) + +def _get_instance_name(instance): + return instance.__class__.__module__ + "." + instance.__class__.__name__ + "." + hex(id(instance)) + +def instance_logger(instance): + return logging.getLogger(_get_instance_name(instance)) + +def class_logger(cls): + return logging.getLogger(cls.__module__ + "." + cls.__name__) + +def is_debug_enabled(logger): + return logger.isEnabledFor(logging.DEBUG) +def is_info_enabled(logger): + return logger.isEnabledFor(logging.INFO) + +class echo_property(object): + level_map={logging.DEBUG : "debug", logging.NOTSET : False} + def __get__(self, instance, owner): + level = logging.getLogger(_get_instance_name(instance)).getEffectiveLevel() + return echo_property.level_map.get(level, True) + def __set__(self, instance, value): + if value: + default_logging() + logging.getLogger(_get_instance_name(instance)).setLevel(value == 'debug' and logging.DEBUG or logging.INFO) + else: + logging.getLogger(_get_instance_name(instance)).setLevel(logging.NOTSET) + + |