aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Twisted/py2/twisted/logger/_file.py
diff options
context:
space:
mode:
authorshmel1k <shmel1k@ydb.tech>2023-11-26 18:16:14 +0300
committershmel1k <shmel1k@ydb.tech>2023-11-26 18:43:30 +0300
commitb8cf9e88f4c5c64d9406af533d8948deb050d695 (patch)
tree218eb61fb3c3b96ec08b4d8cdfef383104a87d63 /contrib/python/Twisted/py2/twisted/logger/_file.py
parent523f645a83a0ec97a0332dbc3863bb354c92a328 (diff)
downloadydb-b8cf9e88f4c5c64d9406af533d8948deb050d695.tar.gz
add kikimr_configure
Diffstat (limited to 'contrib/python/Twisted/py2/twisted/logger/_file.py')
-rw-r--r--contrib/python/Twisted/py2/twisted/logger/_file.py86
1 files changed, 86 insertions, 0 deletions
diff --git a/contrib/python/Twisted/py2/twisted/logger/_file.py b/contrib/python/Twisted/py2/twisted/logger/_file.py
new file mode 100644
index 0000000000..4913070ef2
--- /dev/null
+++ b/contrib/python/Twisted/py2/twisted/logger/_file.py
@@ -0,0 +1,86 @@
+# -*- test-case-name: twisted.logger.test.test_file -*-
+# Copyright (c) Twisted Matrix Laboratories.
+# See LICENSE for details.
+
+"""
+File log observer.
+"""
+
+from zope.interface import implementer
+
+from twisted.python.compat import ioType, unicode
+from ._observer import ILogObserver
+from ._format import formatTime
+from ._format import timeFormatRFC3339
+from ._format import formatEventAsClassicLogText
+
+
+
+@implementer(ILogObserver)
+class FileLogObserver(object):
+ """
+ Log observer that writes to a file-like object.
+ """
+ def __init__(self, outFile, formatEvent):
+ """
+ @param outFile: A file-like object. Ideally one should be passed which
+ accepts L{unicode} data. Otherwise, UTF-8 L{bytes} will be used.
+ @type outFile: L{io.IOBase}
+
+ @param formatEvent: A callable that formats an event.
+ @type formatEvent: L{callable} that takes an C{event} argument and
+ returns a formatted event as L{unicode}.
+ """
+ if ioType(outFile) is not unicode:
+ self._encoding = "utf-8"
+ else:
+ self._encoding = None
+
+ self._outFile = outFile
+ self.formatEvent = formatEvent
+
+
+ def __call__(self, event):
+ """
+ Write event to file.
+
+ @param event: An event.
+ @type event: L{dict}
+ """
+ text = self.formatEvent(event)
+
+ if text is None:
+ text = u""
+
+ if self._encoding is not None:
+ text = text.encode(self._encoding)
+
+ if text:
+ self._outFile.write(text)
+ self._outFile.flush()
+
+
+
+def textFileLogObserver(outFile, timeFormat=timeFormatRFC3339):
+ """
+ Create a L{FileLogObserver} that emits text to a specified (writable)
+ file-like object.
+
+ @param outFile: A file-like object. Ideally one should be passed which
+ accepts L{unicode} data. Otherwise, UTF-8 L{bytes} will be used.
+ @type outFile: L{io.IOBase}
+
+ @param timeFormat: The format to use when adding timestamp prefixes to
+ logged events. If L{None}, or for events with no C{"log_timestamp"}
+ key, the default timestamp prefix of C{u"-"} is used.
+ @type timeFormat: L{unicode} or L{None}
+
+ @return: A file log observer.
+ @rtype: L{FileLogObserver}
+ """
+ def formatEvent(event):
+ return formatEventAsClassicLogText(
+ event, formatTime=lambda e: formatTime(e, timeFormat)
+ )
+
+ return FileLogObserver(outFile, formatEvent)