Skip to content

Commit a2abf84

Browse files
authored
Update to v0.2.1
FileSteam rotate bug fixed.
2 parents 3c4845a + 43621b5 commit a2abf84

3 files changed

Lines changed: 41 additions & 43 deletions

File tree

fsLogger/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.2.0"
1+
__version__ = "0.2.1"
22
__doc__ = """
33
Logging utility v{}
44
Copyright (C) 2021 Fusion Solutions KFT <contact@fusionsolutions.io>

fsLogger/modules.py

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -74,30 +74,30 @@ class FileStream(T_ModuleBase):
7474
fullPath:str
7575
stream:Any
7676
lock:Lock
77+
isClosed:bool
7778
def __init__(self, fullPath:str):
7879
self.fullPath = fullPath
79-
self.stream = None
8080
self.lock = Lock()
81-
self.open()
81+
self.stream = None
82+
self.isClosed = False
8283
def open(self) -> None:
84+
os.makedirs( os.path.dirname(self.fullPath), 0o755 , True)
85+
self.stream = open(self.fullPath, "at")
86+
def write(self, data:str) -> None:
87+
if self.isClosed:
88+
return
8389
try:
84-
os.makedirs( os.path.dirname(self.fullPath), 0o755 , True)
85-
except:
86-
traceback.print_exc()
87-
try:
88-
self.stream = open(self.fullPath, "at")
90+
if self.stream is None:
91+
self.open()
92+
if self.stream is not None:
93+
self.stream.write(data)
94+
self.stream.flush()
8995
except:
9096
traceback.print_exc()
91-
def write(self, data:str) -> None:
92-
if self.stream is not None:
93-
with self.lock:
94-
try:
95-
self.stream.write(data)
96-
self.stream.flush()
97-
except:
98-
traceback.print_exc()
97+
self.isClosed = True
9998
def emit(self, message:str) -> None:
100-
self.write(message)
99+
with self.lock:
100+
self.write(message)
101101
def close(self) -> None:
102102
if self.stream is not None:
103103
self.stream.close()
@@ -109,20 +109,21 @@ class RotatedFileStream(FileStream):
109109
maxBackup:Optional[int]
110110
lastRotate:Optional[int]
111111
lastFileSize:Optional[int]
112+
timezone:Optional[timezone]
112113
def __init__(self, fullPath:str, maxBytes:int=0, rotateDaily:bool=False, maxBackup:Optional[int]=None,
113114
useUTCTimezone:bool=True):
115+
self.maxBytes = maxBytes
116+
self.rotateDaily = rotateDaily
117+
self.maxBackup = maxBackup
118+
self.lastRotate = None
119+
self.lastFileSize = None
120+
self.timezone = timezone.utc if useUTCTimezone else None
114121
super().__init__(fullPath)
115-
self.maxBytes = maxBytes
116-
self.rotateDaily = rotateDaily
117-
self.maxBackup = maxBackup
118-
self.lastRotate = None
119-
self.lastFileSize = None
120-
self.timezone:Optional[timezone] = timezone.utc if useUTCTimezone else None
121122
def emit(self, message:str) -> None:
122-
if self.stream is not None:
123+
with self.lock:
123124
if self.shouldRotate(message):
124125
self.doRotate()
125-
super().emit(message)
126+
self.write(message)
126127
def shouldRotate(self, message:str) -> bool:
127128
if self.lastRotate is None:
128129
self.lastRotate = datetime.now(self.timezone).day
@@ -147,24 +148,20 @@ def doRotate(self) -> None:
147148
self.shiftLogFiles()
148149
except:
149150
traceback.print_exc()
151+
self.stream = None
152+
self.isClosed = True
150153
self.open()
151154
def shiftLogFiles(self) -> None:
152155
def sortFileNums(e:str) -> Tuple[int, str]:
153-
r = re.findall(r'^.*[^\.]\.([0-9]*)$', e)
156+
r = re.findall(r'^.*\.([0-9]{3})$', e)
154157
if r:
155158
return int(r[0]), e
156159
else:
157160
return 0, e
158-
if not os.path.isdir(os.path.dirname(self.fullPath)):
159-
try:
160-
os.mkdir(os.path.dirname(self.fullPath), 0o770)
161-
except FileExistsError:
162-
pass
163-
if not os.path.isdir(os.path.dirname(self.fullPath)):
164-
return
161+
os.makedirs( os.path.dirname(self.fullPath), 0o755 , True)
165162
files:List[Tuple[int, str]] = sorted(list(map(sortFileNums, glob(self.fullPath+"*"))), key=lambda x: x[0], reverse=True)
166163
for n, f in files:
167-
if self.maxBackup is not None and self.maxBackup <= n+1:
164+
if self.maxBackup is not None and self.maxBackup < n+1:
168165
os.remove(f)
169166
else:
170167
os.rename(f, "{}.{:>03}".format(self.fullPath, n+1))
@@ -175,13 +172,14 @@ class DailyFileStream(FileStream):
175172
postfix:str
176173
dateFormat:str
177174
lastRotate:Optional[int]
175+
timezone:Optional[timezone]
178176
def __init__(self, path:str, prefix:str="", postfix:str="", dateFormat:str="%Y-%m-%d", useUTCTimezone:bool=True):
179-
self.path = path
180-
self.prefix = prefix
181-
self.postfix = postfix
182-
self.dateFormat = dateFormat
183-
self.lastRotate = None
184-
self.timezone:Optional[timezone] = timezone.utc if useUTCTimezone else None
177+
self.path = path
178+
self.prefix = prefix
179+
self.postfix = postfix
180+
self.dateFormat = dateFormat
181+
self.lastRotate = None
182+
self.timezone = timezone.utc if useUTCTimezone else None
185183
super().__init__(self.buildPath())
186184
def buildPath(self) -> str:
187185
return os.path.join(
@@ -193,10 +191,10 @@ def buildPath(self) -> str:
193191
)
194192
)
195193
def emit(self, message:str) -> None:
196-
if self.stream is not None:
194+
with self.lock:
197195
if self.shouldRotate(message):
198196
self.doRotate()
199-
super().emit(message)
197+
self.write(message)
200198
def shouldRotate(self, message:str) -> bool:
201199
if self.lastRotate is None or self.lastRotate != datetime.now(self.timezone).day:
202200
self.lastRotate = datetime.now(self.timezone).day

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setup(
1111
name = "python-fslogger",
12-
version = "0.2.0",
12+
version = "0.2.1",
1313
description = "Logging utility",
1414
keywords = "logging utility fusion solutions fusionsolutions",
1515
author = "Andor `iFA` Rajci - Fusions Solutions KFT",

0 commit comments

Comments
 (0)