Beispiel #1
0
    def test_level_names_overriding(self):
        formatter = TextFormatter(colorize=False)
        # formatter.override_level_names({'CRITICAL': 'CRIT', 'ERROR': 'ERRO', 'WARNING': 'WARN', 'DEBUG': 'DEBU'})
        formatter.override_level_names({'ERROR': 'ERRO'})
        log = self.get_logger(formatter)

        log.error("test message")
        with open(self.filename) as f:
            content = f.readlines()[-1]
            self.assertIn(" ERRO ", content)
Beispiel #2
0
 def test_color_overriding(self):
     formatter = TextFormatter(colorize=True)
     formatter.override_colors({'info': CL_BLDYLW, 'field': CL_TXTBLU})
     log = self.get_logger(formatter)
     log.withFields({'user': '******'}).info("test message")
     with open(self.filename) as f:
         content = f.readlines()[-1]
         self.assertIn(" {}INFO{} ".format(CL_BLDYLW, CL_TXTRST), content)
         self.assertIn(
             "; {}user{}={}John Doe{}".format(CL_TXTBLU, CL_TXTRST,
                                              CL_TXTRST, CL_TXTRST),
             content)
Beispiel #3
0
 def test_color_output(self):
     formatter = TextFormatter(colorize=True)
     log = self.get_logger(formatter)
     log.info("test message")
     with open(self.filename) as f:
         content = f.readlines()[-1]
         self.assertIn(" {}INFO{} ".format(CL_TXTGRN, CL_TXTRST), content)
Beispiel #4
0
 def test_extra_fields(self):
     formatter = TextFormatter(colorize=False)
     log = self.get_logger(formatter)
     log.withFields({'user': '******'}).info("test message")
     with open(self.filename) as f:
         content = f.readlines()[-1]
         self.assertIn("; user=John Doe", content)
Beispiel #5
0
    def test_levels(self):
        formatter = TextFormatter(colorize=False)
        log = self.get_logger(formatter)

        log.debug("test_debug_output")
        with open(self.filename) as f:
            content = f.readlines()[-1]
            self.assertIn(" DEBUG ", content)
            self.assertTrue(content.endswith("test_debug_output\n"))

        log.info("test_info_output")
        with open(self.filename) as f:
            content = f.readlines()[-1]
            self.assertIn(" INFO ", content)
            self.assertTrue(content.endswith("test_info_output\n"))

        log.warning("test_warning_output")
        with open(self.filename) as f:
            content = f.readlines()[-1]
            self.assertIn(" WARNING ", content)
            self.assertTrue(content.endswith("test_warning_output\n"))

        log.error("test_error_output")
        with open(self.filename) as f:
            content = f.readlines()[-1]
            self.assertIn(" ERROR ", content)
            self.assertTrue(content.endswith("test_error_output\n"))

        log.critical("test_critical_output")
        with open(self.filename) as f:
            content = f.readlines()[-1]
            self.assertIn(" CRITICAL ", content)
            self.assertTrue(content.endswith("test_critical_output\n"))
Beispiel #6
0
 def test_write_to_logfile(self):
     formatter = TextFormatter(colorize=False)
     log = self.get_logger(formatter)
     log.info("test log output")
     with open(self.filename) as f:
         content = f.readlines()[-1]
         # print("\n" + content)
         self.assertTrue(content.endswith("test log output\n"))
Beispiel #7
0
 def test_message_format(self):
     fmt = "%(levelname)-8s %(message)s"
     formatter = TextFormatter(fmt=fmt, colorize=False)
     log = self.get_logger(formatter)
     log.info("test message")
     with open(self.filename) as f:
         content = f.readlines()[-1]
         self.assertEqual("INFO     test message\n", content)
Beispiel #8
0
 def test_date_format_zulu(self):
     formatter = TextFormatter(datefmt='Z', colorize=False)
     log = self.get_logger(formatter)
     log.info("test a date in Zulu format")
     with open(self.filename) as f:
         content = f.readlines()[-1]
         pattern = "^\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z\]"
         if sys.version_info >= (3, 1):  # Python version >= 3.1
             self.assertRegex(content, pattern)
         else:
             self.assertRegexpMatches(content, pattern)
Beispiel #9
0
 def test_date_format(self):
     formatter = TextFormatter(datefmt='%m/%d/%Y %I:%M:%S %p',
                               colorize=False)
     log = self.get_logger(formatter)
     log.info("test a date in custom format")
     with open(self.filename) as f:
         content = f.readlines()[-1]
         pattern = "^\[\d{2}\/\d{2}\/\d{4} \d{2}:\d{2}:\d{2} [AP]M\]"
         if sys.version_info >= (3, 1):  # Python version >= 3.1
             self.assertRegex(content, pattern)
         else:
             self.assertRegexpMatches(content, pattern)
Beispiel #10
0
def get_logger():
    logging.setLoggerClass(PyLogrus)

    logger = logging.getLogger(__name__)  # type: PyLogrus
    logger.setLevel(logging.DEBUG)

    formatter = TextFormatter(datefmt='Z', colorize=True)
    formatter.override_level_names({
        'CRITICAL': 'CRIT',
        'ERROR': 'ERRO',
        'WARNING': 'WARN',
        'DEBUG': 'DEBU'
    })
    formatter.override_colors({
        'prefix': CL_BLDYLW + CL_BAKPUR,
        'debug': CL_BLDWHT + CL_BAKBLU,
        'info': CL_TXTWHT + CL_BAKGRN,
        'warning': CL_BLDBLK + CL_BAKYLW,
        'error': CL_BLDYLW + CL_BAKRED,
        'critical': CL_BLDYLW + CL_BAKRED
    })
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    ch.setFormatter(formatter)
    logger.addHandler(ch)

    return logger
Beispiel #11
0
 def test_unicode(self):
     formatter = TextFormatter(colorize=False)
     log = self.get_logger(formatter)
     log.debug("😄 😁 😆 😅 😂")
     with open(self.filename, 'rb') as f:
         content = f.readlines()[-1]
         self.assertIn(
             "\\xf0\\x9f\\x98\\x84 "
             "\\xf0\\x9f\\x98\\x81 "
             "\\xf0\\x9f\\x98\\x86 "
             "\\xf0\\x9f\\x98\\x85 "
             "\\xf0\\x9f\\x98\\x82"
             "\\n", repr(content))
def get_logger():
    try:
        logging.setLoggerClass(PyLogrus)
        logger = logging.getLogger(__name__)
        logger.setLevel(logging.DEBUG)
        formatter = TextFormatter(datefmt='Z', colorize=True)
        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)
        ch.setFormatter(formatter)
        logger.addHandler(ch)
        return logger
    except Exception as e:
        raise("Error getting logger: ", e)
Beispiel #13
0
    def test_contextual_logging(self):
        formatter = TextFormatter(colorize=False)
        log = self.get_logger(formatter)

        log_ctx = log.withFields({'context': 1})
        log_ctx.withFields({'user': '******'}).info("contextual logger")
        with open(self.filename) as f:
            content = f.readlines()[-1]
            self.assertIn("; context=1", content)
            self.assertIn("; user=John Doe", content)

        log.withFields({'company': 'Awesome Company'}).info("default logger")
        with open(self.filename) as f:
            content = f.readlines()[-1]
            self.assertNotIn("; context=1", content)
            self.assertIn("; company=Awesome Company", content)
Beispiel #14
0
def get_logger():
    logging.setLoggerClass(PyLogrus)

    logger = logging.getLogger(__name__)  # type: PyLogrus
    logger.setLevel(logging.DEBUG)

    formatter = TextFormatter(datefmt='Z', colorize=True)
    # formatter.override_level_names({'CRITICAL': 'CRIT', 'ERROR': 'ERRO', 'WARNING': 'WARN', 'DEBUG': 'DEBU'})
    # formatter.override_colors({'prefix': CL_BLDYLW})

    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    ch.setFormatter(formatter)
    logger.addHandler(ch)

    return logger
Beispiel #15
0
def get_logger(name):
    logging.setLoggerClass(PyLogrus)
    logger = logging.getLogger(name)  # type: PyLogrus
    logger.setLevel(logging.DEBUG)

    formatter = TextFormatter(datefmt='Z', colorize=True)
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    ch.setFormatter(formatter)
    logger.addHandler(ch)

    ch = logging.FileHandler('/var/log/py_logrus_sample2.log')
    ch.setLevel(logging.ERROR)
    ch.setFormatter(formatter)
    logger.addHandler(ch)

    return logger
Beispiel #16
0
    def test_message_with_prefix(self):
        formatter = TextFormatter(colorize=False)
        log = self.get_logger(formatter)
        log_pfx = log.withPrefix("[API]")

        log_pfx.info("Log message with the prefix")
        with open(self.filename) as f:
            content = f.readlines()[-1]
            self.assertTrue(
                content.endswith("[API] Log message with the prefix\n"))

        log_pfx.critical("Another one log message with the prefix")
        with open(self.filename) as f:
            content = f.readlines()[-1]
            self.assertTrue(
                content.endswith(
                    "[API] Another one log message with the prefix\n"))
Beispiel #17
0
def setup(name):
    logging.setLoggerClass(PyLogrus)

    # Set root log level - higher handlers can set their own filter level
    logging.root.setLevel(logging.DEBUG)
    log = logging.getLogger(name)

    if log.hasHandlers():
        handler = log.handlers[0]
    else:
        # Add our handler
        handler = logging_handlers.SysLogHandler(address='/dev/log')
        handler.setFormatter(
            TextFormatter(fmt='%(levelname)s %(message)s', colorize=False))
        log.addHandler(handler)

    return log, handler
Beispiel #18
0
    def __init__(self, config_path):
        self.config = self.get_config(config_path)

        # Setup logging tool
        logging.setLoggerClass(PyLogrus)
        logger = logging.getLogger(__name__)  # type: PyLogrus
        logger.setLevel(logging.DEBUG)

        formatter = TextFormatter(datefmt='Z', colorize=True)
        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)
        ch.setFormatter(formatter)

        jsonformatter = JsonFormatter(datefmt='Z')
        fh = logging.FileHandler("logs/cloudalgo.log", 'w+')
        fh.setLevel(logging.DEBUG)
        fh.setFormatter(jsonformatter)

        logger.addHandler(ch)
        logger.addHandler(fh)
        self.log = logger.withFields({"node": "cloud-algo"})
Beispiel #19
0
def setup(name):
    logging.setLoggerClass(SFPyLogrus)

    # Set root log level - higher handlers can set their own filter level
    logging.root.setLevel(logging.DEBUG)
    log = logging.getLogger(name)

    if log.hasHandlers():
        # The parent logger might have the handler, not this lower logger
        if len(log.handlers) > 0:
            # TODO(andy): Remove necessity to return handler or
            # correctly obtain the handler without causing an exception
            handler = log.handlers[0]
    else:
        # Add our handler
        handler = logging_handlers.SysLogHandler(address='/dev/log')
        handler.setFormatter(
            TextFormatter(fmt='%(levelname)s %(message)s', colorize=False))
        log.addHandler(handler)

    return log.withPrefix(), handler
Beispiel #20
0
import utils.env_utils as env_utils
import random
import numpy as np
import pandas as pd
import torch
import torchvision
import requests
import io
from PIL import Image
import os

# Setup logging tool
logging.setLoggerClass(PyLogrus)
logger = logging.getLogger(__name__)  # type: PyLogrus
logger.setLevel(logging.DEBUG)
formatter = TextFormatter(datefmt='Z', colorize=True)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
logger.addHandler(ch)


# Base environment class. The environment class loads the
# appropriate imports, state, function and variables. This
# allows the functions, state, and imports to be accessed
# inside the process dealing with the requests from a client.
class Environment:
    pass


# Environment class for a site. Loads the imports, state and
Beispiel #21
0
from yaml import safe_load
from datetime import datetime
from time import sleep

from subprocess import check_output
from shutil import copyfile

import logging
from pylogrus import PyLogrus, TextFormatter

logging.setLoggerClass(PyLogrus)

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

formatter = TextFormatter(datefmt="Z", colorize=True)

ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
logger.addHandler(ch)

config.load_kube_config()

corev1_api = client.CoreV1Api(client.ApiClient())
apps_v1_api = client.AppsV1Api(client.ApiClient())
webhook_api = client.AdmissionregistrationV1beta1Api(client.ApiClient())
cert_api = client.CertificatesV1beta1Api(client.ApiClient())

APP_NAME = "maglev-admission-webhook"
SECRET = "shhhh!donottell"