logging.py :  » Web-Frameworks » Spyce » spyce-2.1 » sqlalchemy » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Spyce 
Spyce » spyce 2.1 » sqlalchemy » logging.py
# 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.Engine.%s' % hex(id(engine))).setLevel(logging.DEBUG)
    
"""

import sys

# py2.5 absolute imports will fix....
logging = __import__('logging')

# turn off logging at the root sqlalchemy level
logging.getLogger('sqlalchemy').setLevel(logging.ERROR)

default_enabled = False
def default_logging(name):
    global default_enabled
    if logging.getLogger(name).getEffectiveLevel() < logging.WARN:
        default_enabled=True
    if not default_enabled:
        default_enabled = True
        rootlogger = logging.getLogger('sqlalchemy')
        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):                    
    # since getLogger() does not have any way of removing logger objects from memory,
    # instance logging displays the instance id as a modulus of 16 to prevent endless memory growth
    # also speeds performance as logger initialization is apparently slow
    return instance.__class__.__module__ + "." + instance.__class__.__name__ + ".0x.." + hex(id(instance))[-2:]
    
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.INFO:True}
    def __get__(self, instance, owner):
        level = logging.getLogger(_get_instance_name(instance)).getEffectiveLevel()
        return echo_property.level_map.get(level, False)
    def __set__(self, instance, value):
        if value:
            default_logging(_get_instance_name(instance))
            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)
    
    
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.