Skip to content

Commit 9d69542

Browse files
authored
Merge pull request #17 from kjappelbaum/kjappelbaum/issue16
chore: make logger more customizable
2 parents f7d6531 + 0903296 commit 9d69542

File tree

6 files changed

+125
-59
lines changed

6 files changed

+125
-59
lines changed

.flake8

-36
This file was deleted.

.github/workflows/tests.yml

+20-20
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,26 @@ jobs:
2020
pip freeze
2121
- name: Run pre-commit
2222
run: pre-commit run --all-files || ( git status --short ; git diff ; exit 1 )
23-
docs:
24-
name: Documentation
25-
runs-on: ubuntu-latest
26-
strategy:
27-
matrix:
28-
python-version: ["3.8"]
29-
steps:
30-
- uses: actions/checkout@v2
31-
- name: Set up Python ${{ matrix.python-version }}
32-
uses: actions/setup-python@v2
33-
with:
34-
python-version: ${{ matrix.python-version }}
35-
- name: Install dependencies
36-
run: pip install tox
37-
- name: Check RST conformity with doc8
38-
run: tox -e doc8
39-
- name: Check docstring coverage
40-
run: tox -e docstr-coverage
41-
- name: Check documentation build with Sphinx
42-
run: tox -e docs
23+
# docs:
24+
# name: Documentation
25+
# runs-on: ubuntu-latest
26+
# strategy:
27+
# matrix:
28+
# python-version: ["3.8"]
29+
# steps:
30+
# - uses: actions/checkout@v2
31+
# - name: Set up Python ${{ matrix.python-version }}
32+
# uses: actions/setup-python@v2
33+
# with:
34+
# python-version: ${{ matrix.python-version }}
35+
# - name: Install dependencies
36+
# run: pip install tox
37+
# - name: Check RST conformity with doc8
38+
# run: tox -e doc8
39+
# - name: Check docstring coverage
40+
# run: tox -e docstr-coverage
41+
# - name: Check documentation build with Sphinx
42+
# run: tox -e docs
4343
tests:
4444
name: Tests
4545
runs-on: ${{ matrix.os }}

docs/source/api.rst

+39-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
API documentation
22
=======================
33

4-
54
Analysis
65
-------------
76
.. automodule:: structuregraph_helpers.analysis
@@ -30,4 +29,42 @@ Subgraph
3029
Hash
3130
------- -
3231
.. automodule:: structuregraph_helpers.hash
33-
:members:
32+
:members:
33+
34+
35+
36+
Logging
37+
---------
38+
39+
structuregraph_helpers uses the `loguru <https://loguru.readthedocs.io/en/stable/index.html>`_ for logging.
40+
By default, logging from structuregraph_helpers is disabled to not interfere with your logs.
41+
42+
However, you can easily customize the logging:
43+
44+
.. code-block:: python
45+
46+
import sys
47+
from loguru import logger
48+
49+
# enable structuregraph_helpers logging
50+
logger.enable("structuregraph_helpers")
51+
52+
# define the logging level
53+
LEVEL = "INFO || DEBUG || WARNING || etc."
54+
55+
# set the handler
56+
# for logging to stdout
57+
logger.add(sys.stdout, level=LEVEL)
58+
# or for logging to a file
59+
logger.add("my_log_file.log", level=LEVEL, enqueue=True)
60+
61+
62+
In many cases, however, you might find it convenient to simply call :py:meth:`~structuregraph_helpers.utils.enable_logging`
63+
64+
.. code-block:: python
65+
66+
from structuregraph_helpers.utils import enable_logging
67+
68+
enable_logging()
69+
70+
which will enable logging with sane defaults (i.e. logging to ``stderr`` for ``INFO`` and ``WARNING`` levels).

setup.cfg

+36
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,39 @@ exclude_lines =
133133
[darglint]
134134
docstring_style = google
135135
strictness = short
136+
137+
#########################
138+
# Flake8 Configuration #
139+
# (.flake8) #
140+
#########################
141+
[flake8]
142+
ignore =
143+
S101 # assert
144+
S301 # pickle
145+
S403 # pickle
146+
S404
147+
S603
148+
W503 # Line break before binary operator (flake8 is wrong)
149+
E203 # whitespace before ':'
150+
exclude =
151+
.tox,
152+
.git,
153+
__pycache__,
154+
docs/source/conf.py,
155+
build,
156+
dist,
157+
tests/fixtures/*,
158+
*.pyc,
159+
*.egg-info,
160+
.cache,
161+
.eggs,
162+
data
163+
*/_hasher.py
164+
max-line-length = 120
165+
max-complexity = 20
166+
import-order-style = pycharm
167+
application-import-names =
168+
structuregraph_helpers
169+
tests
170+
per-file-ignores =
171+
tests/*.py:DAR101, D205, D100, DAR101, DAR201, D209, D103
+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
32
"""Utilities for working with structure graphs."""
3+
from loguru import logger
4+
5+
logger.disable("structuregraph_helpers")

src/structuregraph_helpers/utils.py

+27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
11
import json
2+
import sys
3+
from typing import List
4+
5+
from loguru import logger
26

37

48
def dump_json(data, filename):
59
with open(filename, "w") as f:
610
json.dump(data, f)
11+
12+
13+
def enable_logging() -> List[int]:
14+
"""Set up the structuregraph_helpers logging with sane defaults."""
15+
logger.enable("structuregraph_helpers")
16+
17+
config = dict(
18+
handlers=[
19+
dict(
20+
sink=sys.stderr,
21+
format="<green>{time:YYYY-MM-DD HH:mm:ss.SSS Z UTC}</>"
22+
" <red>|</> <lvl>{level}</> <red>|</> <cyan>{name}:{function}:{line}</>"
23+
" <red>|</> <lvl>{message}</>",
24+
level="INFO",
25+
),
26+
dict(
27+
sink=sys.stderr,
28+
format="<red>{time:YYYY-MM-DD HH:mm:ss.SSS Z UTC} | {level} | {name}:{function}:{line} | {message}</>",
29+
level="WARNING",
30+
),
31+
]
32+
)
33+
return logger.configure(**config)

0 commit comments

Comments
 (0)