diff --git a/fluent/sender.py b/fluent/sender.py index 72e8c36..ed15875 100644 --- a/fluent/sender.py +++ b/fluent/sender.py @@ -13,8 +13,7 @@ def _set_global_sender(sender): # pragma: no cover - """ [For testing] Function to set global sender directly - """ + """[For testing] Function to set global sender directly""" global _global_sender _global_sender = sender @@ -35,7 +34,7 @@ def close(): # pragma: no cover class EventTime(msgpack.ExtType): def __new__(cls, timestamp): seconds = int(timestamp) - nanoseconds = int(timestamp % 1 * 10 ** 9) + nanoseconds = int(timestamp % 1 * 10**9) return super(EventTime, cls).__new__( cls, code=0, @@ -44,17 +43,19 @@ def __new__(cls, timestamp): class FluentSender(object): - def __init__(self, - tag, - host='localhost', - port=24224, - bufmax=1 * 1024 * 1024, - timeout=3.0, - verbose=False, - buffer_overflow_handler=None, - nanosecond_precision=False, - msgpack_kwargs=None, - **kwargs): + def __init__( + self, + tag=None, + host="localhost", + port=24224, + bufmax=1 * 1024 * 1024, + timeout=3.0, + verbose=False, + buffer_overflow_handler=None, + nanosecond_precision=False, + msgpack_kwargs=None, + **kwargs + ): """ :param kwargs: This kwargs argument is not used in __init__. This will be removed in the next major version. """ @@ -88,23 +89,28 @@ def emit_with_time(self, label, timestamp, data): bytes_ = self._make_packet(label, timestamp, data) except Exception as e: self.last_error = e - bytes_ = self._make_packet(label, timestamp, - {"level": "CRITICAL", - "message": "Can't output to log", - "traceback": traceback.format_exc()}) + bytes_ = self._make_packet( + label, + timestamp, + { + "level": "CRITICAL", + "message": "Can't output to log", + "traceback": traceback.format_exc(), + }, + ) return self._send(bytes_) @property def last_error(self): - return getattr(self._last_error_threadlocal, 'exception', None) + return getattr(self._last_error_threadlocal, "exception", None) @last_error.setter def last_error(self, err): self._last_error_threadlocal.exception = err def clear_last_error(self, _thread_id=None): - if hasattr(self._last_error_threadlocal, 'exception'): - delattr(self._last_error_threadlocal, 'exception') + if hasattr(self._last_error_threadlocal, "exception"): + delattr(self._last_error_threadlocal, "exception") def close(self): with self.lock: @@ -121,10 +127,14 @@ def close(self): self.pendings = None def _make_packet(self, label, timestamp, data): - if label: - tag = '.'.join((self.tag, label)) - else: + if self.tag and label: + tag = ".".join((self.tag, label)) + elif self.tag: tag = self.tag + elif label: + tag = label + else: + raise ValueError("tag and label are both empty") packet = (tag, timestamp, data) if self.verbose: print(packet) @@ -174,7 +184,7 @@ def _check_recv_side(self): raise return - if recvd == b'': + if recvd == b"": raise socket.error(errno.EPIPE, "Broken pipe") finally: self.socket.settimeout(self.timeout) @@ -196,10 +206,10 @@ def _send_data(self, bytes_): def _reconnect(self): if not self.socket: try: - if self.host.startswith('unix://'): + if self.host.startswith("unix://"): sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.settimeout(self.timeout) - sock.connect(self.host[len('unix://'):]) + sock.connect(self.host[len("unix://") :]) else: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(self.timeout) diff --git a/setup.py b/setup.py index 1453d55..9e5582e 100755 --- a/setup.py +++ b/setup.py @@ -7,37 +7,37 @@ except ImportError: from distutils.core import setup -README = path.abspath(path.join(path.dirname(__file__), 'README.rst')) -desc = 'A Python logging handler for Fluentd event collector' +README = path.abspath(path.join(path.dirname(__file__), "README.rst")) +desc = "A Python logging handler for Fluentd event collector" setup( - name='fluent-logger', - version='0.10.0', - description=desc, - long_description=open(README).read(), - package_dir={'fluent': 'fluent'}, - packages=['fluent'], - install_requires=['msgpack>1.0'], - author='Kazuki Ohta', - author_email='kazuki.ohta@gmail.com', - maintainer='Arcadiy Ivanov', - maintainer_email='arcadiy@ivanov.biz', - url='https://github.com/fluent/fluent-logger-python', - download_url='https://pypi.org/project/fluent-logger/', - license='Apache License, Version 2.0', - classifiers=[ - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Programming Language :: Python :: 3.8', - 'Programming Language :: Python :: 3.9', - 'Programming Language :: Python :: Implementation :: CPython', - 'Programming Language :: Python :: Implementation :: PyPy', - 'Development Status :: 5 - Production/Stable', - 'Topic :: System :: Logging', - 'Intended Audience :: Developers', - ], - python_requires='>=3.5', - test_suite='tests' + name="fluent-logger", + version="0.10.2", + description=desc, + long_description=open(README).read(), + package_dir={"fluent": "fluent"}, + packages=["fluent"], + install_requires=["msgpack>1.0"], + author="Kazuki Ohta", + author_email="kazuki.ohta@gmail.com", + maintainer="Arcadiy Ivanov", + maintainer_email="arcadiy@ivanov.biz", + url="https://github.com/fluent/fluent-logger-python", + download_url="https://pypi.org/project/fluent-logger/", + license="Apache License, Version 2.0", + classifiers=[ + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.5", + "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: Implementation :: CPython", + "Programming Language :: Python :: Implementation :: PyPy", + "Development Status :: 5 - Production/Stable", + "Topic :: System :: Logging", + "Intended Audience :: Developers", + ], + python_requires=">=3.5", + test_suite="tests", )