Skip to content

Commit

Permalink
Add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
tongyx361 committed Sep 26, 2024
1 parent ec53bd2 commit 123baf7
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 41 deletions.
38 changes: 9 additions & 29 deletions nbs/00_core.ipynb → nbs/99_utils.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@
"metadata": {},
"outputs": [],
"source": [
"# | hide\n",
"%load_ext autoreload\n",
"%autoreload 2"
"%autoreload 2\n",
"\n",
"from nbdev.showdoc import *\n",
"from fastcore.test import *"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# core\n",
"# Utilities\n",
"\n",
"> Fill in a module description here\n"
"> Common utilities used across modules.\n"
]
},
{
Expand All @@ -25,17 +29,7 @@
"metadata": {},
"outputs": [],
"source": [
"# | default_exp core"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# | hide\n",
"from nbdev.showdoc import *"
"from pkg_name.utils import *"
]
},
{
Expand All @@ -44,21 +38,7 @@
"metadata": {},
"outputs": [],
"source": [
"# | export\n",
"def foo():\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# | hide\n",
"import nbdev\n",
"\n",
"nbdev.nbdev_export()"
"show_doc(init_logging)"
]
}
],
Expand Down
2 changes: 1 addition & 1 deletion pkg_name/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
'doc_host': 'https://tongyx361.github.io',
'git_url': 'https://github.com/tongyx361/nbdev-template-tongyx361',
'lib_path': 'pkg_name'},
'syms': {'pkg_name.core': {'pkg_name.core.foo': ('core.html#foo', 'pkg_name/core.py')}}}
'syms': {'pkg_name.utils': {'pkg_name.utils.init_logging': ('utils.html#init_logging', 'pkg_name/utils.py')}}}
10 changes: 0 additions & 10 deletions pkg_name/core.py

This file was deleted.

56 changes: 56 additions & 0 deletions pkg_name/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
Common utilities used across modules.
"""

import logging
from typing import Optional


# %% ../nbs/99_utils.ipynb 0
def init_logging(
fmt: str = "[%(levelname)s] [%(asctime)s.%(msecs)d] [pid %(process)d] [%(pathname)s:%(lineno)d:%(funcName)s]\n%(message)s",
datefmt: str = "%Y-%m-%d %H:%M:%S",
level: int = logging.INFO,
log_path: Optional[str] = None,
file_mode: str = "w",
force: bool = True,
) -> None:
"""
Initialize logging.
Parameters
----------
format: str
Format of the log message.
datefmt: str
Date format of the log message.
level: int
Level of the log message.
log_path: Optional[str]
Path to the log file.
file_mode: str
Mode of the log file.
force: bool
Whether to force the logging to be initialized.
"""
if force:
logging.shutdown()

logging.basicConfig(
format=fmt,
datefmt=datefmt,
level=level,
force=force,
)

if log_path is not None:
file_handler = logging.FileHandler(log_path, mode=file_mode)
file_handler.setLevel(level)
file_handler.setFormatter(logging.Formatter(fmt=fmt, datefmt=datefmt))

logging.getLogger().addHandler(file_handler)
logging.info(f"log_path = {log_path}")


init_logging()
logging.info("Test logging.")
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ line_length = 88 # Should match Black's line length

[tool.mypy]
python_version = "3.11"
disallow_untyped_defs = true
disallow_untyped_defs = false
ignore_missing_imports = true
show_error_codes = true
strict_optional = true
Expand Down

0 comments on commit 123baf7

Please sign in to comment.