#####################################################################
#
# SimpleLog     A simple logging module
#
# This software is governed by a license. See
# LICENSE.txt for the terms of this license.
#
#####################################################################

import time
import sys
import zLOG

# The original log levels as used in the LDAPUserFolder are...
#
#        Level: 1 - Catastrophes
#               2 - Major Events
#               3 - Minor events
#               4 - Login failures
#               5 - Login Successes
#               7 - Login success from cache
#               9 - Debugging

class ZLOGLogger:
    """ Small PEP 282-style shim class to adapt logging to zLOG """

    translator = { 1 : zLOG.ERROR
                 , 2 : zLOG.ERROR
                 , 3 : zLOG.INFO
                 , 4 : zLOG.DEBUG
                 , 5 : zLOG.DEBUG
                 , 7 : zLOG.TRACE
                 , 9 : zLOG.TRACE
                 }

    def __init__(self, name="LDAPUserFolder"):
        self.name = name

    def log(self, level, msg, *args, **kw):
        real_level = self.translator.get(level, None)
        if real_level is None:
            real_level = level
        exc_info = kw.get("exc_info")

        if exc_info == 1:
            exc_info = sys.exc_info()

        if exc_info == (None, None, None):
            exc_info = None

        zLOG.LOG(self.name, real_level, msg, error=exc_info)

    def debug(self, msg, *args, **kw):
        self.log(zLOG.DEBUG, msg, *args, **kw)

