75
75
# Random numbers for rotation temp file names, using secrets module if available (Python 3.6).
76
76
# Otherwise use `random.SystemRandom` if available, then fall back on `random.Random`.
77
77
try :
78
- # noinspection PyPackageRequirements
78
+ # noinspection PyPackageRequirements,PyCompatibility
79
79
from secrets import randbits
80
80
except ImportError :
81
81
import random
@@ -185,12 +185,15 @@ def __init__(self, filename, mode='a', maxBytes=0, backupCount=0,
185
185
self ._set_uid = pwd .getpwnam (self .owner [0 ]).pw_uid
186
186
self ._set_gid = grp .getgrnam (self .owner [1 ]).gr_gid
187
187
188
+ self .lockFilename = self .getLockFilename ()
188
189
189
- def _open_lockfile (self ):
190
- if self .stream_lock and not self .stream_lock .closed :
191
- self ._console_log ("Lockfile already open in this process" )
192
- return
193
- # Use 'file.lock' and not 'file.log.lock' (Only handles the normal "*.log" case.)
190
+ def getLockFilename (self ):
191
+ """
192
+ Decide the lock filename. If the logfile is file.log, then we use `.__file.lock` and
193
+ not `file.log.lock`. This only removes the extension if it's `*.log`.
194
+
195
+ :return: the path to the lock file.
196
+ """
194
197
if self .baseFilename .endswith (".log" ):
195
198
lock_file = self .baseFilename [:- 4 ]
196
199
else :
@@ -199,7 +202,13 @@ def _open_lockfile(self):
199
202
lock_path , lock_name = os .path .split (lock_file )
200
203
# hide the file on Unix and generally from file completion
201
204
lock_name = ".__" + lock_name
202
- lock_file = os .path .join (lock_path , lock_name )
205
+ return os .path .join (lock_path , lock_name )
206
+
207
+ def _open_lockfile (self ):
208
+ if self .stream_lock and not self .stream_lock .closed :
209
+ self ._console_log ("Lockfile already open in this process" )
210
+ return
211
+ lock_file = self .lockFilename
203
212
self ._console_log (
204
213
"concurrent-log-handler %s opening %s" % (hash (self ), lock_file ), stack = False )
205
214
self .stream_lock = open (lock_file , "wb" , buffering = 0 )
@@ -266,8 +275,6 @@ def emit(self, record):
266
275
try :
267
276
msg = self .format (record )
268
277
try :
269
- self ._open_lockfile ()
270
-
271
278
self ._do_lock ()
272
279
273
280
try :
@@ -281,12 +288,6 @@ def emit(self, record):
281
288
282
289
finally :
283
290
self ._do_unlock ()
284
- if self .stream_lock :
285
- unlock (self .stream_lock )
286
- self .stream_lock .close ()
287
- self .stream_lock = None
288
-
289
-
290
291
except Exception :
291
292
self .handleError (record )
292
293
@@ -307,6 +308,7 @@ def do_write(self, msg):
307
308
return
308
309
309
310
def _do_lock (self ):
311
+ self ._open_lockfile ()
310
312
if self .stream_lock :
311
313
lock (self .stream_lock , LOCK_EX )
312
314
else :
@@ -315,6 +317,8 @@ def _do_lock(self):
315
317
def _do_unlock (self ):
316
318
if self .stream_lock :
317
319
unlock (self .stream_lock )
320
+ self .stream_lock .close ()
321
+ self .stream_lock = None
318
322
else :
319
323
self ._console_log ("No self.stream_lock to unlock" , stack = True )
320
324
0 commit comments