如何从gitlab跟踪日志API中删除特殊字符
我称gitlab trace api获取作业原始日志。输出包含一些 Unicode/ansi字符,例如\ u001b,1m等。如何删除所有这些角色?我 尝试了许多不同的正则表达式,但没有一个正在删除所有角色集。有办法 读取无特殊字符的原始日志,或使用正则拨号删除它们。
Gitlab API: https://gitlab.some.com/api/v4/projects/110506/jobs/80341714/trace
**Response** : [0KRunning with gitlab-runner 14.10.1 (f761588f)[0;m
[0KRunning with gitlab-runner 14.10.1 (f761588f)[0;m
[0K on buildops-shared-linux-pks-s3b-main-10026789-gitlab-runner-vl58t
z4p2xwH_[0;m
section_start:1655122076:resolve_secrets
[0K[0K[36;1mResolving secrets[0;m[0;m
section_end:1655122076:resolve_secrets
[0Ksection_start:1655122076:prepare_executor
[0K[0K[36;1mPreparing the "kubernetes" executor[0;m[0;m
[0KUsing Kubernetes namespace: glr-shared[0;m
[0KUsing Kubernetes executor with image harbor.dell.com/devops-images/traditional-
[0K[0K[36;1mPreparing environment[0;m[0;m
我正在使用C#代码,在调试时,我可以看到它将这些特殊字符解码为\ u001b; [0k [1m;等等:“ \ 001b [跑步者上的0krunning''
_commonService.GetGitlabHeaders(errorLog.AuthToken));
var section_description = await
msg2.Result.Content.ReadAsStringAsync() ;
section_description =
Regex.Replace(Regex.Replace(section_description,"\x1B(?:[@-Z\\-
_] |[[0 -?][-/][@-~])", ""),"\a","");
I am calling gitlab trace api to get the job raw log. Output contains some
unicode/ANSI characters like \u001b, 1m etc . How do i remove all those characters? I
tried many different Regex but none is removing all the character set. Is there a way to
read raw log without special characters or remove them by using RegEx.
Gitlab API: https://gitlab.some.com/api/v4/projects/110506/jobs/80341714/trace
**Response** : [0KRunning with gitlab-runner 14.10.1 (f761588f)[0;m
[0KRunning with gitlab-runner 14.10.1 (f761588f)[0;m
[0K on buildops-shared-linux-pks-s3b-main-10026789-gitlab-runner-vl58t
z4p2xwH_[0;m
section_start:1655122076:resolve_secrets
[0K[0K[36;1mResolving secrets[0;m[0;m
section_end:1655122076:resolve_secrets
[0Ksection_start:1655122076:prepare_executor
[0K[0K[36;1mPreparing the "kubernetes" executor[0;m[0;m
[0KUsing Kubernetes namespace: glr-shared[0;m
[0KUsing Kubernetes executor with image harbor.dell.com/devops-images/traditional-
[0K[0K[36;1mPreparing environment[0;m[0;m
I am using C# code and while debugging i can see that it is decoding those special characters to \u001b;[0K [1m; etc like this : "\001b[0KRUnning on the runner"
_commonService.GetGitlabHeaders(errorLog.AuthToken));
var section_description = await
msg2.Result.Content.ReadAsStringAsync() ;
section_description =
Regex.Replace(Regex.Replace(section_description,"\x1B(?:[@-Z\\-
_] |[[0 -?][-/][@-~])", ""),"\a","");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在通过GitLab Trace API创建的日志文件的末尾,您可能会看到类似的内容:
这删除了大多数ANSI颜色代码,例如 ^[[0; m。
更新2022-11-21:正如@katrinleinweber建议的,Gitlab支持使用此正则:
这就像魅力一样。但这确实只留下了“启动”和“部分结尾”标签:
它们与gitlab gui中的可折叠和可膨胀部分有关

更新2022-11-25中的可扩展部分:要标记开始和结束部分用“>>>”和“<<<<<<您可以使用:
这将为您提供纯文本文件,包括节标记。
有关解决方法,请参见: https://gitlab.com/gitlab.com/gitlab-com/gitlab-com/gitlab-com/gitlab-com/gitlab-com/gitlab-.com/gitlab-com/gitlab-com-com/gitlab-com/gitlab-com-com/gitlab-com/gitlab-.com/gitlab-com/ghin-gitlab-com/ghin-g- org/gitlab-runner/ - /esseage/1950
例子:
At the end of a log file created via the gitlab trace api, you might see something like this:
This removes most of the ANSI color codes like ^[[0;m.
Update 2022-11-21: As suggested by @KatrinLeinweber, GitLab support uses this regex:
This works like a charm. But it does leave only the section start and section end tags:
They are related to the collapsable and expandable sections in the GitLab GUI

Update 2022-11-25: To mark start and end sections with ">>>" and "<<<", you can use:
This will leave you with a plain text file, including the section markers.
For a workaround, see: https://gitlab.com/gitlab-org/gitlab-runner/-/issues/1950
Example: