feat (DATAGO-118265): colour-coded console logging
Adding colours is a neat idea.
Application logging as a concept is owned by the main application at the solace-ai-connector level. If we want to support colours, I believe supports for it should be added there.
As it is, the main application logging configuration gets overridden.
Here's what I see:
When I enable JSON Logs, the JSON formatting is lost as soon as the colouring gets configured.
A few considerations:
- Colouring likely only makes sense for non-JSON logs for instance the simpleFormatter we generate by default
- SAM Cloud deployments tail stdout logs; I am not sure if that has an impact but colouring should play well with that. SAM Cloud enables JSON logs so if colouring is OFF with JSON logs that should be fine.
- There seem to be libraries that do that. Maybe we could use one of them or get inspired. Using a proven 3rd-party library also simplifies our testing responsibilities.
- If there's a performance impact, coloured logs should be off in production
Implementation Idea
Your custom logging formatter could live in solace-ai-connector.logging.py (similar to this unmerged and closed PR which was attempting to add a custom formatter) and users could choose to use it in their logging configuration files:
formatters:
simpleFormatter:
format: "%(asctime)s | %(levelname)-5s | %(threadName)s | %(name)s | %(message)s"
simpleColouredFormatter:
class: solace_ai_connector.logging.colouredFormatter
format: "%(asctime)s | %(levelname)-5s | %(threadName)s | %(name)s | %(message)s"
jsonFormatter:
"()": pythonjsonlogger.json.JsonFormatter
format: "%(timestamp)s %(levelname)s %(threadName)s %(name)s %(message)s"
timestamp: "timestamp"
handlers:
streamHandler:
class: logging.StreamHandler
formatter: simpleColouredFormatter
stream: ext://sys.stdout
The main benefit of this approach is that it wouldn't conflict with the application logging configuration since colouring is part of it rather than added later on. This solution also naturally only targets non-JSON formatters.
It is likely possible to add colouring programmatically (like this PR is doing) but we need to be more careful. Users have a lot of flexibility with their logging config so whatever we do needs to play nice with users' configs.
Quality Gate passed
Issues
0 New issues
0 Fixed issues
0 Accepted issues
Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code