-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-encode log messages to make sure we will not fail on writing logs
We need to be able to write poorly encoded messages in the logs, so that we can easily identify the issue Unfortiantly there is no encoding parameter for the FileHandler in python 3.6, so we have to re-encode the message manually.
- Loading branch information
Mikhail Sandakov
committed
May 27, 2024
1 parent
4983bce
commit 2dcb2c3
Showing
2 changed files
with
69 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Copyright 2023-2024. WebPros International GmbH. All rights reserved. | ||
import locale | ||
import os | ||
import unittest | ||
|
||
import src.log as log | ||
|
||
|
||
class TestLog(unittest.TestCase): | ||
DEFAULT_LOG_STORE = "test.log" | ||
|
||
def setUp(self) -> None: | ||
log.logger.init_logger([self.DEFAULT_LOG_STORE], [], console=False) | ||
|
||
def tearDown(self) -> None: | ||
if os.path.exists(self.DEFAULT_LOG_STORE): | ||
os.unlink(self.DEFAULT_LOG_STORE) | ||
|
||
def test_error_log(self): | ||
log.err("Test message") | ||
self.assertTrue(os.path.exists(self.DEFAULT_LOG_STORE)) | ||
with open(self.DEFAULT_LOG_STORE, "r") as log_file: | ||
self.assertTrue("Test message" in log_file.read()) | ||
|
||
def test_no_debug_by_default(self): | ||
log.debug("Test message") | ||
self.assertTrue(os.path.exists(self.DEFAULT_LOG_STORE)) | ||
with open(self.DEFAULT_LOG_STORE, "r") as log_file: | ||
self.assertEqual("", log_file.read()) | ||
|
||
def test_debug_log(self): | ||
log.reinit_logger([self.DEFAULT_LOG_STORE], [], console=False, loglevel=log.logging.DEBUG) | ||
log.debug("Test message") | ||
self.assertTrue(os.path.exists(self.DEFAULT_LOG_STORE)) | ||
with open(self.DEFAULT_LOG_STORE, "r") as log_file: | ||
self.assertTrue("Test message" in log_file.read()) | ||
|
||
def test_write_bad_encoded(self): | ||
# To trigger an exception, we need to set the locale to a non-utf8 one | ||
# but we can't be sure what locale will be installed on the test machine | ||
# So it looks like it is enough to make sure message was re-decoded before | ||
# writing to the log file by checking logfile content. | ||
log.reinit_logger([self.DEFAULT_LOG_STORE], [], console=False, encoding='ascii') | ||
log.err("Test message ü") | ||
self.assertTrue(os.path.exists(self.DEFAULT_LOG_STORE)) | ||
with open(self.DEFAULT_LOG_STORE, "r") as log_file: | ||
self.assertTrue("Test message \\xfc" in log_file.read()) |