def get_process(proc_name):
"""Get process given string in
process cmd line.
"""
LOG = log.getLogger(__name__)
proc = None
try:
for pr in psutil.process_iter():
for args in pr.cmdline():
if proc_name in args.split(" "):
proc = pr
return proc
except BaseException:
# pass
LOG.error("Error fetching {%s} process..." % proc_name)
return None
python类getLogger()的实例源码
def stop_spark_submit_process():
"""Stop spark submit program."""
LOG = log.getLogger(__name__)
try:
# get the driver proc
pr = get_process(SPARK_SUBMIT_PROC_NAME)
if pr:
# terminate (SIGTERM) spark driver proc
for cpr in pr.children(recursive=False):
LOG.info("Terminate child pid {%s} ..." % str(cpr.pid))
cpr.terminate()
# terminate spark submit proc
LOG.info("Terminate pid {%s} ..." % str(pr.pid))
pr.terminate()
except Exception as e:
LOG.error("Error killing spark submit "
"process: got exception: {%s}" % str(e))
def error_trap(app_name):
"""Decorator trapping any error during application boot time.
:param app_name: Application name
:type app_name: str
:return: _wrapper function
"""
@six.wraps(error_trap)
def _wrapper(func):
@six.wraps(_wrapper)
def _inner_wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception:
logger = log.getLogger(__name__)
logger.exception(
'Failed to load application: \'{}\''.format(app_name))
raise
return _inner_wrapper
return _wrapper
def setUp(self):
super(ShellCommandTest, self).setUp()
def get_auth_endpoint(bound_self, args):
return ('test', {})
self.useFixture(fixtures.MonkeyPatch(
'karborclient.shell.KarborShell._get_endpoint_and_kwargs',
get_auth_endpoint))
self.client = mock.MagicMock()
# To prevent log descriptors from being closed during
# shell tests set a custom StreamHandler
self.logger = log.getLogger(None).logger
self.logger.level = logging.DEBUG
self.color_handler = handlers.ColorHandler(sys.stdout)
self.logger.addHandler(self.color_handler)
def main():
config.parse_args(sys.argv)
logging.setup(CONF, "masakari")
log = logging.getLogger(__name__)
objects.register_all()
launcher = service.process_launcher()
started = 0
try:
server = service.WSGIService("masakari_api", use_ssl=CONF.use_ssl)
launcher.launch_service(server, workers=server.workers or 1)
started += 1
except exception.PasteAppNotFound as ex:
log.warning("%s. ``enabled_apis`` includes bad values. "
"Fix to remove this warning.", six.text_type(ex))
if started == 0:
log.error('No APIs were started. '
'Check the enabled_apis config option.')
sys.exit(1)
launcher.wait()
def __init__(self, virtapi, read_only=False):
super(IronicDriver, self).__init__(virtapi)
global ironic
if ironic is None:
ironic = importutils.import_module('ironicclient')
# NOTE(deva): work around a lack of symbols in the current version.
if not hasattr(ironic, 'exc'):
ironic.exc = importutils.import_module('ironicclient.exc')
if not hasattr(ironic, 'client'):
ironic.client = importutils.import_module(
'ironicclient.client')
self.firewall_driver = firewall.load_driver(
default='nova.virt.firewall.NoopFirewallDriver')
self.node_cache = {}
self.node_cache_time = 0
ironicclient_log_level = CONF.ironic.client_log_level
if ironicclient_log_level:
level = py_logging.getLevelName(ironicclient_log_level)
logger = py_logging.getLogger('ironicclient')
logger.setLevel(level)
self.ironicclient = client_wrapper.IronicClientWrapper()
test_fixtures.py 文件源码
项目:Trusted-Platform-Module-nova
作者: BU-NU-CLOUD-SP16
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def test_default_logging(self):
stdlog = self.useFixture(fixtures.StandardLogging())
root = logging.getLogger()
# there should be a null handler as well at DEBUG
self.assertEqual(2, len(root.handlers), root.handlers)
log = logging.getLogger(__name__)
log.info("at info")
log.debug("at debug")
self.assertIn("at info", stdlog.logger.output)
self.assertNotIn("at debug", stdlog.logger.output)
# broken debug messages should still explode, even though we
# aren't logging them in the regular handler
self.assertRaises(TypeError, log.debug, "this is broken %s %s", "foo")
# and, ensure that one of the terrible log messages isn't
# output at info
warn_log = logging.getLogger('migrate.versioning.api')
warn_log.info("warn_log at info, should be skipped")
warn_log.error("warn_log at error")
self.assertIn("warn_log at error", stdlog.logger.output)
self.assertNotIn("warn_log at info", stdlog.logger.output)
def shutdown_all_threads_and_die():
"""Shut down all threads and exit process.
Hit it with a hammer to kill all threads and die.
"""
LOG = log.getLogger(__name__)
LOG.info('Monasca Transform service stopping...')
os._exit(1)
def __init__(self, display=None):
setup_log()
self.log = logging.getLogger(__name__)
self.node = None
self.opts = {}
# NOTE(pas-ha) this method is required for Ansible>=2.4
# TODO(pas-ha) rewrite to support defining callback plugin options
# in ansible.cfg after we require Ansible >=2.4
def setUpClass(cls):
cls.LOG = logging.getLogger(cls._get_full_case_name())
super(BaseTestCase, cls).setUpClass()
def _load_config():
# Don't load in global context, since we can't assume
# these modules are accessible when distutils uses
# this module
from six.moves import configparser
from oslo_config import cfg
from oslo_log import log as logging
global loaded, MONITORS_VENDOR, MONITORS_PRODUCT, MONITORS_PACKAGE
if loaded:
return
loaded = True
cfgfile = cfg.CONF.find_file("release")
if cfgfile is None:
return
try:
cfg = configparser.RawConfigParser()
cfg.read(cfgfile)
if cfg.has_option("Masakarimonitors", "vendor"):
MONITORS_VENDOR = cfg.get("Masakarimonitors", "vendor")
if cfg.has_option("Masakarimonitors", "product"):
MONITORS_PRODUCT = cfg.get("Masakarimonitors", "product")
if cfg.has_option("Masakarimonitors", "package"):
MONITORS_PACKAGE = cfg.get("Masakarimonitors", "package")
except Exception as ex:
LOG = logging.getLogger(__name__)
LOG.error("Failed to load %(cfgfile)s: %(ex)s",
{'cfgfile': cfgfile, 'ex': ex})
def _handle_mp_log_events(self, p, mp_log_q):
while True:
try:
record = mp_log_q.get(timeout=1)
if record is None:
break
logger = logging.getLogger(record.name).logger
logger.handle(record)
except queue.Empty:
if not p.is_alive():
break
def _setup_task_process(mp_log_q):
# Setting up logging and cfg, needed since this is a new process
cfg.CONF(sys.argv[1:], project='coriolis', version="1.0.0")
utils.setup_logging()
# Log events need to be handled in the parent process
log_root = logging.getLogger(None).logger
for handler in log_root.handlers:
log_root.removeHandler(handler)
log_root.addHandler(handlers.QueueHandler(mp_log_q))
def __init__(self):
self.logger = logging.getLogger(__name__)
# Authentication
def __init__(self):
self.logger = logging.getLogger(__name__)
def __init__(self):
if not (os.path.exists(const.CONFIG_PATH)):
set_default_for_default_log_levels()
logging.setup(CONF, 'armada')
self.logger = logging.getLogger(__name__)
def __init__(self, name, conf, threads=1000):
os.umask(0o27) # ensure files are created with the correct privileges
self._logger = logging.getLogger("eventlet.wsgi.server")
self._wsgi_logger = WritableLogger(self._logger)
self.name = name
self.threads = threads
self.children = set()
self.stale_children = set()
self.running = True
self.pgid = os.getpid()
self.conf = conf
try:
os.setpgid(self.pgid, self.pgid)
except OSError:
self.pgid = 0
def load_paste_app(app_name=None):
"""Builds and returns a WSGI app from a paste config file.
We assume the last config file specified in the supplied ConfigOpts
object is the paste config file.
:param app_name: name of the application to load
:raises RuntimeError when config file cannot be located or application
cannot be loaded from config file
"""
if app_name is None:
app_name = cfg.CONF.prog
conf_file = _get_deployment_config_file()
if conf_file is None:
raise RuntimeError(_("Unable to locate config file [%s]") %
cfg.CONF.paste_deploy['api_paste_config'])
try:
app = wsgi.paste_deploy_app(conf_file, app_name, cfg.CONF)
# Log the options used when starting if we're in debug mode...
if cfg.CONF.debug:
cfg.CONF.log_opt_values(logging.getLogger(app_name),
sys_logging.DEBUG)
return app
except (LookupError, ImportError) as e:
raise RuntimeError(_("Unable to load %(app_name)s from "
"configuration file %(conf_file)s."
"\nGot: %(e)r") % {'app_name': app_name,
'conf_file': conf_file,
'e': e})
def setup_logging(name, level):
logging.setup(CONF, name)
LOG = logging.getLogger(None)
if level == 'INFO':
LOG.logger.setLevel(logging.INFO)
if level == 'DEBUG':
LOG.logger.setLevel(logging.DEBUG)
if level == 'WARNING':
LOG.logger.setLevel(logging.WARNING)
if level == 'ERROR':
LOG.logger.setLevel(logging.ERROR)
def _setup_logging(self, debug):
# Output the logs to command-line interface
color_handler = handlers.ColorHandler(sys.stdout)
logger_root = logging.getLogger(None).logger
logger_root.level = logging.DEBUG if debug else logging.WARNING
logger_root.addHandler(color_handler)
# Set the logger level of special library
logging.getLogger('iso8601') \
.logger.setLevel(logging.WARNING)
logging.getLogger('urllib3.connectionpool') \
.logger.setLevel(logging.WARNING)
def get_logger(name):
return logging.getLogger(name)
def main(manager='rock.extension_manager.ExtensionManager'):
utils.register_all_options()
utils.prepare_log(service_name='rock-mon')
log = logging.getLogger(__name__)
log.info('Start rock monitor.')
mgr_class = importutils.import_class(manager)
file_path = os.path.abspath(__file__)
file_dir = os.path.dirname(file_path)
ext_mgr = mgr_class(file_dir + '/extensions')
ext_mgr.start_collect_data()
def main(manager='rock.rules.rule_manager.RuleManager'):
utils.register_all_options()
utils.prepare_log(service_name='rock-engine')
log = logging.getLogger(__name__)
log.info('Start rock engine')
mgr_class = importutils.import_class(manager)
mgr = mgr_class('/etc/rock/cases')
from rock.tasks.check_and_run import check_and_run
check_and_run()
mgr.after_start()
def set_json_logger():
json_logger = syslog.getLogger(__name__ + '_json_log')
json_logger.setLevel(syslog.DEBUG)
if not json_logger.handlers:
log_dir = cfg.CONF.log_dir if cfg.CONF.log_dir \
else '/var/log/neutron'
log_file = log_dir + '/json.output'
fh = syslog.handlers.WatchedFileHandler(log_file)
formatter = syslog.Formatter(
'%(asctime)s.%(msecs)d %(process)d %(message)s',
'%Y-%m-%d %H:%M:%S')
fh.setFormatter(formatter)
json_logger.addHandler(fh)
return json_logger
def get_logger(name):
return logging.getLogger(name)
def _load_config():
# Don't load in global context, since we can't assume
# these modules are accessible when distutils uses
# this module
from six.moves import configparser
from oslo_config import cfg
from oslo_log import log as logging
global loaded, MASAKARI_VENDOR, MASAKARI_PRODUCT, MASAKARI_PACKAGE
if loaded:
return
loaded = True
cfgfile = cfg.CONF.find_file("release")
if cfgfile is None:
return
try:
cfg = configparser.RawConfigParser()
cfg.read(cfgfile)
if cfg.has_option("Masakari", "vendor"):
MASAKARI_VENDOR = cfg.get("Masakari", "vendor")
if cfg.has_option("Masakari", "product"):
MASAKARI_PRODUCT = cfg.get("Masakari", "product")
if cfg.has_option("Masakari", "package"):
MASAKARI_PACKAGE = cfg.get("Masakari", "package")
except Exception as ex:
LOG = logging.getLogger(__name__)
LOG.error("Failed to load %(cfgfile)s: %(ex)s",
{'cfgfile': cfgfile, 'ex': ex})
def __init__(self, logger_name):
self._logger_name = logger_name
self._snatch_handler = SnatchHandler()
self._logger = oslo_logging.getLogger(self._logger_name)
self._previous_level = self._logger.logger.getEffectiveLevel()
def dump_log(self, name):
log = logging.getLogger(name)
if not self.log_file or not os.path.exists(self.log_file):
return
with open(self.log_file, 'r') as fptr:
for line in fptr:
log.info(line.strip())
def __init__(self, threads=1000, initialize_glance_store=False):
os.umask(0o27) # ensure files are created with the correct privileges
self._logger = logging.getLogger("eventlet.wsgi.server")
self.threads = threads
self.children = set()
self.stale_children = set()
self.running = True
self.initialize_glance_store = initialize_glance_store
self.pgid = os.getpid()
try:
os.setpgid(self.pgid, self.pgid)
except OSError:
self.pgid = 0
def load_paste_app(app_name, flavor=None, conf_file=None):
"""Builds and returns a WSGI app from a paste config file.
We assume the last config file specified in the supplied ConfigOpts
object is the paste config file, if conf_file is None.
:param app_name: name of the application to load
:param flavor: name of the variant of the application to load
:param conf_file: path to the paste config file
:raises: RuntimeError when config file cannot be located or application
cannot be loaded from config file
"""
# append the deployment flavor to the application name,
# in order to identify the appropriate paste pipeline
app_name += _get_deployment_flavor(flavor)
if not conf_file:
conf_file = _get_deployment_config_file()
logger = logging.getLogger(__name__)
try:
logger.debug("Loading %(app_name)s from %(conf_file)s",
{'conf_file': conf_file, 'app_name': app_name})
app = deploy.loadapp("config:%s" % conf_file, name=app_name)
# Log the options used when starting if we're in debug mode...
if CONF.debug:
CONF.log_opt_values(logger, logging.DEBUG)
return app
except (LookupError, ImportError) as e:
msg = (_("Unable to load %(app_name)s from "
"configuration file %(conf_file)s."
"\nGot: %(e)r") % {'app_name': app_name,
'conf_file': conf_file,
'e': e})
logger.error(msg)
raise RuntimeError(msg)