Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add utf-8 encoding to file handlers for logging #1916

Merged
merged 1 commit into from
Apr 16, 2023

Conversation

k-boikov
Copy link
Contributor

Background

Using characters (like “→”) not in cp1252 causes logger to throw error:

UnicodeEncodeError: 'charmap' codec can't encode character '\u2192' in position 48: character maps to <undefined>

image

Changes

Added 'utf-8' parameter to Logger's FileHandlers.

PR Quality Checklist

  • My pull request is atomic and focuses on a single change.
  • I have thoroughly tested my changes with multiple different prompts.
  • I have considered potential risks and mitigations for my changes.
  • I have documented my changes clearly and comprehensively.
  • I have not snuck in any "extra" small tweaks changes

@nponeccop nponeccop added the B7 label Apr 16, 2023
@@ -46,15 +46,19 @@ def __init__(self):
self.console_handler.setFormatter(console_formatter)

# Info handler in activity.log
self.file_handler = logging.FileHandler(os.path.join(log_dir, log_file))
self.file_handler = logging.FileHandler(
os.path.join(log_dir, log_file), 'a', 'utf-8'

Choose a reason for hiding this comment

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

Specifying the mode 'a' is unnecessary, since this is the default. Just adding encoding='utf-8' is enough, and is also more readable.

So the change becomes:

self.file_handler = logging.FileHandler(os.path.join(log_dir, log_file), encoding='utf-8')

self.file_handler.setLevel(logging.DEBUG)
info_formatter = AutoGptFormatter(
"%(asctime)s %(levelname)s %(title)s %(message_no_color)s"
)
self.file_handler.setFormatter(info_formatter)

# Error handler error.log
error_handler = logging.FileHandler(os.path.join(log_dir, error_file))
error_handler = logging.FileHandler(
os.path.join(log_dir, error_file), 'a', 'utf-8'

Choose a reason for hiding this comment

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

Specifying the mode 'a' is unnecessary, since this is the default. Just adding encoding='utf-8' is enough, and is also more readable.

So the change becomes:

error_handler = logging.FileHandler(os.path.join(log_dir, error_file), encoding='utf-8')

@nponeccop nponeccop added the bug Something isn't working label Apr 16, 2023
@p-i- p-i- merged commit 4f33e1b into Significant-Gravitas:master Apr 16, 2023
@nponeccop nponeccop mentioned this pull request Apr 17, 2023
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants