Skip to content

Commit

Permalink
0.4.20 logger encoding and keep file open
Browse files Browse the repository at this point in the history
  • Loading branch information
sinri committed Oct 14, 2021
1 parent ad49459 commit 6fda496
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
41 changes: 35 additions & 6 deletions nehushtan/logger/NehushtanFileLogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(
date_rotate: bool = True,
print_higher_than_this_level=NehushtanLogging.CRITICAL,
record_millisecond=False,
file_encoding='utf-8'
):
self.categorize = categorize
self.log_dir = log_dir
Expand Down Expand Up @@ -60,32 +61,58 @@ def __init__(
else:
self.print_higher_than_this_level = print_higher_than_this_level

# Since 0.4.20 CACHED FILE HANDLER
self.keep_file_open = True
self.opened_files = {}
# Since 0.4.20 TARGET FILE ENCODING
self.file_encoding = file_encoding

def __del__(self):
if len(self.opened_files.items()) > 0:
for name, file in self.opened_files.items():
file.close()
# print(name, 'closed')

def get_target_file(self):
if self.log_dir is None:
return ''

category_dir = self.log_dir

# TODO
# a -> a/a-DATE.log
# a/b -> a/b-DATE.log
# a/b/c -> a/b/c-DATE.log

if self.categorize:
category_dir = os.path.join(self.log_dir, self.title)

if not os.path.exists(category_dir):
os.makedirs(category_dir)

today = ''
if self.date_rotate:
today = time.strftime("%Y%m%d", time.localtime())
today = f'-{today}'

target_file = os.path.join(category_dir, f'{self.title}{today}.log')

final_dir = os.path.dirname(target_file)
if not os.path.exists(final_dir):
os.makedirs(final_dir)

return target_file

def get_target_file_hander(self, target_file_path: str):
if not target_file_path:
return None

if self.keep_file_open:
file = self.opened_files.get(target_file_path)
if not file:
file = open(target_file_path, 'a', encoding=self.file_encoding)
self.opened_files[target_file_path] = file
else:
file = open(target_file_path, 'a', encoding=self.file_encoding)

return file

def write_raw_line_to_log(self, text: str, level: int = NehushtanLogging.INFO, end=os.linesep):
"""
Parameter `level` is only used to determine stdout or stderr when file empty.
Expand All @@ -94,10 +121,12 @@ def write_raw_line_to_log(self, text: str, level: int = NehushtanLogging.INFO, e
target_file = self.get_target_file()

if target_file != '':
file = open(target_file, 'a',encoding='utf-8')
file = self.get_target_file_hander(target_file)
file.write(text + end)
file.flush()
file.close()

if not self.keep_file_open:
file.close()

if target_file == '' or level > self.print_higher_than_this_level:
if level >= NehushtanLogging.WARNING:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='nehushtan',
version='0.4.19',
version='0.4.20',
packages=find_packages(),
url='https://sinri.github.io/nehushtan/',
license='MIT',
Expand Down

0 comments on commit 6fda496

Please sign in to comment.