Skip to content

All special characters in jobs/pods logs#177

Open
PeekLeon wants to merge 1 commit intorundeck-plugins:masterfrom
PeekLeon:special-chars
Open

All special characters in jobs/pods logs#177
PeekLeon wants to merge 1 commit intorundeck-plugins:masterfrom
PeekLeon:special-chars

Conversation

@PeekLeon
Copy link
Copy Markdown

Hi,

I have an issue with special characters disappearing from Kubernetes logs.

I noticed issue #122, which removes all non-ASCII characters, but this also filters out characters like é, è, à, etc., which causes my French messages to be truncated.

I would like to replace:

log.info(line.encode('ascii', 'ignore'))

with:

try:
    log.info(line)
except:
    log.info(line.encode('ascii', 'ignore'))

I added the try/except as a fallback, but I believe it may not be necessary since log.info(line) already works fine.

Would this change be acceptable?

Here are some screenshots

logs output :
all-chars

Job :
job

Wait-for :
wait-for

Characters used : ✅ ! # $ % & ' ( )éèàùüêûîô * + , - . / ; < = > ? @ [ \ ] ^ _ ` { | } ~µ🧙‍

Thanks!

@fdevans fdevans requested a review from Copilot August 26, 2025 18:47
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves the handling of special characters in Kubernetes pod and job logs by preserving non-ASCII characters like French accented letters (é, è, à, etc.) that were previously being stripped out. The change addresses an issue where the existing ASCII-only encoding was too restrictive and caused message truncation for internationalized content.

  • Replaces direct ASCII encoding with a try-catch approach to preserve Unicode characters
  • Adds fallback to ASCII encoding only when the primary logging fails
  • Applies the fix consistently across both pod log reading and job waiting functionality

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
contents/pods-read-logs.py Updates log output handling to preserve Unicode characters with ASCII fallback
contents/job-wait.py Applies the same Unicode-preserving log handling for job monitoring
Comments suppressed due to low confidence (2)

contents/pods-read-logs.py:38

  • The original code used print() but the new code uses log.info(). This changes the output destination and may break existing functionality that depends on stdout output.
                    try:

contents/pods-read-logs.py:50

  • The original code used print() but the new code uses log.info(). This changes the output destination and may break existing functionality that depends on stdout output.
                    try:

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

print(line.encode('ascii', 'ignore'))
try:
log.info(line)
except:
Copy link

Copilot AI Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a bare except clause is not recommended as it catches all exceptions, including system exits and keyboard interrupts. Consider catching specific exceptions like UnicodeError or UnicodeEncodeError instead.

Suggested change
except:
except UnicodeError:

Copilot uses AI. Check for mistakes.
print(line.encode('ascii', 'ignore'))
try:
log.info(line)
except:
Copy link

Copilot AI Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a bare except clause is not recommended as it catches all exceptions, including system exits and keyboard interrupts. Consider catching specific exceptions like UnicodeError or UnicodeEncodeError instead.

Suggested change
except:
except UnicodeError:

Copilot uses AI. Check for mistakes.
Comment thread contents/job-wait.py
log.info(line.encode('ascii', 'ignore'))
try:
log.info(line)
except:
Copy link

Copilot AI Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a bare except clause is not recommended as it catches all exceptions, including system exits and keyboard interrupts. Consider catching specific exceptions like UnicodeError or UnicodeEncodeError instead.

Suggested change
except:
except UnicodeEncodeError:

Copilot uses AI. Check for mistakes.
@fdevans
Copy link
Copy Markdown
Contributor

fdevans commented Mar 6, 2026

Thank you for addressing this! You're absolutely right that the current encode('ascii', 'ignore') approach is too aggressive and strips valid international characters like French accents.

However, we'd like to suggest some improvements to your implementation:

1. Specific exception handling

Instead of a bare except:, catch the specific exception:

try:
    log.info(line)
except UnicodeEncodeError:
    log.info(line.encode('ascii', 'ignore').decode('ascii'))

2. Consistency in pods-read-logs.py

Your change switches from print() to log.info() in pods-read-logs.py. This changes the output behavior. Please keep using print() for consistency with the rest of that file.

3. Consider UTF-8 encoding

Instead of falling back to ASCII, consider UTF-8 with error handling:

try:
    log.info(line)
except UnicodeEncodeError:
    log.info(line.encode('utf-8', 'replace').decode('utf-8'))

This would preserve more characters while still handling problematic ones gracefully.

4. Root cause consideration

The original issue (#122) was a UnicodeEncodeError when printing. Modern Python 3 should handle UTF-8 by default. The real fix might be ensuring proper encoding configuration in the logging setup rather than encoding each line.

Could you update the PR to:

  1. Use specific exception handling (UnicodeEncodeError)
  2. Keep print() in pods-read-logs.py instead of switching to log.info()
  3. Consider UTF-8 encoding instead of ASCII as the fallback
  4. Rebase on the latest master branch

Thanks for helping improve international character support!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants