| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import typing as t |
| 4 |
|
| 5 |
try: |
| 6 |
import syslog |
| 7 |
|
| 8 |
HAS_SYSLOG = True |
| 9 |
except ImportError: |
| 10 |
HAS_SYSLOG = False |
| 11 |
|
| 12 |
try: |
| 13 |
from systemd import journal, daemon as systemd_daemon |
| 14 |
|
| 15 |
has_journal = hasattr(journal, "sendv") and systemd_daemon.booted() |
| 16 |
except (ImportError, AttributeError): |
| 17 |
has_journal = False |
| 18 |
|
| 19 |
|
| 20 |
def _log_to_syslog(msg: str, module_name: str, syslog_facility: str) -> None: |
| 21 |
if HAS_SYSLOG: |
| 22 |
module = "ansible-%s" % module_name |
| 23 |
facility = getattr(syslog, syslog_facility, syslog.LOG_USER) |
| 24 |
syslog.openlog(str(module), 0, facility) |
| 25 |
syslog.syslog(syslog.LOG_INFO, msg) |
| 26 |
|
| 27 |
|
| 28 |
def log_to_system( |
| 29 |
msg: str, |
| 30 |
*, |
| 31 |
module_name: str, |
| 32 |
log_args: dict[str, t.Any] | None = None, |
| 33 |
syslog_facility: str = "LOG_USER", |
| 34 |
target_log_info: str | None = None, |
| 35 |
) -> None: |
| 36 |
"""Log a message to the system logging service (systemd journal or syslog). |
| 37 |
|
| 38 |
Dispatches to systemd journal when available, falling back to syslog. |
| 39 |
The caller is responsible for sanitizing secrets from *msg* before calling. |
| 40 |
|
| 41 |
The syslog identifier is built as ``ansible-<module_name>``. |
| 42 |
When *target_log_info* is provided (typically remote host information), it is prepended to the message. |
| 43 |
Extra key/value pairs in *log_args* are included as structured journal fields. |
| 44 |
The *syslog_facility* should be a syslog facility name such as ``LOG_USER``. |
| 45 |
|
| 46 |
Raises TypeError or ValueError if the underlying syslog call fails due to invalid input. |
| 47 |
""" |
| 48 |
if log_args is None: |
| 49 |
log_args = {} |
| 50 |
|
| 51 |
module = "ansible-%s" % module_name |
| 52 |
|
| 53 |
if target_log_info: |
| 54 |
msg = " ".join([target_log_info, msg]) |
| 55 |
|
| 56 |
if has_journal: |
| 57 |
journal_args = [("MODULE", module_name)] |
| 58 |
for arg in log_args: |
| 59 |
name, value = (arg.upper(), str(log_args[arg])) |
| 60 |
if name in ( |
| 61 |
"PRIORITY", |
| 62 |
"MESSAGE", |
| 63 |
"MESSAGE_ID", |
| 64 |
"CODE_FILE", |
| 65 |
"CODE_LINE", |
| 66 |
"CODE_FUNC", |
| 67 |
"SYSLOG_FACILITY", |
| 68 |
"SYSLOG_IDENTIFIER", |
| 69 |
"SYSLOG_PID", |
| 70 |
): |
| 71 |
name = "_%s" % name |
| 72 |
journal_args.append((name, value)) |
| 73 |
|
| 74 |
try: |
| 75 |
if HAS_SYSLOG: |
| 76 |
facility = getattr(syslog, syslog_facility, syslog.LOG_USER) >> 3 |
| 77 |
journal.send( |
| 78 |
MESSAGE="%s %s" % (module, msg), |
| 79 |
SYSLOG_FACILITY=facility, |
| 80 |
**dict(journal_args), |
| 81 |
) |
| 82 |
else: |
| 83 |
journal.send(MESSAGE="%s %s" % (module, msg), **dict(journal_args)) |
| 84 |
except OSError: |
| 85 |
_log_to_syslog(msg, module_name, syslog_facility) |
| 86 |
else: |
| 87 |
_log_to_syslog(msg, module_name, syslog_facility) |