logging fixed and synergy shell updated

- logging: every time a new manager is loaded, it is now added to the
  Synergy logger
- logging is configured in main()
- shell: commands list sorted
- commented code: deleted

Change-Id: I1c5261db78950e03417eb4e90e2049a2ad927cb0
This commit is contained in:
Lisa Zangrando 2016-06-07 16:33:29 +02:00 committed by Vincent Llorens
parent af3d6953f7
commit 1de615b32e
3 changed files with 54 additions and 71 deletions

View File

@ -113,25 +113,20 @@ def main():
for entry in iter_entry_points(COMMANDS_ENTRY_POINT):
command_class = entry.load()
command = command_class()
# command = command_class(*args, **kwargs)
command.configureParser(subparser)
commands[entry.name] = command
args = parser.parse_args(sys.argv[1:])
for command_name in sorted(commands.keys()):
commands[command_name].configureParser(subparser)
# print("args %s" % args)
args = parser.parse_args(sys.argv[1:])
os_username = args.os_username
os_password = args.os_password
os_project_name = args.os_project_name
# os_project_id = args.os_project_id
os_auth_token = args.os_auth_token
os_auth_token_cache = args.os_auth_token_cache
os_auth_url = args.os_auth_url
# os_auth_system = args.os_auth_system
# insecure = args.insecure
bypass_url = args.bypass_url
# cacert = args.os_cacert
command_name = args.command_name
if not os_username:
@ -150,21 +145,14 @@ def main():
username=os_username,
password=os_password,
project_name=os_project_name)
"""
client.authenticate()
token = client.getToken()
print("os_auth_token=%s" % os_auth_token)
print("os_auth_token_cache=%s" % os_auth_token_cache)
"""
token = None
if os_auth_token:
token = os_auth_token
elif os_auth_token_cache:
token = keystone_v3.Token.load(".auth_token")
# print("token is expired? %s" % token.isExpired())
if token is None or token.isExpired():
client.authenticate()
token = client.getToken()

View File

@ -1,7 +1,3 @@
import logging
import logging.handlers
import os
try:
from oslo_config import cfg
except ImportError:
@ -79,42 +75,9 @@ cfg.CONF.register_opts(wsgi_opts, group="WSGI")
cfg.CONF.register_opts(logger_opts, group="Logger")
def parse_args(args=None, usage=None, default_config_files=None):
def parseArgs(args=None, usage=None, default_config_files=None):
cfg.CONF(args=args,
project='synergy',
version="1.0",
usage=usage,
default_config_files=default_config_files)
# create a logging format
formatter = logging.Formatter(CONF.Logger.formatter)
log_dir = os.path.dirname(CONF.Logger.filename)
if not os.path.exists(log_dir):
os.makedirs(log_dir)
# Add the log message handler to the logger
handler = logging.handlers.RotatingFileHandler(
CONF.Logger.filename,
maxBytes=CONF.Logger.maxBytes,
backupCount=CONF.Logger.backupCount)
handler.setFormatter(formatter)
# set root logger
root_logger = logging.getLogger("synergy")
if cfg.CONF.Logger.level == "DEBUG":
root_logger.setLevel(logging.DEBUG)
elif cfg.CONF.Logger.level == "INFO":
root_logger.setLevel(logging.INFO)
elif cfg.CONF.Logger.level == "WARNING":
root_logger.setLevel(logging.WARNING)
elif cfg.CONF.Logger.level == "ERROR":
root_logger.setLevel(logging.ERROR)
elif cfg.CONF.Logger.level == "CRITICAL":
root_logger.setLevel(logging.CRITICAL)
else:
root_logger.setLevel(logging.INFO)
root_logger.addHandler(handler)

View File

@ -1,6 +1,8 @@
import eventlet
import json
import logging
import logging.handlers
import os
import sys
from cgi import escape
@ -39,6 +41,39 @@ LOG = None
MANAGER_ENTRY_POINT = "synergy.managers" # used to discover Synergy managers
def setLogger(name):
"""Configure the given logger with Synergy logging configuration.
Note:
This function should only be used when entering Synergy by the main()
function. Otherwise you may run into issues due to logging to protected
files.
"""
# create a logging format
formatter = logging.Formatter(CONF.Logger.formatter)
log_dir = os.path.dirname(CONF.Logger.filename)
if not os.path.exists(log_dir):
os.makedirs(log_dir)
# Add the log message handler to the logger
handler = logging.handlers.RotatingFileHandler(
CONF.Logger.filename,
maxBytes=CONF.Logger.maxBytes,
backupCount=CONF.Logger.backupCount)
handler.setFormatter(formatter)
# set logger level
logger = logging.getLogger(name)
try:
logger.setLevel(cfg.CONF.Logger.level)
except ValueError: # wrong level, we default to INFO
logger.setLevel(logging.INFO)
logger.addHandler(handler)
class ManagerRPC(object):
def __init__(self, managers):
@ -144,21 +179,8 @@ class Synergy(service.Service):
LOG.info("loading manager %r", entry.name)
try:
"""
found = False
try:
CONF.get(entry.name)
found = True
except Exception as ex:
LOG.info("missing section [%s] in synergy.conf for manager"
" %r: using the default values"
% (entry.name, entry.name))
"""
CONF.register_opts(config.manager_opts, group=entry.name)
# manager_conf = CONF.get(entry.name)
manager_class = entry.load()
manager_obj = manager_class(*args, **kwargs)
@ -184,12 +206,15 @@ class Synergy(service.Service):
manager.managers = self.managers
try:
LOG.info("initializing the %r manager" % (manager.getName()))
manager.setup()
manager.setStatus("ACTIVE")
LOG.info("manager '%s' initialized!" % (manager.getName()))
LOG.info("manager %r initialized!" % (manager.getName()))
except Exception as ex:
LOG.error("manager '%s' instantiation error: %s" % (name, ex))
LOG.error("Exception has occured", exc_info=1)
LOG.error("manager %r instantiation error: %s" % (name, ex))
self.managers[manager.getName()].setStatus("ERROR")
raise ex
@ -451,14 +476,16 @@ def main():
eventlet.monkey_patch(os=False)
# the configuration will be into the cfg.CONF global data structure
config.parse_args(args=sys.argv[1:],
default_config_files=["/etc/synergy/synergy.conf"])
config.parseArgs(args=sys.argv[1:],
default_config_files=["/etc/synergy/synergy.conf"])
if not cfg.CONF.config_file:
sys.exit("ERROR: Unable to find configuration file via the "
"default search paths (~/.synergy/, ~/, /etc/synergy/"
", /etc/) and the '--config-file' option!")
setLogger(name="synergy")
global LOG
# LOG = logging.getLogger(None)
@ -469,6 +496,11 @@ def main():
# os.setsid()
server = Synergy()
# Configure logging for managers
for manager in server.managers.values():
setLogger(manager.__module__)
server.start()
LOG.info("Synergy started")