Singleton stacklog
This commit is contained in:
parent
93fd8ad8b7
commit
f601dff52f
@ -1,4 +1,4 @@
|
||||
from stacktach import logging as stacklog
|
||||
from stacktach import stacklog
|
||||
from stacktach import models
|
||||
|
||||
|
||||
|
@ -1,43 +0,0 @@
|
||||
# Copyright (c) 2013 - Rackspace Inc.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to
|
||||
# deal in the Software without restriction, including without limitation the
|
||||
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
# sell copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
# IN THE SOFTWARE.
|
||||
|
||||
LOG = None
|
||||
|
||||
|
||||
def set_logger(logger):
|
||||
global LOG
|
||||
LOG = logger
|
||||
|
||||
|
||||
def get_logger():
|
||||
global LOG
|
||||
return LOG
|
||||
|
||||
|
||||
def warn(msg):
|
||||
global LOG
|
||||
if LOG is not None:
|
||||
LOG.warn(msg)
|
||||
|
||||
|
||||
def error(msg):
|
||||
global LOG
|
||||
if LOG is not None:
|
||||
LOG.error(msg)
|
69
stacktach/stacklog.py
Normal file
69
stacktach/stacklog.py
Normal file
@ -0,0 +1,69 @@
|
||||
# Copyright (c) 2013 - Rackspace Inc.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to
|
||||
# deal in the Software without restriction, including without limitation the
|
||||
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
# sell copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
# IN THE SOFTWARE.
|
||||
|
||||
import logging
|
||||
|
||||
LOGGERS = {}
|
||||
default_logger_name = 'stacktach-default'
|
||||
|
||||
|
||||
def set_default_logger_name(name):
|
||||
global default_logger_name
|
||||
default_logger_name = name
|
||||
|
||||
|
||||
def _make_logger(name):
|
||||
log = logging.getLogger(__name__)
|
||||
log.setLevel(logging.DEBUG)
|
||||
handler = logging.handlers.TimedRotatingFileHandler('%s.log' % name,
|
||||
when='h', interval=6, backupCount=4)
|
||||
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
handler.setFormatter(formatter)
|
||||
log.addHandler(handler)
|
||||
log.handlers[0].doRollover()
|
||||
return log
|
||||
|
||||
|
||||
def init_logger(name=None):
|
||||
global LOGGERS
|
||||
if name is None:
|
||||
name = default_logger_name
|
||||
if name not in LOGGERS:
|
||||
LOGGERS[name] = _make_logger(name)
|
||||
|
||||
|
||||
def get_logger(name=None):
|
||||
global LOGGERS
|
||||
if name is None:
|
||||
name = default_logger_name
|
||||
init_logger(name=name)
|
||||
return LOGGERS[name]
|
||||
|
||||
|
||||
def warn(msg, name=None):
|
||||
if name is None:
|
||||
name = default_logger_name
|
||||
get_logger(name=name).warn(msg)
|
||||
|
||||
|
||||
def error(msg, name=None):
|
||||
if name is None:
|
||||
name = default_logger_name
|
||||
get_logger(name=name).warn(msg)
|
@ -11,7 +11,7 @@ from stacktach import datetime_to_decimal as dt
|
||||
from stacktach import db as stackdb
|
||||
from stacktach import image_type
|
||||
from stacktach import models
|
||||
from stacktach import logging as stacklog
|
||||
from stacktach import stacklog
|
||||
from stacktach import utils
|
||||
|
||||
|
||||
|
@ -32,7 +32,7 @@ from utils import TENANT_ID_1
|
||||
from utils import INSTANCE_TYPE_ID_1
|
||||
from utils import DUMMY_TIME
|
||||
from utils import INSTANCE_TYPE_ID_2
|
||||
from stacktach import logging as stacklog
|
||||
from stacktach import stacklog
|
||||
from stacktach import views
|
||||
|
||||
|
||||
@ -409,14 +409,19 @@ class StacktachUsageParsingTestCase(unittest.TestCase):
|
||||
self.mox = mox.Mox()
|
||||
views.STACKDB = self.mox.CreateMockAnything()
|
||||
self.log = self.mox.CreateMockAnything()
|
||||
stacklog.set_logger(self.log)
|
||||
self.mox.StubOutWithMock(stacklog, 'get_logger')
|
||||
|
||||
def tearDown(self):
|
||||
self.mox.UnsetStubs()
|
||||
stacklog.set_logger(None)
|
||||
|
||||
def setup_mock_log(self, name=None):
|
||||
if name is None:
|
||||
stacklog.get_logger(name=mox.IgnoreArg()).AndReturn(self.log)
|
||||
else:
|
||||
stacklog.get_logger(name=name).AndReturn(self.log)
|
||||
|
||||
def test_process_usage_for_new_launch_create_start(self):
|
||||
kwargs = {'launched': str(DUMMY_TIME), 'tenant_id': TENANT_ID_1 }
|
||||
kwargs = {'launched': str(DUMMY_TIME), 'tenant_id': TENANT_ID_1}
|
||||
notification = utils.create_nova_notif(request_id=REQUEST_ID_1, **kwargs)
|
||||
event = 'compute.instance.create.start'
|
||||
raw, usage = self._setup_process_usage_mocks(event, notification)
|
||||
@ -684,7 +689,8 @@ class StacktachUsageParsingTestCase(unittest.TestCase):
|
||||
raw = utils.create_raw(self.mox, current_decimal, event=event,
|
||||
json_str=json_str)
|
||||
raw.id = 1
|
||||
self.log.warn('Exists without launched_at. RawData(1)')
|
||||
self.setup_mock_log()
|
||||
self.log.warn('Ignoring exists without launched_at. RawData(1)')
|
||||
self.mox.ReplayAll()
|
||||
views._process_exists(raw, notif[1])
|
||||
self.mox.VerifyAll()
|
||||
|
@ -1,4 +1,4 @@
|
||||
# Copyright (c) 2012 - Rackspace Inc.
|
||||
# Copyright (c) 2013 - Rackspace Inc.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to
|
||||
@ -24,15 +24,15 @@ import unittest
|
||||
import mox
|
||||
|
||||
from stacktach import db
|
||||
from stacktach import logging as stacklog
|
||||
from stacktach import stacklog
|
||||
from stacktach import models
|
||||
|
||||
|
||||
class DBAPITestCase(unittest.TestCase):
|
||||
class StacktachDBTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.mox = mox.Mox()
|
||||
self.log = self.mox.CreateMockAnything()
|
||||
stacklog.set_logger(self.log)
|
||||
self.mox.StubOutWithMock(stacklog, 'get_logger')
|
||||
self.mox.StubOutWithMock(models, 'RawData', use_mock_anything=True)
|
||||
models.RawData.objects = self.mox.CreateMockAnything()
|
||||
self.mox.StubOutWithMock(models, 'Deployment', use_mock_anything=True)
|
||||
@ -58,7 +58,12 @@ class DBAPITestCase(unittest.TestCase):
|
||||
|
||||
def tearDown(self):
|
||||
self.mox.UnsetStubs()
|
||||
stacklog.set_logger(None)
|
||||
|
||||
def setup_mock_log(self, name=None):
|
||||
if name is None:
|
||||
stacklog.get_logger(name=mox.IgnoreArg()).AndReturn(self.log)
|
||||
else:
|
||||
stacklog.get_logger(name=name).AndReturn(self.log)
|
||||
|
||||
def test_safe_get(self):
|
||||
Model = self.mox.CreateMockAnything()
|
||||
@ -82,6 +87,8 @@ class DBAPITestCase(unittest.TestCase):
|
||||
results = self.mox.CreateMockAnything()
|
||||
Model.objects.filter(**filters).AndReturn(results)
|
||||
results.count().AndReturn(0)
|
||||
log = self.mox.CreateMockAnything()
|
||||
self.setup_mock_log()
|
||||
self.log.warn('No records found for Model get.')
|
||||
self.mox.ReplayAll()
|
||||
returned = db._safe_get(Model, **filters)
|
||||
@ -96,6 +103,7 @@ class DBAPITestCase(unittest.TestCase):
|
||||
results = self.mox.CreateMockAnything()
|
||||
Model.objects.filter(**filters).AndReturn(results)
|
||||
results.count().AndReturn(2)
|
||||
self.setup_mock_log()
|
||||
self.log.warn('Multiple records found for Model get.')
|
||||
object = self.mox.CreateMockAnything()
|
||||
results[0].AndReturn(object)
|
||||
|
@ -39,7 +39,7 @@ class NovaConsumerTestCase(unittest.TestCase):
|
||||
|
||||
def test_get_consumers(self):
|
||||
created_queues = []
|
||||
created_callbacks = []
|
||||
created_callbacks = []
|
||||
created_consumers = []
|
||||
def Consumer(queues=None, callbacks=None):
|
||||
created_queues.extend(queues)
|
||||
|
@ -21,7 +21,6 @@
|
||||
import argparse
|
||||
import datetime
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
from time import sleep
|
||||
@ -38,17 +37,10 @@ POSSIBLE_TOPDIR = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
|
||||
if os.path.exists(os.path.join(POSSIBLE_TOPDIR, 'stacktach')):
|
||||
sys.path.insert(0, POSSIBLE_TOPDIR)
|
||||
|
||||
from stacktach import logging as stacklog
|
||||
from stacktach import stacklog
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
LOG.setLevel(logging.DEBUG)
|
||||
handler = logging.handlers.TimedRotatingFileHandler('verifier.log',
|
||||
when='h', interval=6, backupCount=4)
|
||||
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
handler.setFormatter(formatter)
|
||||
LOG.addHandler(handler)
|
||||
LOG.handlers[0].doRollover()
|
||||
stacklog.set_logger(LOG)
|
||||
stacklog.set_default_logger_name('verifier')
|
||||
LOG = stacklog.get_logger()
|
||||
|
||||
from stacktach import models
|
||||
from stacktach import datetime_to_decimal as dt
|
||||
@ -57,6 +49,7 @@ from verifier import FieldMismatch
|
||||
from verifier import NotFound
|
||||
from verifier import VerificationException
|
||||
|
||||
|
||||
def _list_exists(ending_max=None, status=None):
|
||||
params = {}
|
||||
if ending_max:
|
||||
|
@ -20,7 +20,6 @@ import datetime
|
||||
import kombu
|
||||
import kombu.entity
|
||||
import kombu.mixins
|
||||
import logging
|
||||
import sys
|
||||
import time
|
||||
|
||||
@ -34,21 +33,13 @@ except ImportError:
|
||||
|
||||
from pympler.process import ProcessMemoryInfo
|
||||
|
||||
from stacktach import logging as stacklog
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
LOG.setLevel(logging.DEBUG)
|
||||
handler = logging.handlers.TimedRotatingFileHandler('worker.log',
|
||||
when='h', interval=6, backupCount=4)
|
||||
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
handler.setFormatter(formatter)
|
||||
LOG.addHandler(handler)
|
||||
LOG.handlers[0].doRollover()
|
||||
stacklog.set_logger(LOG)
|
||||
|
||||
from stacktach import db
|
||||
from stacktach import stacklog
|
||||
from stacktach import views
|
||||
|
||||
stacklog.set_default_logger_name('worker')
|
||||
LOG = stacklog.get_logger()
|
||||
|
||||
|
||||
class NovaConsumer(kombu.mixins.ConsumerMixin):
|
||||
def __init__(self, name, connection, deployment, durable, queue_arguments):
|
||||
@ -141,11 +132,13 @@ class NovaConsumer(kombu.mixins.ConsumerMixin):
|
||||
def continue_running():
|
||||
return True
|
||||
|
||||
|
||||
def exit_or_sleep(exit=False):
|
||||
if exit:
|
||||
sys.exit(1)
|
||||
time.sleep(5)
|
||||
|
||||
|
||||
def run(deployment_config):
|
||||
name = deployment_config['name']
|
||||
host = deployment_config.get('rabbit_host', 'localhost')
|
||||
|
Loading…
Reference in New Issue
Block a user