refactor: replace f-strings with % formatting in logging statements

- Replace f-strings in logging.exception() and logging.warning() calls
- Use % formatting for better performance when log level is disabled
- Follows Python logging best practices
This commit is contained in:
-LAN- 2025-08-19 03:28:41 +08:00
parent 16d30fbd60
commit 25ae492247
2 changed files with 31 additions and 4 deletions

View File

@ -83,7 +83,34 @@ class Mail:
case _:
raise ValueError(f"Unsupported mail type {mail_type}")
def send(self, to: str, subject: str, html: str, from_: str | None = None):
def _get_oauth_token(self) -> Optional[str]:
"""Get OAuth access token using client credentials flow"""
try:
from libs.mail.oauth_email import MicrosoftEmailOAuth
client_id = dify_config.MICROSOFT_OAUTH2_CLIENT_ID
client_secret = dify_config.MICROSOFT_OAUTH2_CLIENT_SECRET
tenant_id = dify_config.MICROSOFT_OAUTH2_TENANT_ID or "common"
if not client_id or not client_secret:
return None
oauth_client = MicrosoftEmailOAuth(
client_id=client_id,
client_secret=client_secret,
redirect_uri="", # Not needed for client credentials flow
tenant_id=tenant_id,
)
token_response = oauth_client.get_access_token_client_credentials()
access_token = token_response.get("access_token")
return str(access_token) if access_token is not None else None
except Exception as e:
logging.warning("Failed to obtain OAuth 2.0 access token: %s", str(e))
return None
def send(self, to: str, subject: str, html: str, from_: Optional[str] = None):
if not self._client:
raise ValueError("Mail client is not initialized")

View File

@ -50,10 +50,10 @@ class SMTPAuthenticator:
try:
connection.docmd("AUTH", f"XOAUTH2 {auth_string}")
except smtplib.SMTPAuthenticationError as e:
logging.exception(f"OAuth2 authentication failed for user {username}")
logging.exception("OAuth2 authentication failed for user %s", username)
raise ValueError(f"OAuth2 authentication failed: {str(e)}")
except Exception:
logging.exception(f"Unexpected error during OAuth2 authentication for user {username}")
logging.exception("Unexpected error during OAuth2 authentication for user %s", username)
raise
@ -153,7 +153,7 @@ class SMTPClient:
logging.exception("Timeout occurred while sending email")
raise
except Exception:
logging.exception(f"Unexpected error occurred while sending email to {mail['to']}")
logging.exception("Unexpected error occurred while sending email to %s", mail['to'])
raise
finally:
if connection: