Skip to content

Commit b895af8

Browse files
europauleriknordmark
authored andcommitted
newlog: fix ANSI escape code stripping
- Updates the regex to catch both raw and escaped representations of ANSI codes. - Actually use the cleaned message text instead of the original message text when populating the logInfo struct. Signed-off-by: Paul Gaiduk <paulg@zededa.com>
1 parent f71f601 commit b895af8

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

pkg/newlog/cmd/getMemlogMsg.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
)
1919

2020
const (
21-
ansi = "\u001B\\[[0-9;]*[A-Za-z]|\u001B[\\(\\)\\[\\]#;?]*[A-Za-z0-9]|\u009B[0-9;]*[A-Za-z]"
21+
ansi = "(?:\u001B|\\\\u001[bB])\\[[0-9;]*[A-Za-z]|(?:\u001B|\\\\u001[bB])[\\(\\)\\[\\]#;?]*[A-Za-z0-9]|(?:\u009B|\\\\u009[bB])[0-9;]*[A-Za-z]"
2222
)
2323

2424
var (
@@ -171,8 +171,8 @@ func parseLogInfo(memlogEntry MemlogLogEntry) Loginfo {
171171
if logInfo.Source == "" {
172172
logInfo.Source = memlogEntry.Source
173173
}
174-
// and keep the original message text and fields
175-
logInfo.Msg = memlogEntry.Msg
174+
// and keep the cleaned message text and fields
175+
logInfo.Msg = cleanMsg
176176
} else {
177177
// Some messages have attr=val syntax
178178
// If the inner message has Level, Time or Msg set they take

pkg/newlog/cmd/getMemlogMsg_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,18 @@ func TestParseMemlogEntryIndividual(t *testing.T) {
138138
"timestamp": "2025-10-06T18:31:44.311128265Z",
139139
"source": "zedmanager",
140140
"severity": "info",
141-
"content": "{\"file\":\"/pillar/types/zedmanagertypes.go:364\",\"func\":\"github.com/lf-edge/eve/pkg/pillar/types.AppInstanceStatus.LogModify\",\"level\":\"info\",\"log_event_type\":\"log\",\"msg\":\"App instance status modify\",\"obj_key\":\"app_instance_status-173ee6b9-c454-45b8-959e-5c1e9c91a0ce\",\"obj_name\":\"nginx\",\"obj_type\":\"app_instance_status\",\"obj_uuid\":\"173ee6b9-c454-45b8-959e-5c1e9c91a0ce\",\"old-purge-in-progress\":0,\"old-restart-in-progress\":0,\"old-state\":\"BOOTING\",\"pid\":2221,\"purge-in-progress\":0,\"restart-in-progress\":0,\"source\":\"zedmanager\",\"state\":\"RUNNING\",\"time\":\"2025-10-06T18:31:44.311128265Z\"}",
141+
"content": `{"file":"/pillar/types/zedmanagertypes.go:364","func":"github.com/lf-edge/eve/pkg/pillar/types.AppInstanceStatus.LogModify","level":"info","log_event_type":"log","msg":"App instance status modify","obj_key":"app_instance_status-173ee6b9-c454-45b8-959e-5c1e9c91a0ce","obj_name":"nginx","obj_type":"app_instance_status","obj_uuid":"173ee6b9-c454-45b8-959e-5c1e9c91a0ce","old-purge-in-progress":0,"old-restart-in-progress":0,"old-state":"BOOTING","pid":2221,"purge-in-progress":0,"restart-in-progress":0,"source":"zedmanager","state":"RUNNING","time":"2025-10-06T18:31:44.311128265Z"}`,
142+
},
143+
},
144+
{
145+
name: "Entry with ANSI color codes in a JSON message",
146+
input: `{"time":"2026-02-05T18:52:28.551352594Z","source":"pillar","msg":"{\"appuuid\":\"ff9d588b-e22c-4256-abe4-9da5bae810d9\",\"level\":\"info\",\"msg\":\"Hello, \\u001b[31m this is my text \\u001b[0m. How does it look?\",\"time\":\"2026-02-05T18:52:28.551046324Z\"}"}`,
147+
expectError: false,
148+
expectedFields: map[string]string{
149+
"timestamp": "2026-02-05T18:52:28.551046324Z",
150+
"source": "pillar",
151+
"severity": "info",
152+
"content": `{"appuuid":"ff9d588b-e22c-4256-abe4-9da5bae810d9","level":"info","msg":"Hello, this is my text . How does it look?","time":"2026-02-05T18:52:28.551046324Z"}`,
142153
},
143154
},
144155
{
@@ -268,6 +279,11 @@ func TestCleanForLogParsing(t *testing.T) {
268279
input: "\x1b[31mRed text\x1b[0m",
269280
expected: "Red text",
270281
},
282+
{
283+
name: "ANSI color codes (unicode)",
284+
input: "\u001b[31m this is my text \u001b[0m.",
285+
expected: " this is my text .",
286+
},
271287
{
272288
name: "Newlines at start and end",
273289
input: "\nLine content\n",

0 commit comments

Comments
 (0)