From 081a2fe93720be9e3582c62f1c04a06870fca27d Mon Sep 17 00:00:00 2001 From: Tim Hawes Date: Sat, 20 Jul 2024 11:02:38 +0100 Subject: [PATCH] ldapsync: use logging framework --- ldapsync/receivers.py | 45 ++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/ldapsync/receivers.py b/ldapsync/receivers.py index a979de8..11f8f34 100644 --- a/ldapsync/receivers.py +++ b/ldapsync/receivers.py @@ -1,7 +1,9 @@ -# SPDX-FileCopyrightText: 2022 Tim Hawes +# SPDX-FileCopyrightText: 2022-2024 Tim Hawes # # SPDX-License-Identifier: MIT +import logging + from django.conf import settings from django.contrib.auth import get_user_model from django.contrib.auth.models import Group @@ -13,69 +15,72 @@ from .utils import sync_group, sync_user +logger = logging.getLogger(__name__) + + @receiver(post_save, sender=get_user_model()) def sync_ldap_user(sender, instance, update_fields=[], **kwargs): if update_fields and list(update_fields) == ["last_login"]: # skip update if last_login was the only change return try: - print(f"ldapsync user {instance}") + logger.info(f"ldapsync user {instance}") sync_user(instance) - except Exception as e: - print(f"Exception in ldapsync of {instance}: {e}") + except Exception: + logger.exception(f"Exception in ldapsync of {instance}") @receiver(post_save, sender=PosixUser) def sync_ldap_posix_user(sender, instance, **kwargs): try: - print(f"ldapsync user {instance.user} (for PosixUser)") + logger.info(f"ldapsync user {instance.user} (for PosixUser)") sync_user(instance.user) - except Exception as e: - print(f"Exception in ldapsync of {instance}: {e}") + except Exception: + logger.exception(f"Exception in ldapsync of {instance}") @receiver(post_save, sender=SSHKey) def sync_ldap_posix_sshkey(sender, instance, **kwargs): try: - print(f"ldapsync user {instance.user} (for SSHKey)") + logger.info(f"ldapsync user {instance.user} (for SSHKey)") sync_user(instance.user) - except Exception as e: - print(f"Exception in ldapsync of {instance}: {e}") + except Exception: + logger.exception(f"Exception in ldapsync of {instance}") @receiver(post_save, sender=Group) def sync_ldap_group(sender, instance, **kwargs): try: - print(f"ldapsync group {instance}") + logger.info(f"ldapsync group {instance}") sync_group(instance) - except Exception as e: - print(f"Exception in ldapsync of {instance}: {e}") + except Exception: + logger.exception(f"Exception in ldapsync of {instance}") @receiver(post_save, sender=PosixGroup) def sync_ldap_posix_group(sender, instance, **kwargs): try: - print(f"ldapsync user {instance.group} (for PosixGroup)") + logger.info(f"ldapsync user {instance.group} (for PosixGroup)") sync_group(instance.group) - except Exception as e: - print(f"Exception in ldapsync of {instance}: {e}") + except Exception: + logger.exception(f"Exception in ldapsync of {instance}") @receiver(m2m_changed, sender=Group.user_set.through) def sync_ldap_group_m2m(sender, instance, *, action=None, **kwargs): if action in ["post_add", "post_remove"]: try: - print(f"ldapsync group {instance} (for membership)") + logger.info(f"ldapsync group {instance} (for membership)") sync_group(instance) except Exception as e: - print(f"Exception in ldapsync of {instance}: {e}") + logger.exception(f"Exception in ldapsync of {instance}") # @receiver(post_save) # def test_post_save(sender, instance, **kwargs): -# print(f"POST_SAVE sender={sender} instance={instance} kwargs={kwargs}") +# logger.debug(f"POST_SAVE sender={sender} instance={instance} kwargs={kwargs}") # @receiver(m2m_changed) # def test_m2m_changed(sender, instance, **kwargs): -# print(f"M2M_CHANGED sender={sender} instance={instance} kwargs={kwargs}") +# logger.debug(f"M2M_CHANGED sender={sender} instance={instance} kwargs={kwargs}")