Closed jemrobinson closed 1 month ago
Click to see where and how coverage changed
File Statements Missing Coverage Coverage
(new stmts)Lines missing
data_safe_haven/infrastructure
project_manager.py
82
Project Total
This report was generated by python-coverage-comment-action
The current process looks like the following:
"\x1b[38;5;13m\x1b[1mDuration\x1b[0m 45s"
)ProjectManager::log_message()
logging::from_ansi()
rich.Text
object (e.g. <text 'Duration 45s' [Span(0, 8, Style(color=Color('color(13)', ColorType.STANDARD, number=13), bold=True))]>
)logging.logger.info
rich.Handler
it's converted from a rich.Text
object to a string, dropping the formatting along the wayrich.Handler
then prints the non-coloured string to the consoleThe change in this PR does the following:
"\x1b[38;5;13m\x1b[1mDuration\x1b[0m 45s"
)logging.logger.info
rich.Handler
then prints the coloured string to the consoleThere's no problem with how rich
is handling the string conversion - it's just that logging
doesn't want to pass rich.Text
objects around internally.
The other option I considered was to convert the ANSI markers to rich
markup that would pass through the logging
internals equally well, but there doesn't seem to be much point in converting from ANSI just to convert straight back again.
Note on the strip_ansi()
function:
It's because you quite often get Pulumi output that looks like this:
"\033[31;merror:\033[0m Code='AnErrorCode'"
which we'd like to log as "Pulumi output: Code='AnErrorCode'"
.
If we try to do error_line.replace("error:", "").strip()
then the first whitespace character is masked by the ANSI colour codes so you get:
"Pulumi output: \033[31;m\033[0m Code='AnErrorCode'"
.
which looks like
and effectively a double-space in the output.
If we can strip the ANSI codes first, then the strip()
call will work. There are other ways to strip ANSI codes but they mainly involve complicated regexes. If you'd prefer to do this then we can.
I'll move this commit to the other PR though.
@jemrobinson Could you check if https://github.com/alan-turing-institute/data-safe-haven/tree/pulumi_colour does the same? It seems to work for me.
I can't quite remember the thinking behind removing the colour, I think it was more about removing the ANSI codes. The logging handler doesn't seem to work with rich Text (which is odd given it is a rich handler) but you can convert the Text object to a string with rich markup like we use for other log messages.
Yes it works, I've already tested this. I don't really see the benefit of converting ANSI-text to markup if it's not causing problems though: see my note above: The other option I considered was to convert the ANSI markers to rich markup that would pass through the logging internals equally well, but there doesn't seem to be much point in converting from ANSI just to convert straight back again.
I don't think we need the log_message
or from_ansi
functions at all.
NB. The logging handler does work with rich.Text
objects, but the Python logging
library converts these to str
before they make it as far as RichHandler
(I tested this by adding debugging print statements throughout the logging and Rich libraries).
Before the ANSI codes were printed.
If that's not true now, I'm not sure what has changed but we wouldn't need the extra handling. Otherwise, I think converting from ANSI markup to rich markup is most sensible because that is what we use elsewhere.
I don't see printed ANSI codes with this change, and I'm not sure why this was happening in #1952. Note that in 5.0.0-rc1
we also passed Pulumi output straight to the logger (see below) and this was printed correctly.
If you think there are other benefits to converting to rich markup, I'm happy to restore a helper function that does so.
Do we need the changes here then? The strip_ansi
stuff feels like a lot of complexity to remove a space.
I feel like that is more likely to cause problems than just pre-pending the full Pulumi error with "Pulumi error:", replacing anything.
Do we need the changes here then? The
strip_ansi
stuff feels like a lot of complexity to remove a space.
from_ansi
function (or something equivalent)strip_ansi
changes - the change you suggest would also remove the need for us to try to guess the structure of a Pulumi error message. It might look a bit like this:strip_ansi()
removed in 51a0ff935. Are you happy to merge @JimMadge ?
:white_check_mark: Checklist
Enable foobar integration
rather than515 foobar
).develop
.:vertical_traffic_light: Depends on
2111
:arrow_heading_up: Summary
IMPORTANT: I am confident that merging this will not affect the findings of the ongoing penetration test
Fix Pulumi colours
Before
After
:closed_umbrella: Related issues
Closes #2112
:microscope: Tests
Tested by rerunning against an existing SRE.