Skip to content

Commit

Permalink
- When logger_setup called for the same logger multiple times, only s…
Browse files Browse the repository at this point in the history
…et 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.
  • Loading branch information
mauvilsa committed Jan 17, 2024
1 parent 4c40775 commit d0cb4f0
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 46 deletions.
56 changes: 30 additions & 26 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: 2.1
jobs:
build:
docker:
- image: circleci/python:3.8
- image: cimg/python:3.10
steps:
- checkout
- run:
Expand All @@ -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: |
Expand All @@ -32,47 +31,52 @@ 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:
at: .
- 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
Expand All @@ -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:
Expand Down
7 changes: 0 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions reconplogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand Down
14 changes: 7 additions & 7 deletions reconplogger_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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, {
Expand Down
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit d0cb4f0

Please sign in to comment.