Skip to content

Commit 3fa29c1

Browse files
authored
refactor: add journald log formatter for systemd environments (#89)
- Use journald compatible log level prefixes. - Use timestamp prefixes for non-systemd environments.
1 parent 4051a38 commit 3fa29c1

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

CHANGELOG.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
_Changes in the next release_
1111

12+
### Changed
13+
- Use journald log levels if running as a systemd service
14+
1215
---
1316

1417
## v0.20.0 - 2025-12-23
1518
### Changed
16-
- Added connection retry when network is not ready. Contributed by @albaintor, thanks! ([#80](https://github.com/unfoldedcircle/integration-appletv/pull/80).
17-
- Update ucapi to 0.5.1 ([#88](https://github.com/unfoldedcircle/integration-appletv/pull/88).
19+
- Added connection retry when network is not ready. Contributed by @albaintor, thanks! ([#80](https://github.com/unfoldedcircle/integration-appletv/pull/80)).
20+
- Update ucapi to 0.5.1 ([#88](https://github.com/unfoldedcircle/integration-appletv/pull/88)).
1821

1922
## v0.19.3 - 2025-11-27
2023
### Fixed
21-
- Artwork not being updated ([#79](https://github.com/unfoldedcircle/integration-appletv/pull/79).
24+
- Artwork not being updated ([#79](https://github.com/unfoldedcircle/integration-appletv/pull/79)).
2225
### Changed
23-
- If a client disconnects, the connections to the ATV devices are no longer closed ([#78](https://github.com/unfoldedcircle/integration-appletv/pull/78).
26+
- If a client disconnects, the connections to the ATV devices are no longer closed ([#78](https://github.com/unfoldedcircle/integration-appletv/pull/78)).
2427

2528
## v0.19.2 - 2025-11-21
2629
### Fixed

intg-appletv/driver.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,9 +721,36 @@ async def pyatv_patched_system_info(self):
721721
)
722722

723723

724+
class JournaldFormatter(logging.Formatter):
725+
"""Formatter for journald. Prefixes messages with priority level."""
726+
727+
def format(self, record):
728+
"""Format the log record with journald priority prefix."""
729+
# mapping of logging levels to journald priority levels
730+
# https://www.freedesktop.org/software/systemd/man/latest/sd-daemon.html#syslog-compatible-log-levels
731+
priority = {
732+
logging.DEBUG: "<7>",
733+
logging.INFO: "<6>",
734+
logging.WARNING: "<4>",
735+
logging.ERROR: "<3>",
736+
logging.CRITICAL: "<2>",
737+
}.get(record.levelno, "<6>")
738+
return f"{priority}{record.name}: {record.getMessage()}"
739+
740+
724741
async def main():
725-
"""Start the Remote Two integration driver."""
726-
logging.basicConfig()
742+
"""Start the Remote Two/3 integration driver."""
743+
if os.getenv("INVOCATION_ID"):
744+
# when running under systemd: timestamps are added by the journal
745+
# and we use a custom formatter for journald priority levels
746+
handler = logging.StreamHandler()
747+
handler.setFormatter(JournaldFormatter())
748+
logging.basicConfig(handlers=[handler])
749+
else:
750+
logging.basicConfig(
751+
format="%(asctime)s.%(msecs)03d %(levelname)-5s %(name)s.%(funcName)s: %(message)s",
752+
datefmt="%Y-%m-%d %H:%M:%S",
753+
)
727754

728755
level = os.getenv("UC_LOG_LEVEL", "DEBUG").upper()
729756
logging.getLogger("tv").setLevel(level)

0 commit comments

Comments
 (0)