Skip to content
This repository has been archived by the owner on Jan 19, 2018. It is now read-only.

Commit

Permalink
Implemented LoggerAdapter to provide extended paths in the output. Im…
Browse files Browse the repository at this point in the history
…proves #571
  • Loading branch information
concaf committed Feb 25, 2016
1 parent b8f890c commit 7b52749
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 40 deletions.
44 changes: 33 additions & 11 deletions atomicapp/applogging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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)
Expand All @@ -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':
Expand All @@ -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
5 changes: 2 additions & 3 deletions atomicapp/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import sys

import argparse
import logging
from lockfile import LockFile
from lockfile import AlreadyLocked

Expand All @@ -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):
Expand Down
7 changes: 4 additions & 3 deletions atomicapp/nulecule/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
EXTERNAL_APP_DIR,
GLOBAL_CONF,
LOGGER_COCKPIT,
LOGGER_DEFAULT,
MAIN_FILE,
RESOURCE_KEY,
PARAMS_KEY,
Expand All @@ -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):
Expand Down
6 changes: 4 additions & 2 deletions atomicapp/nulecule/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions atomicapp/nulecule/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@
import urlparse
import urllib

from atomicapp.applogging import Logging
from atomicapp.constants import (GLOBAL_CONF,
ANSWERS_FILE_SAMPLE_FORMAT,
ANSWERS_FILE,
ANSWERS_FILE_SAMPLE,
ANSWERS_RUNTIME_FILE,
DEFAULT_ANSWERS,
LOGGER_COCKPIT,
LOGGER_DEFAULT,
MAIN_FILE,
PROVIDER_KEY)
from atomicapp.nulecule.base import Nulecule
from atomicapp.nulecule.exceptions import NuleculeException
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):
Expand Down
7 changes: 3 additions & 4 deletions atomicapp/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions atomicapp/providers/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 4 additions & 3 deletions atomicapp/providers/kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 4 additions & 3 deletions atomicapp/providers/marathon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions atomicapp/providers/openshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
6 changes: 4 additions & 2 deletions atomicapp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 7b52749

Please sign in to comment.