diff --git a/atomicapp/applogging.py b/atomicapp/applogging.py index ad5030a6..95f94af4 100644 --- a/atomicapp/applogging.py +++ b/atomicapp/applogging.py @@ -71,8 +71,15 @@ def _make_unicode(self, input): return input -class Logging: +class AtomicappLoggingAdapter(logging.LoggerAdapter): + """ + A class to pass contextual information to logs. + """ + def process(self, msg, kwargs): + return('{} : {}'.format(self.extra['atomicapp_extra'], msg), kwargs) + +class Logging: @staticmethod def setup_logging(verbose=None, quiet=None, logtype=None): """ @@ -101,20 +108,20 @@ def setup_logging(verbose=None, quiet=None, logtype=None): # Get the loggers and clear out the handlers (allows this function # to be ran more than once) - logger = logging.getLogger(LOGGER_DEFAULT) - logger.handlers = [] + logger_raw = logging.getLogger(LOGGER_DEFAULT) + logger_raw.handlers = [] cockpit_logger = logging.getLogger(LOGGER_COCKPIT) cockpit_logger.handlers = [] if logtype == 'none': # blank out both loggers - logger.addHandler(logging.NullHandler()) + logger_raw.addHandler(logging.NullHandler()) cockpit_logger.addHandler(logging.NullHandler()) return if logtype == 'cockpit': # blank out normal log messages - logger.addHandler(logging.NullHandler()) + logger_raw.addHandler(logging.NullHandler()) # configure cockpit logger handler = logging.StreamHandler(stream=sys.stdout) @@ -130,10 +137,10 @@ def setup_logging(verbose=None, quiet=None, logtype=None): # configure logger for basic no color printing to stdout handler = logging.StreamHandler(stream=sys.stdout) - formatter = logging.Formatter('%(asctime)s - [%(levelname)s] - %(filename)s - %(message)s') + formatter = logging.Formatter('%(asctime)s - [%(levelname)s] - %(message)s') handler.setFormatter(formatter) - logger.addHandler(handler) - logger.setLevel(logging_level) + logger_raw.addHandler(handler) + logger_raw.setLevel(logging_level) return if logtype == 'color': @@ -142,11 +149,26 @@ def setup_logging(verbose=None, quiet=None, logtype=None): # configure logger for color printing to stdout handler = logging.StreamHandler(stream=sys.stdout) - formatter = colorizeOutputFormatter('%(asctime)s - [%(levelname)s] - %(filename)s - %(message)s') + formatter = colorizeOutputFormatter('%(asctime)s - [%(levelname)s] - %(message)s') handler.setFormatter(formatter) - logger.addHandler(handler) - logger.setLevel(logging_level) + logger_raw.addHandler(handler) + logger_raw.setLevel(logging_level) return # If we made it here then there is an error raise Exception("Invalid logging output type: {}".format(logtype)) + + @staticmethod + def global_logger(filename): + """ + This function returns a logging instance which will output logging event information + along with what the LoggerAdapter tells it to output + :param filename: path of the file calling this function + :return the function returns the logger instance which is being used by all the files + """ + + # creating a logging instance + logger_raw = logging.getLogger(LOGGER_DEFAULT) + # the logging adapter handles the filename received from the file importing this + logger = AtomicappLoggingAdapter(logger_raw, {'atomicapp_extra': '/'.join(filename.split('/')[-2:])}) + return logger diff --git a/atomicapp/cli/main.py b/atomicapp/cli/main.py index 2cf50ed6..2bacf6ce 100644 --- a/atomicapp/cli/main.py +++ b/atomicapp/cli/main.py @@ -21,7 +21,6 @@ import sys import argparse -import logging from lockfile import LockFile from lockfile import AlreadyLocked @@ -34,13 +33,13 @@ CACHE_DIR, HOST_DIR, LOCK_FILE, - LOGGER_DEFAULT, PROVIDERS) from atomicapp.nulecule import NuleculeManager from atomicapp.nulecule.exceptions import NuleculeException from atomicapp.utils import Utils -logger = logging.getLogger(LOGGER_DEFAULT) +# assign the logger instance from applogging.py to use here +logger = Logging.global_logger(__file__) def print_app_location(app_path): diff --git a/atomicapp/nulecule/base.py b/atomicapp/nulecule/base.py index 17c19a51..7ee343b6 100644 --- a/atomicapp/nulecule/base.py +++ b/atomicapp/nulecule/base.py @@ -11,7 +11,6 @@ EXTERNAL_APP_DIR, GLOBAL_CONF, LOGGER_COCKPIT, - LOGGER_DEFAULT, MAIN_FILE, RESOURCE_KEY, PARAMS_KEY, @@ -26,11 +25,13 @@ from atomicapp.nulecule.container import DockerHandler from atomicapp.nulecule.exceptions import NuleculeException from atomicapp.providers.openshift import OpenShiftProvider - +from atomicapp.applogging import Logging from jsonpointer import resolve_pointer, set_pointer, JsonPointerException cockpit_logger = logging.getLogger(LOGGER_COCKPIT) -logger = logging.getLogger(LOGGER_DEFAULT) + +# assign the logger instance from applogging.py to use here +logger = Logging.global_logger(__file__) class Nulecule(NuleculeBase): diff --git a/atomicapp/nulecule/container.py b/atomicapp/nulecule/container.py index cc40b426..3d2d7824 100644 --- a/atomicapp/nulecule/container.py +++ b/atomicapp/nulecule/container.py @@ -3,15 +3,17 @@ import uuid import logging +from atomicapp.applogging import Logging from atomicapp.constants import (APP_ENT_PATH, LOGGER_COCKPIT, - LOGGER_DEFAULT, MAIN_FILE) from atomicapp.utils import Utils from atomicapp.nulecule.exceptions import NuleculeException cockpit_logger = logging.getLogger(LOGGER_COCKPIT) -logger = logging.getLogger(LOGGER_DEFAULT) + +# assign the logger instance from applogging.py to use here +logger = Logging.global_logger(__file__) class DockerHandler(object): diff --git a/atomicapp/nulecule/main.py b/atomicapp/nulecule/main.py index 1ce88b8d..46b36d49 100644 --- a/atomicapp/nulecule/main.py +++ b/atomicapp/nulecule/main.py @@ -8,6 +8,7 @@ import urlparse import urllib +from atomicapp.applogging import Logging from atomicapp.constants import (GLOBAL_CONF, ANSWERS_FILE_SAMPLE_FORMAT, ANSWERS_FILE, @@ -15,7 +16,6 @@ ANSWERS_RUNTIME_FILE, DEFAULT_ANSWERS, LOGGER_COCKPIT, - LOGGER_DEFAULT, MAIN_FILE, PROVIDER_KEY) from atomicapp.nulecule.base import Nulecule @@ -23,7 +23,8 @@ from atomicapp.utils import Utils cockpit_logger = logging.getLogger(LOGGER_COCKPIT) -logger = logging.getLogger(LOGGER_DEFAULT) +# assign the logger instance from applogging.py to use here +logger = Logging.global_logger(__file__) class NuleculeManager(object): diff --git a/atomicapp/plugin.py b/atomicapp/plugin.py index 96a43f11..79eca12c 100644 --- a/atomicapp/plugin.py +++ b/atomicapp/plugin.py @@ -24,18 +24,17 @@ import imp -import logging +from atomicapp.applogging import Logging from utils import Utils from constants import (HOST_DIR, - LOGGER_DEFAULT, PROVIDER_CONFIG_KEY) -logger = logging.getLogger(LOGGER_DEFAULT) +# assign the logger instance from applogging.py to use here +logger = Logging.global_logger(__file__) class Provider(object): key = None - config = None path = None dryrun = None diff --git a/atomicapp/providers/docker.py b/atomicapp/providers/docker.py index a157ccc7..b211b716 100644 --- a/atomicapp/providers/docker.py +++ b/atomicapp/providers/docker.py @@ -20,14 +20,15 @@ import os import subprocess import re -import logging + +from atomicapp.applogging import Logging from atomicapp.constants import (DEFAULT_CONTAINER_NAME, - DEFAULT_NAMESPACE, - LOGGER_DEFAULT) + DEFAULT_NAMESPACE,) from atomicapp.plugin import Provider, ProviderFailedException from atomicapp.utils import Utils -logger = logging.getLogger(LOGGER_DEFAULT) +# assign the logger instance from applogging.py to use here +logger = Logging.global_logger(__file__) class DockerProvider(Provider): diff --git a/atomicapp/providers/kubernetes.py b/atomicapp/providers/kubernetes.py index ad6c8be8..0b9693fb 100644 --- a/atomicapp/providers/kubernetes.py +++ b/atomicapp/providers/kubernetes.py @@ -22,13 +22,14 @@ import os from string import Template -from atomicapp.constants import (LOGGER_COCKPIT, - LOGGER_DEFAULT) +from atomicapp.constants import (LOGGER_COCKPIT) from atomicapp.plugin import Provider, ProviderFailedException from atomicapp.utils import Utils +from atomicapp.applogging import Logging cockpit_logger = logging.getLogger(LOGGER_COCKPIT) -logger = logging.getLogger(LOGGER_DEFAULT) +# assign the logger instance from applogging.py to use here +logger = Logging.global_logger(__file__) class KubernetesProvider(Provider): diff --git a/atomicapp/providers/marathon.py b/atomicapp/providers/marathon.py index c22165d8..85c947fb 100644 --- a/atomicapp/providers/marathon.py +++ b/atomicapp/providers/marathon.py @@ -21,14 +21,15 @@ import urlparse import logging import os -from atomicapp.constants import (LOGGER_COCKPIT, - LOGGER_DEFAULT) +from atomicapp.constants import (LOGGER_COCKPIT) from atomicapp.plugin import Provider, ProviderFailedException from atomicapp.utils import Utils from atomicapp.constants import PROVIDER_API_KEY +from atomicapp.applogging import Logging cockpit_logger = logging.getLogger(LOGGER_COCKPIT) -logger = logging.getLogger(LOGGER_DEFAULT) +# assign the logger instance from applogging.py to use here +logger = Logging.global_logger(__file__) class Marathon(Provider): diff --git a/atomicapp/providers/openshift.py b/atomicapp/providers/openshift.py index 0a73b811..42304f55 100644 --- a/atomicapp/providers/openshift.py +++ b/atomicapp/providers/openshift.py @@ -29,18 +29,18 @@ from atomicapp.utils import Utils from atomicapp.plugin import Provider, ProviderFailedException +from atomicapp.applogging import Logging from atomicapp.constants import (ACCESS_TOKEN_KEY, ANSWERS_FILE, DEFAULT_NAMESPACE, - LOGGER_DEFAULT, NAMESPACE_KEY, PROVIDER_API_KEY, PROVIDER_TLS_VERIFY_KEY, PROVIDER_CA_KEY) from requests.exceptions import SSLError -import logging -logger = logging.getLogger(LOGGER_DEFAULT) +# assign the logger instance from applogging.py to use here +logger = Logging.global_logger(__file__) # If running in an openshift POD via `oc new-app`, the ca file is here OPENSHIFT_POD_CA_FILE = "/run/secrets/kubernetes.io/serviceaccount/ca.crt" diff --git a/atomicapp/utils.py b/atomicapp/utils.py index bb7a77c3..c7d4d255 100644 --- a/atomicapp/utils.py +++ b/atomicapp/utils.py @@ -31,19 +31,21 @@ import logging +from atomicapp.applogging import Logging + from subprocess import Popen, PIPE from constants import (APP_ENT_PATH, CACHE_DIR, EXTERNAL_APP_DIR, HOST_DIR, LOGGER_COCKPIT, - LOGGER_DEFAULT, WORKDIR) __all__ = ('Utils') cockpit_logger = logging.getLogger(LOGGER_COCKPIT) -logger = logging.getLogger(LOGGER_DEFAULT) +# assign the logger instance from applogging.py to use here +logger = Logging.global_logger(__file__) class AtomicAppUtilsException(Exception):