- self.logger.addHandler(handler)
-
-
-info_logger = _SfaLogger(loggername='info', level=logging.INFO)
-debug_logger = _SfaLogger(loggername='debug', level=logging.DEBUG)
-warn_logger = _SfaLogger(loggername='warning', level=logging.WARNING)
-error_logger = _SfaLogger(loggername='error', level=logging.ERROR)
-critical_logger = _SfaLogger(loggername='critical', level=logging.CRITICAL)
-logger = info_logger
-sfi_logger = _SfaLogger(logfile=os.path.expanduser("~/.sfi/") + 'sfi.log',
- loggername='sfilog', level=logging.DEBUG)
-########################################
-import time
-
-
-def profile(logger):
- """
- Prints the runtime of the specified callable. Use as a decorator, e.g.,
-
- @profile(logger)
- def foo(...):
- ...
- """
- def logger_profile(callable):
- def wrapper(*args, **kwds):
- start = time.time()
- result = callable(*args, **kwds)
- end = time.time()
- args = map(str, args)
- args += ["%s = %s" % (name, str(value))
- for (name, value) in kwds.iteritems()]
- # should probably use debug, but then debug is not always enabled
- logger.info("PROFILED %s (%s): %.02f s" %
- (callable.__name__, ", ".join(args), end - start))
- return result
- return wrapper
- return logger_profile
-
-
-if __name__ == '__main__':
- print('testing sfalogging into logger.log')
- logger1 = _SfaLogger('logger.log', loggername='std(info)')
- logger2 = _SfaLogger('logger.log', loggername='error', level=logging.ERROR)
- logger3 = _SfaLogger('logger.log', loggername='debug', level=logging.DEBUG)
-
- for logger, msg in ((logger1, "std(info)"), (logger2, "error"), (logger3, "debug")):
-
- print("====================", msg, logger.logger.handlers)
-
- logger.enable_console()
- logger.critical("logger.critical")
- logger.error("logger.error")
- logger.warn("logger.warning")
- logger.info("logger.info")
- logger.debug("logger.debug")
- logger.setLevel(logging.DEBUG)
- logger.debug("logger.debug again")
-
- @profile(logger)
- def sleep(seconds=1):
- time.sleep(seconds)
-
- logger.info('console.info')
- sleep(0.5)
- logger.setLevel(logging.DEBUG)
- sleep(0.25)
+ self.addHandler(handler)
+
+
+# install our class as the default
+logging.setLoggerClass(SfaLogger)
+
+
+# configure
+# this is *NOT* passed to dictConfig as-is
+# instead we filter 'handlers' and 'loggers'
+# to contain just one entry
+# so make sure that 'handlers' and 'loggers'
+# have the same set of keys
+def logging_config(context):
+ if context == 'server':
+ handlername = 'file'
+ filename = '/var/log/sfa.log'
+ level = 'INFO'
+ elif context == 'import':
+ handlername = 'file'
+ filename = '/var/log/sfa-import.log'
+ level = 'INFO'
+ elif context == 'cli':
+ handlername = 'file'
+ filename = os.path.expanduser("~/.sfi.log")
+ level = 'DEBUG'
+ elif context == 'console':
+ handlername = 'stdout'
+ filename = 'ignored'
+ level = 'INFO'
+ else:
+ print("Cannot configure logging - exiting")
+ exit(1)
+
+ config = {
+ 'version': 1,
+ # IMPORTANT: we may be imported by something else, so:
+ 'disable_existing_loggers': False,
+ 'formatters': {
+ 'standard': {
+ 'datefmt': '%m-%d %H:%M:%S',
+ 'format': ('%(asctime)s %(levelname)s '
+ '%(filename)s:%(lineno)d %(message)s'),
+ },
+ },
+ # fill in later with just the one needed
+ # otherwise a dummy 'ignored' file gets created
+ 'handlers': {
+ },
+ 'loggers': {
+ 'sfa': {
+ 'handlers': [handlername],
+ 'level': level,
+ 'propagate': False,
+ },
+ },
+ }
+ if handlername == 'stdout':
+ config['handlers']['stdout'] = {
+ 'level': level,
+ 'formatter': 'standard',
+ 'class': 'logging.StreamHandler',
+ }
+ else:
+ config['handlers']['file'] = {
+ 'filename': filename,
+ 'level': level,
+ 'formatter': 'standard',
+ 'class': 'logging.handlers.TimedRotatingFileHandler',
+ # every monday and during 3 months
+ 'when': 'w0',
+ 'interval': 1,
+ 'backupCount': 12,
+ }
+ return config
+
+
+logger = logging.getLogger('sfa')
+
+
+def init_logger(context):
+ logging.config.dictConfig(logging_config(context))
+
+
+# if the user process does not do anything
+# like for the miscell testers and other certificate
+# probing/dumping utilities
+init_logger('console')