Introduced LogFactory to centralize log management
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/fa48ec1c Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/fa48ec1c Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/fa48ec1c Branch: refs/heads/master Commit: fa48ec1c699eb45db620f7e41cd7fe016e927223 Parents: 850fd05 Author: Chamila de Alwis <[email protected]> Authored: Wed Oct 1 19:51:24 2014 +0530 Committer: Chamila de Alwis <[email protected]> Committed: Thu Oct 9 15:40:41 2014 +0530 ---------------------------------------------------------------------- .../cartridge-agent/logging.ini | 34 ++++++++++++++++++ .../cartridge-agent/modules/databridge/agent.py | 5 ++- .../modules/datapublisher/logpublisher.py | 7 ++-- .../modules/healthstatspublisher/healthstats.py | 10 ++---- .../cartridge-agent/modules/util/log.py | 38 ++++++++++++++++++++ 5 files changed, 79 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/fa48ec1c/tools/python-cartridge-agent/cartridge-agent/logging.ini ---------------------------------------------------------------------- diff --git a/tools/python-cartridge-agent/cartridge-agent/logging.ini b/tools/python-cartridge-agent/cartridge-agent/logging.ini new file mode 100644 index 0000000..cf6bc7d --- /dev/null +++ b/tools/python-cartridge-agent/cartridge-agent/logging.ini @@ -0,0 +1,34 @@ +[formatters] +keys=default + +[formatter_default] +format=%(asctime)s:%(levelname)s:%(message)s +class=logging.Formatter + +[handlers] +keys=console, error_file + +[handler_console] +class=logging.StreamHandler +formatter=default +args=tuple() + +[handler_log_file] +class=logging.FileHandler +level=ERROR +formatter=default +args=("agent.log", "w") + +[handler_error_file] +class=logging.FileHandler +level=ERROR +formatter=default +args=("error.log", "w") + +[loggers] +keys=root + +[logger_root] +level=DEBUG +formatter=default +handlers=console,error_file \ No newline at end of file http://git-wip-us.apache.org/repos/asf/stratos/blob/fa48ec1c/tools/python-cartridge-agent/cartridge-agent/modules/databridge/agent.py ---------------------------------------------------------------------- diff --git a/tools/python-cartridge-agent/cartridge-agent/modules/databridge/agent.py b/tools/python-cartridge-agent/cartridge-agent/modules/databridge/agent.py index 7a8a0dc..b65a2be 100644 --- a/tools/python-cartridge-agent/cartridge-agent/modules/databridge/agent.py +++ b/tools/python-cartridge-agent/cartridge-agent/modules/databridge/agent.py @@ -1,5 +1,5 @@ from thrift.publisher import * -import logging +from ..util.log import * class StreamDefinition: @@ -88,8 +88,7 @@ class ThriftPublisher: """ Handles publishing events to BAM/CEP through thrift using the provided address and credentials """ - logging.basicConfig(level=logging.DEBUG) - log = logging.getLogger(__name__) + log = LogFactory().get_log(__name__) def __init__(self, ip, port, username, password, stream_definition): """ http://git-wip-us.apache.org/repos/asf/stratos/blob/fa48ec1c/tools/python-cartridge-agent/cartridge-agent/modules/datapublisher/logpublisher.py ---------------------------------------------------------------------- diff --git a/tools/python-cartridge-agent/cartridge-agent/modules/datapublisher/logpublisher.py b/tools/python-cartridge-agent/cartridge-agent/modules/datapublisher/logpublisher.py index d2769d9..d5cdc01 100644 --- a/tools/python-cartridge-agent/cartridge-agent/modules/datapublisher/logpublisher.py +++ b/tools/python-cartridge-agent/cartridge-agent/modules/datapublisher/logpublisher.py @@ -1,6 +1,5 @@ import os import datetime -import logging from threading import Thread, current_thread from ..databridge.agent import * @@ -14,8 +13,7 @@ class LogPublisher(Thread): def __init__(self, file_path, stream_definition, tenant_id, alias, date_time, member_id): Thread.__init__(self) - logging.basicConfig(level=logging.DEBUG) - self.log = logging.getLogger(__name__) + self.log = LogFactory().get_log(__name__) self.file_path = file_path self.thrift_publisher = ThriftPublisher( @@ -192,8 +190,7 @@ class DataPublisherConfiguration: """ __instance = None - logging.basicConfig(level=logging.DEBUG) - log = logging.getLogger(__name__) + log = LogFactory().get_log(__name__) @staticmethod def get_instance(): http://git-wip-us.apache.org/repos/asf/stratos/blob/fa48ec1c/tools/python-cartridge-agent/cartridge-agent/modules/healthstatspublisher/healthstats.py ---------------------------------------------------------------------- diff --git a/tools/python-cartridge-agent/cartridge-agent/modules/healthstatspublisher/healthstats.py b/tools/python-cartridge-agent/cartridge-agent/modules/healthstatspublisher/healthstats.py index 953c0fc..ea85d44 100644 --- a/tools/python-cartridge-agent/cartridge-agent/modules/healthstatspublisher/healthstats.py +++ b/tools/python-cartridge-agent/cartridge-agent/modules/healthstatspublisher/healthstats.py @@ -1,6 +1,5 @@ from threading import Thread import time -import logging import psutil import os @@ -23,8 +22,7 @@ class HealthStatisticsPublisherManager(Thread): """ Thread.__init__(self) - logging.basicConfig(level=logging.DEBUG) - self.log = logging.getLogger(__name__) + self.log = LogFactory().get_log(__name__) self.publish_interval = publish_interval """:type : int""" @@ -53,8 +51,7 @@ class HealthStatisticsPublisher: Publishes memory usage and load average to thrift server """ def __init__(self): - logging.basicConfig(level=logging.DEBUG) - self.log = logging.getLogger(__name__) + self.log = LogFactory().get_log(__name__) self.ports = [] self.ports.append(CEPPublisherConfiguration.get_instance().server_port) cartridgeagentutils.wait_until_ports_active(CEPPublisherConfiguration.get_instance().server_ip, self.ports) @@ -154,8 +151,7 @@ class CEPPublisherConfiguration: """ __instance = None - logging.basicConfig(level=logging.DEBUG) - log = logging.getLogger(__name__) + log = LogFactory().get_log(__name__) @staticmethod def get_instance(): http://git-wip-us.apache.org/repos/asf/stratos/blob/fa48ec1c/tools/python-cartridge-agent/cartridge-agent/modules/util/log.py ---------------------------------------------------------------------- diff --git a/tools/python-cartridge-agent/cartridge-agent/modules/util/log.py b/tools/python-cartridge-agent/cartridge-agent/modules/util/log.py new file mode 100644 index 0000000..83b1f50 --- /dev/null +++ b/tools/python-cartridge-agent/cartridge-agent/modules/util/log.py @@ -0,0 +1,38 @@ +import logging +import logging.config +import os + + +class LogFactory(object): + """ + Singleton implementation for handling logging in CartridgeAgent + """ + class __LogFactory: + def __init__(self): + self.logs = {} + logging_conf = os.path.join(os.path.dirname(__file__), "logging.ini") + logging.config.fileConfig(logging_conf) + + def get_log(self, name): + if name not in self.logs: + self.logs[name] = logging.getLogger(name) + + return self.logs[name] + + instance = None + + def __new__(cls, *args, **kwargs): + if not LogFactory.instance: + LogFactory.instance = LogFactory.__LogFactory() + + return LogFactory.instance + + def get_log(self, name): + """ + Returns a logger class with the specified channel name. Creates a new logger if one doesn't exists for the + specified channel + :param str name: Channel name + :return: The logger class + :rtype: RootLogger + """ + self.instance.get_log(name) \ No newline at end of file
