Skip to content

Commit

Permalink
feat: add option to log output into file
Browse files Browse the repository at this point in the history
  • Loading branch information
vzhd1701 committed Dec 25, 2021
1 parent 95d7796 commit f50bb58
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
17 changes: 15 additions & 2 deletions enex2notion/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import sys
from pathlib import Path
from typing import Optional

from notion.client import NotionClient

Expand Down Expand Up @@ -90,7 +91,7 @@ def _get_notebook_root(self, notebook_title):
def cli(argv):
args = parse_args(argv)

_setup_logging(args.verbose)
_setup_logging(args.verbose, args.log)

if args.mode_webclips == "PDF":
ensure_wkhtmltopdf()
Expand Down Expand Up @@ -177,6 +178,11 @@ def parse_args(argv):
"metavar": "FILE",
"help": "file for uploaded notes hashes to resume interrupted upload",
},
"--log": {
"type": Path,
"metavar": "FILE",
"help": "file to store program log",
},
"--verbose": {
"action": "store_true",
"default": False,
Expand All @@ -194,11 +200,18 @@ def parse_args(argv):
return parser.parse_args(argv)


def _setup_logging(is_verbose):
def _setup_logging(is_verbose: bool, log_file: Optional[Path]):
logging.basicConfig(format="%(levelname)s: %(message)s")

logging.getLogger("enex2notion").setLevel(
logging.DEBUG if is_verbose else logging.INFO
)

if log_file:
file_handler = logging.FileHandler(log_file)
file_handler.setFormatter(
logging.Formatter("%(asctime)s [%(levelname)-8.8s] %(message)s")
)
logging.getLogger("enex2notion").addHandler(file_handler)

logging.getLogger("urllib3").setLevel(logging.ERROR)
13 changes: 13 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

import pytest

from enex2notion.cli import cli
Expand Down Expand Up @@ -185,5 +187,16 @@ def test_unhandled_exception(mock_api, fake_note_factory, caplog):
assert "Unhandled exception while parsing note" in caplog.text


def test_file_log(mock_api, fake_note_factory, fs):
fs.create_file("log.txt")

cli(["--log", "log.txt", "fake.enex"])

with open("log.txt") as f:
done_result = f.read()

assert "No token provided, dry run mode." in done_result


def test_cli_main_import():
from enex2notion import __main__

0 comments on commit f50bb58

Please sign in to comment.