def test_logfile(capsys, log_file): config = libs.get_config( docopt(uddns_doc, version=uddns_ver, argv=['-n', 'x', '-u', 'x', '-p', 'x', '--log', log_file.name])) with libs.LoggingSetup(config['verbose'], config['log'], config['quiet']) as f: logging.config.fileConfig(f.config) # Setup logging. assert 2 == len(logging.getLogger().handlers) assert isinstance(logging.getLogger().handlers[0], libs.LoggingSetup.ConsoleHandler) assert isinstance(logging.getLogger().handlers[1], libs.LoggingSetup.TimedRotatingFileHandler) timestamp = log_samples() stdout_actual, stderr_actual = capsys.readouterr() stdout_expected = "Test info testing.\n" stderr_expected = "Test warn testing.\nTest error testing.\nTest critical testing.\n" assert stdout_expected == stdout_actual assert stderr_expected == stderr_actual log_actual = log_file.read(1024) log_expected = "%s INFO root Test info testing.\n" % timestamp log_expected += "%s WARNING root Test warn testing.\n" % timestamp log_expected += "%s ERROR root Test error testing.\n" % timestamp log_expected += "%s CRITICAL root Test critical testing.\n" % timestamp assert log_expected == log_actual
def test_default(capsys): config = libs.get_config( docopt(uddns_doc, version=uddns_ver, argv=['-n', 'x', '-u', 'x', '-p', 'x'])) with libs.LoggingSetup(config['verbose'], config['log'], config['quiet']) as f: logging.config.fileConfig(f.config) # Setup logging. assert 1 == len(logging.getLogger().handlers) assert isinstance(logging.getLogger().handlers[0], libs.LoggingSetup.ConsoleHandler) log_samples() stdout_actual, stderr_actual = capsys.readouterr() stdout_expected = "Test info testing.\n" stderr_expected = "Test warn testing.\nTest error testing.\nTest critical testing.\n" assert stdout_expected == stdout_actual assert stderr_expected == stderr_actual
def test_quiet(capsys): config = libs.get_config( docopt(uddns_doc, version=uddns_ver, argv=['-n', 'x', '-u', 'x', '-p', 'x', '--quiet'])) with libs.LoggingSetup(config['verbose'], config['log'], config['quiet']) as f: logging.config.fileConfig(f.config) # Setup logging. assert len(logging.getLogger().handlers) == 1 assert isinstance(logging.getLogger().handlers[0], libs.LoggingSetup.NullHandler) log_samples() stdout_actual, stderr_actual = capsys.readouterr() stdout_expected = "" stderr_expected = "" assert stdout_expected == stdout_actual assert stderr_expected == stderr_actual
def log_file(request): f = tempfile.NamedTemporaryFile() with libs.LoggingSetup(True, f.name, False) as cm: logging.config.fileConfig(cm.config) request.addfinalizer(lambda: f.close()) return f
if __name__ == "__main__": signal.signal(signal.SIGINT, lambda a, b: sys.exit(0)) # Properly handle Control+C # Get CLI args/options and parse config file. try: main_config = libs.get_config(docopt(__doc__, version=__version__)) except libs.MultipleConfigSources.ConfigError as e: print("ERROR: %s" % e, file=sys.stderr) sys.exit(1) # Initialize logging. umask = 0o027 os.umask(umask) with libs.LoggingSetup(main_config['verbose'], main_config['log'], main_config['quiet']) as cm: try: logging.config.fileConfig(cm.config) # Setup logging. except IOError: print("ERROR: Unable to write to file %s" % main_config['log'], file=sys.stderr) sys.exit(1) sys.excepthook = lambda t, v, b: logging.critical( "Uncaught exception!", exc_info=(t, v, b)) # Log exceptions. atexit.register( lambda: logging.info("%s pid %d shutting down." % (__program__, os.getpid()))) # Log when exiting. logging.info("Starting %s version %s" % (__program__, __version__)) # Initialize context manager. Daemonize, use pid file, or both, or none! cm = PidFile(main_config['pid']) if main_config['pid'] else None