Hass.io: Show ANSI color codes in logs#2155
Conversation
| pre { | ||
| overflow-x: auto; | ||
| white-space: pre-wrap; | ||
| overflow-wrap: break-word; |
| while (this.$.content.lastChild) { | ||
| this.$.content.removeChild(this.$.content.lastChild); | ||
| } | ||
| this.$.content.appendChild(HassioAddonLogs.parseLogsToPre(text)); |
There was a problem hiding this comment.
| this.$.content.appendChild(HassioAddonLogs.parseLogsToPre(text)); | |
| this.$.content.appendChild(this.parseLogsToPre(text)); |
There was a problem hiding this comment.
parseLogsToPre is a static function.
| }, | ||
| () => { | ||
| this.log = "Error fetching logs"; | ||
| while (this.$.content.lastChild) { |
There was a problem hiding this comment.
You don't have to do all this, just put the error message in the div:
| while (this.$.content.lastChild) { | |
| this.$.content.innerHTML = "Error fetching logs"; |
| while (this.$.content.lastChild) { | ||
| this.$.content.removeChild(this.$.content.lastChild); | ||
| } | ||
| this.$.content.appendChild(HassioSupervisorLog.parseLogsToPre(text)); |
There was a problem hiding this comment.
| this.$.content.appendChild(HassioSupervisorLog.parseLogsToPre(text)); | |
| this.$.content.appendChild(this.parseLogsToPre(text)); |
|
We should create a helper function to convert an ANSI log to HTML, that can be called inside these files. |
I don't think this is a frontend problem, the supervisor or backend should fix that? |
|
I love the green output 👍 |
I find this hard to read. Yes this is probably more of a frontend problem, though when viewing the supervisor docker logs it is nice to have the green text - for the frontend it is IMHO not so nice because readability goes down a lot.
Yes, that's true. But I don't know how 😇 "However I don't know how to create such a class + I have no idea how imports work here" - Care to explain where such a file would be created (I can't find a |
|
For readability, you could make the background of the log screen dark, as most of those colors were designed for a dark background? You should create a new file export function createHtml(text) {
...
return formatted;
}You can then import them like: import { createHtml } from "../ansi-to-html";You can then just use that function. |
|
Thanks! ❤️ For the black background: yes for readability that would be ideal (also the way it's done in my base implementation, the esphomeyaml dashboard). Example: |
|
I think it looks better, but the entire card should be dark. |
|
Yes, we could do that. Currently, we follow Home Assistant style. |
|
Yes, that was also my concern before. Speaking for myself, I like the green color output when viewing the docker logs in the terminal - especially when showing the logs from multiple containers at once. For the frontend however I think the green output just hurts readability. Therefore my initial idea was to do remove the green INFO level in the frontend layer. That way we get nice colored logs in the terminal plus nice readable messages in the supervisor logs. |
|
I think that sounds good. We can start and change things later if they are a problem. |
| ${ANSI_HTML_STYLE} | ||
| <paper-card heading="Log"> | ||
| <div class="card-content"><pre>[[log]]</pre></div> | ||
| <div class="card-content" id="content"></div> |
There was a problem hiding this comment.
| <div class="card-content" id="content"></div> | |
| <div class="card-content">[[_computeLog(log)]]</div> |
With:
_computeLog(log) {
return parseTextToColoredPre(log);
}
There was a problem hiding this comment.
Unless I misunderstand how the html function works, I believe this way would be quite inefficient.
parseTextToColoredPre parses the log and returns a DOM element. With the method that is implemented here, that generated DOM element must then just be appended - done. So API response -> DOM element (parseTextToColoredPre) -> append
With _computeLog however, the generated DOM tree would need to be serialized to HTML after parseTextToColoredPre and then be de-serialized again when it is inserted into the <template>. So API response -> DOM element (parseTextToColoredPre) -> Serialize -> Insert into <template> -> De-serialize by browser. Also, _computeLog would need to look something like this:
_computeLog(log) {
return parseTextToColoredPre(log).outerHTML;
}In the application where this code is taken from, this really became a performance problem with longer logs (I used innerHTML, but the speed would be similar). Especially once the Hass.io logs are streamed using websockets re-computing the entire log every time a new line appears would be quite inefficient IMHO.
There was a problem hiding this comment.
You might be right... let's use lit, that will fix it all :-P
There was a problem hiding this comment.
Yeah, Polymer databinding doesn't allow setting HTML content, just text.
| type: String, | ||
| observer: "addonSlugChanged", | ||
| }, | ||
| log: String, |
| .then((info) => { | ||
| this.log = info; | ||
| .then((text) => { | ||
| while (this.$.content.lastChild) { |
There was a problem hiding this comment.
| while (this.$.content.lastChild) { | |
| this.log = text; |
hassio/src/ansi-to-html.js
Outdated
| }; | ||
|
|
||
| /* eslint-disable no-constant-condition */ | ||
| while (true) { |
There was a problem hiding this comment.
| while (true) { | |
| let match; | |
| while ((match = re.exec(text)) !== null) { |
There was a problem hiding this comment.






Home-Assistant PR to forward ANSI color codes in HTTP view: home-assistant/core#18834
Show colors in Hass.io logs. Example:
(source: https://gist.github.com/OttoWinter/88ec233c3bd714c00f86cb70018aa7af)