Commit e125eb35 authored by Johan Ström's avatar Johan Ström

python3 logging compatability

parent 9cbedc14
import logging
from six import PY2
# We want to keep C++ and Python ago-coding as similar as possible.
# In C++ we have levels TRACE and CRITICAL.
if PY2:
_logginglevelNames = logging._levelNames
else:
_logginglevelNames = logging._levelToName
# Add extra Trace level
TRACE = logging.TRACE = 5
_logginglevelNames[TRACE] = TRACE
_logginglevelNames['TRACE'] = TRACE
# C++ impl has FATAL, in python we have CRITICAL
# Add this alias so we can use the same logging consts
FATAL = logging.FATAL = logging.CRITICAL
_logginglevelNames[FATAL] = FATAL
_logginglevelNames['FATAL'] = FATAL
LOGGING_LOGGER_CLASS = logging.getLoggerClass()
class AgoLoggingClass(LOGGING_LOGGER_CLASS):
def trace(self, msg, *args, **kwargs):
if self.isEnabledFor(TRACE):
......@@ -31,10 +23,11 @@ class AgoLoggingClass(LOGGING_LOGGER_CLASS):
self._log(FATAL, msg, args, **kwargs)
if logging.getLoggerClass() is not AgoLoggingClass:
logging.setLoggerClass(AgoLoggingClass)
logging.addLevelName(TRACE, 'TRACE')
logging.addLevelName(FATAL, 'FATAL')
def init():
if logging.getLoggerClass() is not AgoLoggingClass:
logging.setLoggerClass(AgoLoggingClass)
logging.addLevelName(TRACE, 'TRACE')
logging.addLevelName(FATAL, 'FATAL')
# TODO: Syslog priority mapping
# TODO: Syslog priority mapping
from __future__ import print_function
import agoclient._directories
import agoclient._logging
import argparse
from . import config
import logging
......@@ -13,6 +13,8 @@ from logging.handlers import SysLogHandler
__all__ = ["AgoApp"]
agoclient._logging.init()
class StartupError(Exception):
pass
......@@ -163,11 +165,14 @@ class AgoApp:
lvl_name = self.get_config_option("log_level", "INFO",
section=[None, "system"])
if lvl_name.upper() not in logging._levelNames:
raise ConfigurationError("Invalid log_level %s" % lvl_name)
lvl_name = lvl_name.upper()
lvl = logging.getLevelName(lvl_name)
# ensure it was a defined level name; if it returns this string, it was unknown.
if lvl == "Level %s" % lvl_name:
raise ConfigurationError("Invalid log_level %s" % lvl_name)
# ..and set it
lvl = logging.getLevelName(lvl_name.upper())
root.setLevel(lvl)
# Find log method..
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment