Skip to content

Commit

Permalink
Format non-strings automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Mar 7, 2022
1 parent a934622 commit 3aa6e95
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 10 deletions.
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"cSpell.words": [
"coveragerc",
"minilog",
"minimalistic",
"sinfo",
"USERPROFILE",
"venv",
"verchew"
]
}
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 2.1 (beta)
# 2.1 (2022-03-06)

- Dropped support for Python 3.6.
- Added automatic formatting of non-string messages.

# 2.0.1 (2021-06-03)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ if __name__ == "__main__":
log.init()
```

It will produce the exact same standard library `logging` records behind the scenes.
It will produce the exact same standard library `logging` records behind the scenes with automatic formatting for non-strings.

## Installation

Expand Down
11 changes: 10 additions & 1 deletion log/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import logging

from log.utils import create_logger_record, parse_name
from log.utils import create_logger_record, format_message, parse_name


def describe_create_logger_record():
Expand All @@ -29,3 +29,12 @@ def it_uses_the_filename_when_main(expect):

def it_handles_interactive_sessions(expect):
expect(parse_name("__main__", {})[0]) == "interactive"


def describe_format_message():
def it_formats_structures(expect):
data = {x: x * 10 for x in range(20)}
expect(format_message(data).count("\n")) == 19

def it_preserves_strings(expect):
expect(format_message("foobar")) == "foobar"
19 changes: 13 additions & 6 deletions log/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import inspect
import logging
from pprint import pformat
from typing import Dict, Tuple

from . import state
Expand All @@ -24,9 +25,9 @@ def create_logger_record(
record = logger.makeRecord(
name,
level,
fn=parse_fn(frame.f_globals),
fn=parse_filename(frame.f_globals),
lno=frame.f_lineno,
msg=message,
msg=format_message(message),
args=args,
exc_info=exc_info,
extra=kwargs,
Expand All @@ -36,10 +37,6 @@ def create_logger_record(
return True


def parse_fn(frame_info: Dict) -> str:
return frame_info.get("__file__", "interactive")


def parse_name(custom_name: str, frame_info: Dict) -> Tuple[str, str]:
module_name = custom_name or frame_info["__name__"]
if module_name == "__main__":
Expand All @@ -59,3 +56,13 @@ def get_logger(name: str, parent_name: str):
if not logger.level:
logger.level = logging.root.level
return logger


def parse_filename(frame_info: Dict) -> str:
return frame_info.get("__file__", "interactive")


def format_message(value) -> str:
if not isinstance(value, str):
value = pformat(value)
return value
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]

name = "minilog"
version = "2.1b1"
version = "2.1"
description = "Minimalistic wrapper for Python logging."

license = "MIT"
Expand Down

0 comments on commit 3aa6e95

Please sign in to comment.