Skip to content

Commit 8f61189

Browse files
authored
feat: enhance logging setup and add rich output support with figlet
feat: enhance logging setup and add rich output support with figlet
2 parents 8f17fe2 + 382616b commit 8f61189

8 files changed

Lines changed: 137 additions & 20 deletions

File tree

.github/workflows/docker-build-and-scan.yaml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ jobs:
3939
id: build-image
4040
uses: docker/build-push-action@v6
4141
with:
42-
context: ${{ env.DOCKER_PATH_CONTEXT }}
43-
file: ${{ env.DOCKER_BUILD_DOCKERFILE }}
44-
load: ${{ env.DOCKER_LOAD_BOOL }}
45-
tags: ${{ env.DOCKER_TAGS }}
42+
context: ${{ inputs.DOCKER_PATH_CONTEXT || env.DOCKER_PATH_CONTEXT }}
43+
file: ${{ inputs.DOCKER_BUILD_DOCKERFILE || env.DOCKER_BUILD_DOCKERFILE }}
44+
load: ${{ inputs.DOCKER_LOAD_BOOL || env.DOCKER_LOAD_BOOL }}
45+
tags: ${{ inputs.DOCKER_TAGS || env.DOCKER_TAGS }}
4646
- name: Run Trivy vulnerability scanner
4747
uses: aquasecurity/trivy-action@0.33.1
4848
with:
49-
image-ref: ${{ env.DOCKER_TAGS }}
49+
image-ref: ${{ inputs.DOCKER_TAGS || env.DOCKER_TAGS }}
5050
format: 'table'
5151
exit-code: '1'
5252
ignore-unfixed: true
@@ -62,7 +62,7 @@ jobs:
6262
if: github.event_name == 'workflow_call'
6363
uses: docker/build-push-action@v6
6464
with:
65-
context: ${{ env.DOCKER_PATH_CONTEXT }}
66-
file: ${{ env.DOCKER_BUILD_DOCKERFILE }}
65+
context: ${{ inputs.DOCKER_PATH_CONTEXT || env.DOCKER_PATH_CONTEXT }}
66+
file: ${{ inputs.DOCKER_BUILD_DOCKERFILE || env.DOCKER_BUILD_DOCKERFILE }}
6767
push: true
68-
tags: ${{ env.DOCKER_TAGS }}
68+
tags: ${{ inputs.DOCKER_TAGS || env.DOCKER_TAGS }}

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ dependencies = [
1313
"pathlib>=1.0.1",
1414
"pydantic>=2.12.5",
1515
"pydantic-settings>=2.12.0",
16+
"pyfiglet>=1.0.4",
1617
"pylint>=4.0.4",
1718
"pylint-pydantic>=0.4.1",
1819
"pytest>=9.0.2",
20+
"rich>=14.3.2",
1921
]
2022

2123
[project.optional-dependencies]

src/sample_python_app/core/logging.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,25 @@
1212
)
1313

1414

15-
def setup_logger():
15+
def setup_logger(mode="normal"):
1616
logger.remove()
17-
logger.add(sink=lambda msg: print(msg, end=""), format=log_format, level="INFO")
18-
logger.add(
19-
"app.log",
20-
format=log_format,
21-
level="DEBUG",
22-
rotation="1 MB",
23-
retention="10 days",
24-
compression="zip",
25-
)
17+
if mode == "silent":
18+
logger.add(
19+
"app.log",
20+
format=log_format,
21+
level="DEBUG",
22+
rotation="1 MB",
23+
retention="10 days",
24+
compression="zip",
25+
)
26+
else:
27+
logger.add(sink=lambda msg: print(msg, end=""), format=log_format, level="INFO")
28+
logger.add(
29+
"app.log",
30+
format=log_format,
31+
level="DEBUG",
32+
rotation="1 MB",
33+
retention="10 days",
34+
compression="zip",
35+
)
2636
return logger

src/sample_python_app/main.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22
Main entry point for the python-app-template project.
33
"""
44

5+
from pyfiglet import Figlet
6+
from rich.console import Console
7+
58
from sample_python_app.core import settings, setup_logger
69

710

811
def run_app():
9-
logger = setup_logger()
12+
console = Console()
13+
f = Figlet(font="slant")
14+
ascii_art = f.renderText("Hello, Synthwave!")
15+
console.print(f"[bold magenta]{ascii_art}[/bold magenta]")
16+
logger = setup_logger(mode="silent")
1017
logger.info(f"Starting {settings.APP_NAME}...")
1118
logger.info("Hello from python-app-template!")
1219

tests/test_figlet_rich.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Test for minimal figlet output using Rich and pyfiglet.
3+
"""
4+
5+
from pyfiglet import Figlet
6+
from rich.console import Console
7+
8+
9+
def test_figlet_rich_output(capfd):
10+
console = Console()
11+
f = Figlet(font="slant")
12+
ascii_art = f.renderText("Hello, Synthwave!")
13+
console.print(f"[bold magenta]{ascii_art}[/bold magenta]")
14+
out, _ = capfd.readouterr()
15+
# Check for a distinctive substring from the ASCII art output
16+
assert "__ __" in out or "/ / / /__" in out

tests/test_logging.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Tests for sample_python_app.core.logging
3+
"""
4+
5+
import time
6+
7+
from loguru import logger as loguru_logger
8+
9+
from sample_python_app.core.logging import setup_logger
10+
11+
12+
def test_logger_normal_mode_stdout(capsys):
13+
logger = setup_logger("normal")
14+
logger.info("Test normal mode log")
15+
out, _ = capsys.readouterr()
16+
assert "Test normal mode log" in out
17+
18+
19+
def test_logger_silent_mode_no_stdout(capsys):
20+
logger = setup_logger("silent")
21+
logger.info("Test silent mode log")
22+
out, _ = capsys.readouterr()
23+
assert "Test silent mode log" not in out
24+
25+
26+
def test_logger_file_logging(tmp_path):
27+
log_path = tmp_path / "app.log"
28+
loguru_logger.remove()
29+
loguru_logger.add(str(log_path), format="{message}", level="DEBUG")
30+
loguru_logger.info("File log test")
31+
time.sleep(0.1)
32+
with open(log_path, "r", encoding="utf-8") as f:
33+
contents = f.read()
34+
assert "File log test" in contents

tests/test_main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ def test_main_subprocess():
2121
def test_main_runs(capfd):
2222
main.run_app()
2323
out, _ = capfd.readouterr()
24-
assert "Hello from python-app-template!" in out
24+
# Check for a distinctive substring from the ASCII art output
25+
assert "__ __" in out or "/ / / /__" in out

uv.lock

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)