Skip to content

Commit 64ca1bf

Browse files
committed
remove requirement to log to file
1 parent ec8c2db commit 64ca1bf

File tree

3 files changed

+41
-31
lines changed

3 files changed

+41
-31
lines changed

fancylog/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
most_recent_version = luddite.get_version_pypi(__name__)
1212

1313
if version.parse(__version__) < version.parse(most_recent_version):
14-
print(f"This version of {__name__} ({__version__}) is not the most recent "
15-
f"version. Please update to v{most_recent_version} by running: "
16-
f"'pip install {__name__} --upgrade'")
14+
print(
15+
f"This version of {__name__} ({__version__}) is not the most recent "
16+
f"version. Please update to v{most_recent_version} by running: "
17+
f"'pip install {__name__} --upgrade'"
18+
)

fancylog/fancylog.py

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323

2424
def start_logging(
25-
output_dir,
26-
package,
25+
output_dir=None,
26+
package=None,
2727
variables=None,
2828
verbose=True,
2929
file_log_level="DEBUG",
@@ -34,6 +34,7 @@ def start_logging(
3434
write_git=True,
3535
write_cli_args=True,
3636
write_variables=True,
37+
log_to_file=True,
3738
):
3839
"""
3940
Prepares the log file, and then begins logging.
@@ -55,6 +56,8 @@ def start_logging(
5556
:param write_cli_args: Log the command-line arguments. Default: True
5657
:param write_variables: Write the attributes of selected objects.
5758
Default: True
59+
:param log_to_file: If True, write a log file, otherwise just print to
60+
terminal.
5861
:return: Path to the logging file
5962
"""
6063
# TODO: accept PosixPath
@@ -63,26 +66,31 @@ def start_logging(
6366
else:
6467
print_log_level = "INFO"
6568

66-
if filename is None:
67-
filename = package.__name__
68-
69-
logging_file = datetime.now().strftime(filename + "_%Y-%m-%d_%H-%M-%S.log")
70-
try:
71-
logging_file = os.path.join(output_dir, logging_file)
72-
except TypeError:
73-
logging_file = output_dir.joinpath(logging_file)
74-
75-
LoggingHeader(
76-
logging_file,
77-
package,
78-
variables,
79-
output_dir,
80-
write_header=write_header,
81-
write_git=write_git,
82-
write_cli_args=write_cli_args,
83-
write_variables=write_variables,
84-
log_header=log_header,
85-
)
69+
if log_to_file:
70+
if filename is None:
71+
filename = package.__name__
72+
logging_file = datetime.now().strftime(
73+
filename + "_%Y-%m-%d_%H-%M-%S.log"
74+
)
75+
try:
76+
logging_file = os.path.join(output_dir, logging_file)
77+
except TypeError:
78+
logging_file = output_dir.joinpath(logging_file)
79+
else:
80+
logging_file = None
81+
82+
if logging_file is not None:
83+
LoggingHeader(
84+
logging_file,
85+
package,
86+
variables,
87+
output_dir,
88+
write_header=write_header,
89+
write_git=write_git,
90+
write_cli_args=write_cli_args,
91+
write_variables=write_variables,
92+
log_header=log_header,
93+
)
8694

8795
setup_logging(
8896
logging_file,
@@ -94,7 +102,6 @@ def start_logging(
94102

95103

96104
class LoggingHeader:
97-
98105
def __init__(
99106
self,
100107
file,
@@ -239,11 +246,12 @@ def setup_logging(
239246
" - %(message)s"
240247
)
241248
formatter.datefmt = "%Y-%m-%d %H:%M:%S %p"
242-
fh = logging.FileHandler(filename)
243249

244-
fh.setLevel(getattr(logging, file_level))
245-
fh.setFormatter(formatter)
246-
logger.addHandler(fh)
250+
if filename is not None:
251+
fh = logging.FileHandler(filename)
252+
fh.setLevel(getattr(logging, file_level))
253+
fh.setFormatter(formatter)
254+
logger.addHandler(fh)
247255

248256
ch = logging.StreamHandler()
249257
ch.setLevel(getattr(logging, print_level))

tests/tests/test_DUMMY.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
def test_import():
2-
import fancylog
2+
import fancylog

0 commit comments

Comments
 (0)