Skip to content

Commit 9cabd0e

Browse files
Fix line endings (to emit CRLF on Windows) when opening with a declared encoding such as 'utf-16'. Also allow customizing line endings if for some weird reason you wanted to emit CRLF on Unix.
This should address issue #19 and PR #20.
1 parent 7251c50 commit 9cabd0e

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/concurrent_log_handler/__init__.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
5353
"""
5454

55+
import io
5556
import os
5657
import sys
5758
import time
@@ -63,11 +64,6 @@
6364

6465
from portalocker import LOCK_EX, lock, unlock
6566

66-
try:
67-
import codecs
68-
except ImportError:
69-
codecs = None
70-
7167
try:
7268
import pwd
7369
import grp
@@ -122,7 +118,7 @@ class ConcurrentRotatingFileHandler(BaseRotatingHandler):
122118

123119
def __init__(self, filename, mode='a', maxBytes=0, backupCount=0,
124120
encoding=None, debug=False, delay=None, use_gzip=False,
125-
owner=None, chmod=None, umask=None):
121+
owner=None, chmod=None, umask=None, newline=None, terminator="\n"):
126122
"""
127123
Open the specified file and use it as the stream for logging.
128124
@@ -140,6 +136,10 @@ def __init__(self, filename, mode='a', maxBytes=0, backupCount=0,
140136
This is an alternative to chmod. It is mainly for Unix systems but
141137
can also be used on Windows. The Windows security model is more complex
142138
and this is not the same as changing access control entries.
139+
:param newline: None (default): use CRLF on Windows, LF on Unix. Set to '' for
140+
no translation, in which case the 'terminator' argument determines the line ending.
141+
:param terminator: set to '\r\n' along with newline='' to force Windows style
142+
newlines regardless of OS platform.
143143
144144
By default, the file grows indefinitely. You can specify particular
145145
values of maxBytes and backupCount to allow the file to rollover at
@@ -177,6 +177,7 @@ def __init__(self, filename, mode='a', maxBytes=0, backupCount=0,
177177
self._rotateFailed = False
178178
self.maxBytes = maxBytes
179179
self.backupCount = backupCount
180+
self.newline = newline
180181

181182
self._debug = debug
182183
self.use_gzip = True if gzip and use_gzip else False
@@ -194,8 +195,7 @@ def __init__(self, filename, mode='a', maxBytes=0, backupCount=0,
194195
super(ConcurrentRotatingFileHandler, self).__init__(
195196
filename, mode, encoding=encoding, delay=True)
196197

197-
if not hasattr(self, "terminator"):
198-
self.terminator = "\n"
198+
self.terminator = terminator or "\n"
199199

200200
if owner and os.chown and pwd and grp:
201201
self._set_uid = pwd.getpwnam(self.owner[0]).pw_uid
@@ -250,10 +250,9 @@ def do_open(self, mode=None):
250250
mode = self.mode
251251

252252
with self._alter_umask():
253-
if self.encoding is None:
254-
stream = open(self.baseFilename, mode)
255-
else:
256-
stream = codecs.open(self.baseFilename, mode, self.encoding)
253+
# noinspection PyArgumentList
254+
stream = io.open(
255+
self.baseFilename, mode=mode, encoding=self.encoding, newline=self.newline)
257256

258257
self._do_chown_and_chmod(self.baseFilename)
259258

0 commit comments

Comments
 (0)