1- """
2- fancylog
1+ """fancylog
32===============
43
54Wrapper around the standard logging module, with additional information.
65
76"""
87
8+ import contextlib
99import logging
1010import os
1111import sys
@@ -38,8 +38,7 @@ def start_logging(
3838 log_to_console = True ,
3939 timestamp = True ,
4040):
41- """
42- Prepares the log file, and then begins logging.
41+ """Prepares the log file, and then begins logging.
4342
4443 :param output_dir: Directory to save the log file
4544 :param package: What python package are we logging?
@@ -64,12 +63,8 @@ def start_logging(
6463 :param log_to_console: Print logs to the console or not: Default: True
6564 :return: Path to the logging file#
6665 """
67-
6866 output_dir = str (output_dir )
69- if verbose :
70- print_log_level = "DEBUG"
71- else :
72- print_log_level = "INFO"
67+ print_log_level = "DEBUG" if verbose else "INFO"
7368
7469 if log_to_file :
7570 if filename is None :
@@ -120,18 +115,17 @@ def __init__(
120115 write_variables = True ,
121116 log_header = None ,
122117 ):
123- self .file = open (file , "w" , encoding = "utf-8" )
124118 self .program = program
125- if write_header :
126- self .write_log_header (output_dir , log_header )
127- if write_git :
128- self .write_git_info (self .program .__name__ )
129- if write_cli_args :
130- self .write_command_line_arguments ()
131- if write_variables and variable_objects :
132- self .write_variables (variable_objects )
133119
134- self .file .close ()
120+ with open (file , "w" , encoding = "utf-8" ) as self .file :
121+ if write_header :
122+ self .write_log_header (output_dir , log_header )
123+ if write_git :
124+ self .write_git_info (self .program .__name__ )
125+ if write_cli_args :
126+ self .write_command_line_arguments ()
127+ if write_variables and variable_objects :
128+ self .write_variables (variable_objects )
135129
136130 def write_git_info (self , program_name , header = "GIT INFO" ):
137131 self .write_separated_section_header (header )
@@ -142,18 +136,14 @@ def write_git_info(self, program_name, header="GIT INFO"):
142136 program_path = os .path .split (program_path )[0 ]
143137 git_info = get_git_info (program_path )
144138
145- self .file .write ("Commit hash: {} \n " .format (git_info .head .hash ))
139+ self .file .write (f"Commit hash: { git_info .head .hash } \n " )
140+ self .file .write (f"Commit message: { git_info .head .message } \n " )
141+ self .file .write (f"Commit date & time: { git_info .head .datetime } \n " )
146142 self .file .write (
147- "Commit message : {} \n " . format ( git_info . head . message )
143+ f "Commit author : { git_info . head . committer_name } \n "
148144 )
149145 self .file .write (
150- "Commit date & time: {} \n " .format (git_info .head .datetime )
151- )
152- self .file .write (
153- "Commit author: {} \n " .format (git_info .head .committer_name )
154- )
155- self .file .write (
156- "Commit author email: {}" .format (git_info .head .committer_email )
146+ f"Commit author email: { git_info .head .committer_email } "
157147 )
158148
159149 except GitPythonError :
@@ -202,10 +192,8 @@ def write_log_header(self, output_dir, log_header):
202192 )
203193 self .file .write ("Output directory: " + output_dir + "\n " )
204194 self .file .write ("Current directory: " + os .getcwd () + "\n " )
205- try :
195+ with contextlib . suppress ( AttributeError ) :
206196 self .file .write (f"Version: { self .program .__version__ } " )
207- except AttributeError :
208- pass
209197
210198 def write_separated_section_header (
211199 self ,
@@ -238,8 +226,7 @@ def initialise_logger(
238226 file_level = "DEBUG" ,
239227 log_to_console = True ,
240228):
241- """
242- Sets up (possibly multiprocessing aware) logging.
229+ """Sets up (possibly multiprocessing aware) logging.
243230 :param filename: Where to save the logs to
244231 :param print_level: What level of logging to print to console.
245232 Default: 'INFO'
@@ -279,8 +266,7 @@ def setup_logging(
279266 multiprocessing_aware = True ,
280267 log_to_console = True ,
281268):
282- """
283- Sets up (possibly multiprocessing aware) logging.
269+ """Sets up (possibly multiprocessing aware) logging.
284270 :param filename: Where to save the logs to
285271 :param print_level: What level of logging to print to console.
286272 Default: 'INFO'
@@ -291,7 +277,6 @@ def setup_logging(
291277 Default: True
292278
293279 """
294-
295280 initialise_logger (
296281 filename ,
297282 print_level = print_level ,
@@ -320,8 +305,7 @@ def setup_logging(
320305
321306
322307def disable_logging ():
323- """
324- Prevents any more logging. Saves remembering that logging.disable() with
308+ """Prevents any more logging. Saves remembering that logging.disable() with
325309 no argument doesn't work.
326310 :return:
327311 """
0 commit comments