如何从gitlab跟踪日志API中删除特殊字符

发布于 2025-02-13 09:52:19 字数 1285 浏览 0 评论 0原文

我称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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千鲤 2025-02-20 09:52:20

在通过GitLab Trace API创建的日志文件的末尾,您可能会看到类似的内容:

 section_end:1668970031:step_script^M^[[0Ksection_start:1668970031:cleanup_file_variables^M^[[0K^[[0K^[[36;1mCleaning up file based variables^[[0;m
^[[0;msection_end:1668970031:cleanup_file_variables^M^[[0K^[[32;1mJob succeeded
^[[0;m

这删除了大多数ANSI颜色代码,例如 ^[[0; m。

sed 's/\x1B\[[0-9;]*[mK]//g' temp.log | tr -d 

更新2022-11-21:正如@katrinleinweber建议的,Gitlab支持使用此正则:

sed "s,\x1B\[[0-9;]*[a-zA-Z],,g;s,\x0D\x0A,\x0A,g" temp.log

这就像魅力一样。但这确实只留下了“启动”和“部分结尾”标签:

... other log content ...
section_end:1669039557:step_script
section_start:1669039557:cleanup_file_variables
Cleaning up file based variables
section_end:1669039557:cleanup_file_variables
Job succeeded

它们与gitlab gui中的可折叠和可膨胀部分有关

更新2022-11-25中的可扩展部分:要标记开始和结束部分用“>>>”和“<<<<<<您可以使用:

sed -e 's/\r/\n/g' temp.log | sed "s,\x1B\[[0-9;]*[a-zA-Z]section_start:[0-9]*:, >>> ,g;s,\x1B\[[0-9;]*[a-zA-Z]section_end:[0-9]*:, <<< ,g;s,section_end:[0-9]*:, <<< ,g;s,\x1B\[[0-9;]*[a-zA-Z],,g;s,\x0D\x0A,\x0A,g"

这将为您提供纯文本文件,包括节标记。

Running with gitlab-runner x.x.x (xxxxxxxx)
  on [email protected] yyyyyyyy
 >>> resolve_secrets
Resolving secrets
 <<< resolve_secrets
 >>> prepare_executor
Preparing the "shell" executor
Using Shell executor...
 <<< prepare_executor
 >>> prepare_script
Preparing environment
Running on doesntexistyet.nl...
 <<< prepare_script
 >>> get_sources
Getting source from Git repository
Fetching changes with git depth set to 20...
Reinitialized existing Git repository in /<redacted>/.git/
Checking out 12345678 as main...
Removing old.log
Removing temp.log
Skipping Git submodules setup
 <<< get_sources
 >>> step_script
Executing "step_script" stage of the job script
$ echo "Hello World!"
Hello World!
 <<< step_script
 >>> cleanup_file_variables
Cleaning up file based variables
 <<< cleanup_file_variables
Job succeeded

有关解决方法,请参见: 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

解决方法:set log_format =“ text” 在跑步者的 config.toml 。。

例子:

concurrent = 8
check_interval = 0
log_format = "text"

[[runners]]
  executor = "shell"
  ....
\r'

更新2022-11-21:正如@katrinleinweber建议的,Gitlab支持使用此正则:

这就像魅力一样。但这确实只留下了“启动”和“部分结尾”标签:

它们与gitlab gui中的可折叠和可膨胀部分有关

更新2022-11-25中的可扩展部分:要标记开始和结束部分用“&gt;&gt;&gt;”和“&lt;&lt;&lt;&lt;&lt;&lt;您可以使用:

这将为您提供纯文本文件,包括节标记。

有关解决方法,请参见: 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

解决方法:set log_format =“ text” 在跑步者的 config.toml 。。

例子:

At the end of a log file created via the gitlab trace api, you might see something like this:

 section_end:1668970031:step_script^M^[[0Ksection_start:1668970031:cleanup_file_variables^M^[[0K^[[0K^[[36;1mCleaning up file based variables^[[0;m
^[[0;msection_end:1668970031:cleanup_file_variables^M^[[0K^[[32;1mJob succeeded
^[[0;m

This removes most of the ANSI color codes like ^[[0;m.

sed 's/\x1B\[[0-9;]*[mK]//g' temp.log | tr -d 

Update 2022-11-21: As suggested by @KatrinLeinweber, GitLab support uses this regex:

sed "s,\x1B\[[0-9;]*[a-zA-Z],,g;s,\x0D\x0A,\x0A,g" temp.log

This works like a charm. But it does leave only the section start and section end tags:

... other log content ...
section_end:1669039557:step_script
section_start:1669039557:cleanup_file_variables
Cleaning up file based variables
section_end:1669039557:cleanup_file_variables
Job succeeded

They are related to the collapsable and expandable sections in the GitLab GUI
collapsable and expandable sections in GitLab

Update 2022-11-25: To mark start and end sections with ">>>" and "<<<", you can use:

sed -e 's/\r/\n/g' temp.log | sed "s,\x1B\[[0-9;]*[a-zA-Z]section_start:[0-9]*:, >>> ,g;s,\x1B\[[0-9;]*[a-zA-Z]section_end:[0-9]*:, <<< ,g;s,section_end:[0-9]*:, <<< ,g;s,\x1B\[[0-9;]*[a-zA-Z],,g;s,\x0D\x0A,\x0A,g"

This will leave you with a plain text file, including the section markers.

Running with gitlab-runner x.x.x (xxxxxxxx)
  on [email protected] yyyyyyyy
 >>> resolve_secrets
Resolving secrets
 <<< resolve_secrets
 >>> prepare_executor
Preparing the "shell" executor
Using Shell executor...
 <<< prepare_executor
 >>> prepare_script
Preparing environment
Running on doesntexistyet.nl...
 <<< prepare_script
 >>> get_sources
Getting source from Git repository
Fetching changes with git depth set to 20...
Reinitialized existing Git repository in /<redacted>/.git/
Checking out 12345678 as main...
Removing old.log
Removing temp.log
Skipping Git submodules setup
 <<< get_sources
 >>> step_script
Executing "step_script" stage of the job script
$ echo "Hello World!"
Hello World!
 <<< step_script
 >>> cleanup_file_variables
Cleaning up file based variables
 <<< cleanup_file_variables
Job succeeded

For a workaround, see: https://gitlab.com/gitlab-org/gitlab-runner/-/issues/1950

Workaround: set log_format = "text" in the runner's config.toml.

Example:

concurrent = 8
check_interval = 0
log_format = "text"

[[runners]]
  executor = "shell"
  ....
\r'

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
collapsable and expandable sections in GitLab

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

Workaround: set log_format = "text" in the runner's config.toml.

Example:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文