-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Provide logger formatter separately #37
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
64b3e7a
feat: Provide logger formatter separately
AlejandroFernandezLuces f81a8aa
chore: adding changelog file 37.added.md [dependabot-skip]
pyansys-ci-bot 74123b9
fix: Use defaults in singleton logger
AlejandroFernandezLuces 405b1d4
Merge branch 'feat/logger-formatter' of https://github.com/ansys-inte…
AlejandroFernandezLuces eddc065
fix: pre-commit
AlejandroFernandezLuces 163ec72
fix: Separate formatter from logger to avoid loading singleton
AlejandroFernandezLuces de19ae8
fix: Pre-commit
AlejandroFernandezLuces 3838ba5
fix: Pre-commit
AlejandroFernandezLuces 17c2c97
fix: Tests
AlejandroFernandezLuces 708bdf4
fix: Rename class
AlejandroFernandezLuces afd6f25
fix: Use record copy
AlejandroFernandezLuces db95257
fix: Use plain string format
AlejandroFernandezLuces 42fe92b
fix: Remove truncation
AlejandroFernandezLuces File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
| Provide logger formatter separately |
This file contains hidden or 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 hidden or 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,72 @@ | ||
| # Copyright (C) 2025 ANSYS, Inc. and/or its affiliates. | ||
| # SPDX-License-Identifier: MIT | ||
| # | ||
| # | ||
| # Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| # of this software and associated documentation files (the "Software"), to deal | ||
| # in the Software without restriction, including without limitation the rights | ||
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| # copies of the Software, and to permit persons to whom the Software is | ||
| # furnished to do so, subject to the following conditions: | ||
| # | ||
| # The above copyright notice and this permission notice shall be included in all | ||
| # copies or substantial portions of the Software. | ||
| # | ||
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| # SOFTWARE. | ||
|
|
||
| """Default logger formatter module.""" | ||
|
|
||
| import logging | ||
|
|
||
|
|
||
| class PyAnsysBaseFormatter(logging.Formatter): | ||
| """Custom formatter to truncate long columns.""" | ||
|
|
||
| def set_column_width(self, width: int): | ||
| """Set the maximum column width for module and function names.""" | ||
| # at least 8 | ||
| if width < 8: | ||
| raise ValueError("Column width must be at least 8 characters.") | ||
| self._max_column_width = width | ||
|
|
||
| @property | ||
| def max_column_width(self): | ||
| """Get the maximum column length.""" | ||
| if not hasattr(self, "_max_column_width"): | ||
| self._max_column_width = 15 | ||
| return self._max_column_width | ||
|
|
||
| def format(self, record): | ||
| """Format the log record, truncating the module and function names if necessary.""" | ||
| if len(record.module) > self.max_column_width: | ||
| record.module = record.module[: self.max_column_width - 3] + "..." | ||
| if len(record.funcName) > self.max_column_width: | ||
| record.funcName = record.funcName[: self.max_column_width - 3] + "..." | ||
|
|
||
| # Fill the module and function names with spaces to align them | ||
| record.module = record.module.ljust(self.max_column_width) | ||
| record.funcName = record.funcName.ljust(self.max_column_width) | ||
|
|
||
| return super().format(record) | ||
|
|
||
|
|
||
| DEFAULT_FORMATTER = PyAnsysBaseFormatter( | ||
| "%(asctime)s [%(levelname)-8s | %(module)s | %(funcName)s:%(lineno)-4d] > %(message)s" | ||
| ) | ||
| DEFAULT_FORMATTER.set_column_width(15) | ||
AlejandroFernandezLuces marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """Default formatter for the logger.""" | ||
|
|
||
| DEFAULT_HEADER = ( | ||
| "-" * (70 + DEFAULT_FORMATTER.max_column_width) | ||
| + "\n" | ||
| + f"Timestamp [Level | Module{' ' * (DEFAULT_FORMATTER.max_column_width - 6)} | Function{' ' * (DEFAULT_FORMATTER.max_column_width - 8)}:Line] > Message\n" # noqa: E501 | ||
| + "-" * (70 + DEFAULT_FORMATTER.max_column_width) | ||
| + "\n" | ||
| ) | ||
| """Default header for the log file.""" | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.