From d0cb4f0bd721ecaaa779e88e4bc108aff8163fef Mon Sep 17 00:00:00 2001 From: Mauricio Villegas <5780272+mauvilsa@users.noreply.github.com> Date: Wed, 17 Jan 2024 13:49:36 +0100 Subject: [PATCH] - When logger_setup called for the same logger multiple times, only set parameters the first time. - logger_setup should not change the level of file handlers. - Drop support for python 3.6 and 3.7 and add testing for 3.11 and 3.12. --- .circleci/config.yml | 56 ++++++++++++++++++++++------------------- .pre-commit-config.yaml | 7 ------ README.rst | 2 -- reconplogger.py | 11 ++++++-- reconplogger_tests.py | 14 +++++------ setup.cfg | 4 +-- 6 files changed, 48 insertions(+), 46 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 9e14bf5..3091fbd 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -3,7 +3,7 @@ version: 2.1 jobs: build: docker: - - image: circleci/python:3.8 + - image: cimg/python:3.10 steps: - checkout - run: @@ -16,10 +16,9 @@ jobs: - ./dist/*.tar.gz test-py38: &test-py38 docker: - - image: circleci/python:3.8 + - image: cimg/python:3.8 steps: - - attach_workspace: - at: . + - checkout - run: name: Run unit tests command: | @@ -32,25 +31,25 @@ jobs: root: . paths: - ./coverage_*.xml - test-py310: + test-py312: <<: *test-py38 docker: - - image: circleci/python:3.10 - test-py39: + - image: cimg/python:3.12 + test-py311: <<: *test-py38 docker: - - image: circleci/python:3.9 - test-py37: + - image: cimg/python:3.11 + test-py310: <<: *test-py38 docker: - - image: circleci/python:3.7 - test-py36: + - image: cimg/python:3.10 + test-py39: <<: *test-py38 docker: - - image: circleci/python:3.6 + - image: cimg/python:3.9 codecov: docker: - - image: circleci/python:3.8 + - image: curlimages/curl:latest steps: - checkout - attach_workspace: @@ -58,21 +57,26 @@ jobs: - run: name: Code coverage command: | - for py in 3.6 3.7 3.8 3.9 3.10; do - bash <(curl -s https://codecov.io/bash) \ - -t $CODECOV_TOKEN_RECONPLOGGER \ - -F py$py \ - -f coverage_py$py.xml + curl -Os https://uploader.codecov.io/latest/linux/codecov + chmod +x codecov + for py in 3.8 3.9 3.10 3.11 3.12; do + ./codecov \ + --nonZero \ + --token $CODECOV_TOKEN_RECONPLOGGER \ + --flags py$py \ + --file coverage_py$py.xml done publish-pypi: docker: - - image: mauvilsa/docker-twine:1.11.0 + - image: cimg/python:3.10 steps: - attach_workspace: at: . - run: name: Publish Release on PyPI - command: twine upload --username "${PYPI_USER}" --password "${PYPI_PASS}" ./dist/*.whl ./dist/*.tar.gz + command: | + pip3 install -U twine + twine upload --username __token__ --password "${PYPI_TOKEN}" ./dist/*.whl ./dist/*.tar.gz workflows: version: 2 @@ -86,22 +90,22 @@ workflows: <<: *buildreq requires: - build - - test-py310: + - test-py312: <<: *testreq - - test-py39: + - test-py311: <<: *testreq - - test-py37: + - test-py310: <<: *testreq - - test-py36: + - test-py39: <<: *testreq - codecov: <<: *testreq requires: + - test-py312 + - test-py311 - test-py310 - test-py39 - test-py38 - - test-py37 - - test-py36 - publish-pypi: filters: branches: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b7da65a..8199a8c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -18,13 +18,6 @@ repos: pass_filenames: false verbose: true - - name: circleci config - id: circleci config validate -c .circleci/config.yml - entry: circleci config validate -c - language: system - files: .circleci/config.yml - verbose: true - - name: pylint id: pylint --errors-only --disable=no-member entry: pylint --errors-only --disable=no-member diff --git a/README.rst b/README.rst index 875f9f1..5dd4e55 100644 --- a/README.rst +++ b/README.rst @@ -2,8 +2,6 @@ :target: https://circleci.com/gh/omni-us/reconplogger .. image:: https://codecov.io/gh/omni-us/reconplogger/branch/master/graph/badge.svg :target: https://codecov.io/gh/omni-us/reconplogger -.. image:: https://sonarcloud.io/api/project_badges/measure?project=omni-us_reconplogger&metric=alert_status - :target: https://sonarcloud.io/dashboard?id=omni-us_reconplogger .. image:: https://badge.fury.io/py/reconplogger.svg :target: https://badge.fury.io/py/reconplogger .. image:: https://img.shields.io/badge/contributions-welcome-brightgreen.svg diff --git a/reconplogger.py b/reconplogger.py index ae2aca5..689bfc8 100644 --- a/reconplogger.py +++ b/reconplogger.py @@ -240,7 +240,12 @@ def logger_setup( load_config(os.getenv(env_cfg, config), reload=reload) # Get logger - logger = get_logger(os.getenv(env_name, logger_name)) + name = os.getenv(env_name, logger_name) + logger = get_logger(name) + if getattr(logger, '_reconplogger_setup', False) and not reload: + if parent or level or init_messages: + logger.debug(f'logger {name} already setup by reconplogger, ignoring overriding parameters.') + return logger # Override parent logger.parent = parent @@ -256,13 +261,15 @@ def logger_setup( else: raise ValueError('Expected level argument to be a string.') for handler in logger.handlers: - handler.setLevel(level) + if not isinstance(handler, logging.FileHandler): + handler.setLevel(level) # Log configured done and test logger if init_messages: logger.info('reconplogger (v'+__version__+') logger configured.') test_logger(logger) + logger._reconplogger_setup = True return logger diff --git a/reconplogger_tests.py b/reconplogger_tests.py index f969102..d67fdda 100755 --- a/reconplogger_tests.py +++ b/reconplogger_tests.py @@ -40,12 +40,12 @@ def test_default_logger(self): def test_log_level(self): """Test load config with the default config and plain logger changing the log level.""" - logger = reconplogger.logger_setup(level='INFO') + logger = reconplogger.logger_setup(level='INFO', reload=True) self.assertEqual(logger.handlers[0].level, logging.INFO) - logger = reconplogger.logger_setup(level='ERROR') + logger = reconplogger.logger_setup(level='ERROR', reload=True) self.assertEqual(logger.handlers[0].level, logging.ERROR) with patch.dict(os.environ, {'LOGGER_LEVEL': 'WARNING'}): - logger = reconplogger.logger_setup(level='INFO', env_prefix='LOGGER') + logger = reconplogger.logger_setup(level='INFO', env_prefix='LOGGER', reload=True) self.assertEqual(logger.handlers[0].level, logging.WARNING) def test_default_logger_with_exception(self): @@ -148,10 +148,10 @@ def test_undefined_logger(self): ValueError, lambda: reconplogger.logger_setup('undefined_logger')) def test_logger_setup_invalid_level(self): - self.assertRaises( - ValueError, lambda: reconplogger.logger_setup(level='INVALID')) - self.assertRaises( - ValueError, lambda: reconplogger.logger_setup(level=True)) + with self.assertRaises(ValueError): + reconplogger.logger_setup(level='INVALID', reload=True) + with self.assertRaises(ValueError): + reconplogger.logger_setup(level=True, reload=True) @unittest.skipIf(not Flask, "flask package is required") @patch.dict(os.environ, { diff --git a/setup.cfg b/setup.cfg index c93588f..af66da7 100644 --- a/setup.cfg +++ b/setup.cfg @@ -3,7 +3,7 @@ py_modules = reconplogger reconplogger_tests test_suite = reconplogger_tests -python_requires = >=3.6 +python_requires = >=3.8 install_requires = logmatic-python>=0.1.7 PyYAML>=3.13 @@ -76,7 +76,7 @@ build_dir = sphinx/_build [tox:tox] -envlist = py{36,37,38,39,310},no-extras +envlist = py{38,39,310,311,312},no-extras [testenv]