def test_standards_suppressed_is_not_warn(logging, log):
    """
    Ensure that the new SUPPRESSED level does not collide with the
    existing python WARN level.
    """
    add_standards(logging)
    assert logging.SUPPRESSED == logging.WARN + 1
def test_level_standards(logging, log):
    """
    Ensure that the standard log levels work
    """
    add_standards(logging)

    assert logging.EMERGENCY == 100
    log.emergency("Emergency!")
    assert log.last() == ['EMERGENCY', "Emergency!"]

    assert logging.ALERT == 70
    log.alert("Alert!")
    assert log.last() == ['ALERT', "Alert!"]

    assert logging.NOTICE == 25
    log.notice("FYI")
    assert log.last() == ['NOTICE', "FYI"]

    assert logging.VERBOSE == 7
    log.verbose("I've said too much")
    assert log.last() == ['VERBOSE', "I've said too much"]

    assert logging.TRACE == 5
    log.trace("But I haven't said enough")
    assert log.last() == ['TRACE', "But I haven't said enough"]
Esempio n. 3
0
def setupLogger(configfile="./logger.cfg",
                consoleLevel="INFO",
                logDir="./",
                logFile="default.log"):
    """ Setup a console and file logger
	:param consolelevel: Sets the loglevel for the console (string). The file log
	 level is always DEBUG
	:param logDir: Set the destination dir for the logfile.
	:param logFile: The filename of the logfile. If no extension is given '.log'
	 will be appended
	"""
    # Setup directories
    logDir = logDir if logDir.endswith('/') else logDir + '/'
    logFile = logFile if '.' in logFile else logFile + '.log'
    if not os.path.exists(logDir): os.mkdir(logDir)

    # Add more log levels
    add_standards(logging)

    if os.path.exists(configfile):  # load configuration form file
        with open(configfile, "rt") as f:
            cfg = json.load(f)
            # Set logfile location if not in config:
            if not cfg["handlers"]["file"].get("filename"):
                cfg["handlers"]["file"]["filename"] = logDir + logFile
            # Overwrite console log level
            if cfg["handlers"].get("console"):
                cfg["handlers"]["console"]["level"] = consoleLevel
            # Set config
            logging.config.dictConfig(cfg)
    else:
        logging.basicConfig()
    log = logging.getLogger(sys.modules['__main__'].__file__)
    log.trace("Logger set up")
    return log
Esempio n. 4
0
def test_level_standards(logging, log):
    """
    Ensure that the standard log levels work 
    """
    add_standards(logging)
    
    assert logging.TRACE == 5
    assert logging.VERBOSE == 7

    log.verbose("I've said too much")
    assert log.last() == ['VERBOSE', "I've said too much"]

    log.trace("But I haven't said enough")
    assert log.last() == ['TRACE', "But I haven't said enough"]
Esempio n. 5
0
def test_standards_suppressed(logging, log):
    """
    Ensure that the suppressed log level includes
    the suppressed exception
    """
    add_standards(logging)

    assert logging.SUPPRESSED

    try:
        raise Exception('Suppress this')
    except:
        log.suppressed('Suppressed exception')

    lines = ''.join(log.readlines())
    assert lines.startswith('SUPPRESSED:')
    assert 'Exception: Suppress this' in lines
#!/usr/bin/python3
import os
import sys
import logging
from logging_levels.standards import add_standards
import unittest
import subprocess
from pathlib import Path

MODULE = sys.modules['__main__'].__file__
__version__ = "0.0.1"
DESCRIPTION = MODULE+""":
High level test: Run the main program and check for correct output
"""
EXAMPLE_USAGE = """"""
add_standards(logging)

class TestProgram(unittest.TestCase):
	PROGRAM_NAME = "template_main.py"
	RET_OK = 0
	RET_ARGERR = 2
	ARG_INVALID = "--invalidargument"
	ARG_HELP = "--help"
	ARG_VERSION = "--version"
	ARG_CFGF = "--config"
	ARG_INPUT = "-i"
	ARG_OUTPUT = "-o"
	INVALID_FILE = "nonexistantfile"
	TMP_INPUT_FILE = "tmpinput.tmp"
	TMP_OUTPUT_FILE = "tmpoutput.tmp"
	TEST_CONFIG_FILE = ""