spacemanspiff2007 / sml2mqtt

Sml to MQTT Bridge
GNU General Public License v3.0
25 stars 8 forks source link

Support logging to stdout (to use journalctl for viewing logs) #34

Closed aaronk6 closed 3 months ago

aaronk6 commented 10 months ago

Could you add support for logging to stdout?

I would prefer to see the logs with journalctl -fu sml2mqtt rather than in a separate log file.

I tried this:

logging:
  level: INFO
  file: /dev/stdout

This works when running the script manually, but not in the systemd context:

Sep 01 22:13:44 stromableser systemd[1]: Started sml2mqtt.
Sep 01 22:13:45 stromableser sml2mqtt[3187]: Traceback (most recent call last):
Sep 01 22:13:45 stromableser sml2mqtt[3187]:   File "/usr/local/lib/python3.9/dist-packages/sml2mqtt/__main__.py", line 57, in main
Sep 01 22:13:45 stromableser sml2mqtt[3187]:     setup_log()
Sep 01 22:13:45 stromableser sml2mqtt[3187]:   File "/usr/local/lib/python3.9/dist-packages/sml2mqtt/__log__.py", line 46, in setup_log
Sep 01 22:13:45 stromableser sml2mqtt[3187]:     handler = MidnightRotatingFileHandler(
Sep 01 22:13:45 stromableser sml2mqtt[3187]:   File "/usr/local/lib/python3.9/dist-packages/sml2mqtt/__log__.py", line 20, in __init__
Sep 01 22:13:45 stromableser sml2mqtt[3187]:     super().__init__(*args, **kwargs)
Sep 01 22:13:45 stromableser sml2mqtt[3187]:   File "/usr/lib/python3.9/logging/handlers.py", line 153, in __init__
Sep 01 22:13:45 stromableser sml2mqtt[3187]:     BaseRotatingHandler.__init__(self, filename, mode, encoding=encoding,
Sep 01 22:13:45 stromableser sml2mqtt[3187]:   File "/usr/lib/python3.9/logging/handlers.py", line 58, in __init__
Sep 01 22:13:45 stromableser sml2mqtt[3187]:     logging.FileHandler.__init__(self, filename, mode=mode,
Sep 01 22:13:45 stromableser sml2mqtt[3187]:   File "/usr/lib/python3.9/logging/__init__.py", line 1142, in __init__
Sep 01 22:13:45 stromableser sml2mqtt[3187]:     StreamHandler.__init__(self, self._open())
Sep 01 22:13:45 stromableser sml2mqtt[3187]:   File "/usr/lib/python3.9/logging/__init__.py", line 1171, in _open
Sep 01 22:13:45 stromableser sml2mqtt[3187]:     return open(self.baseFilename, self.mode, encoding=self.encoding,
Sep 01 22:13:45 stromableser sml2mqtt[3187]: OSError: [Errno 6] No such device or address: '/dev/stdout'
Sep 01 22:13:45 stromableser sml2mqtt[3187]: [Errno 6] No such device or address: '/dev/stdout'

My guess is that it would work when using sys.stdout in the Python script.

Thanks for considering this and thanks for the great work!

spacemanspiff2007 commented 3 months ago

Implemented in the rewrite. If you want to try it out there is #39