Bash shell:为日志输出着色

Bash shell:为日志输出着色,bash,shell,Bash,Shell,我想给输出文本上色 我的意思是,我得到了日志: [pod/hes-mpi-fhir-api-2-4p55p/hes-mpi-fhir-api] {"time": "2021-05-27 09:14:02,641", "level": "INFO", "trace_id": "0", "source": "c.u.f.j.s.r.ResourceRe

我想给输出文本上色

我的意思是,我得到了日志:

[pod/hes-mpi-fhir-api-2-4p55p/hes-mpi-fhir-api] {"time": "2021-05-27 09:14:02,641", "level": "INFO", "trace_id": "0", "source": "c.u.f.j.s.r.ResourceReindexingSvcImpl:390", "message": "Loaded 16229 resources for reindexing in 466ms"}
[pod/hes-mpi-fhir-api-2-4p55p/hes-mpi-fhir-api] {"time": "2021-05-27 09:14:02,649", "level": "ERROR", "trace_id": "0", "source": "c.u.f.j.s.r.ResourceReindexingSvcImpl:581", "message": "Failed to index resource SearchParameter/20003/_history/2: ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException: SearchParameter.status is missing or invalid ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException: SearchParameter.status is missing or invalid    at ca.uhn.fhir.jpa.dao.r4.FhirResourceDaoSearchParameterR4.validateSearchParam(FhirResourceDaoSearchParameterR4.java:106) ~[hapi-fhir-jpaserver-base-5.4.0.jar!/:na]    at ca.uhn.fhir.jpa.dao.r4.FhirResourceDaoSearchParameterR4.validateResourceForStorage(FhirResourceDaoSearchParameterR4.java:82) ~[hapi-fhir-jpaserver-base-5.4.0.jar!/:na]  at ca.uhn.fhir.jpa.dao.r4.FhirResourceDaoSearchParameterR4.validateResourceForStorage(FhirResourceDaoSearchParameterR4.java:47) ~[hapi-fhir-jpaserver-base-5.4.0.jar!/:na]  at ca.uhn.fhir.jpa.dao.BaseHapiFhirDao.updateEntity(BaseHapiFhirDao.java:1103) ~[hapi-fhir-jpaserver-base-5.4.0.jar!/:na]   at ca.uhn.fhir.jpa.dao.BaseHapiFhirResourceDao.reindex(BaseHapiFhirResourceDao.java:1283) ~[hapi-fhir-jpaserver-base-5.4.0.jar!/:na]    at ca.uhn.fhir.j
我想给第一个标题上色。有人这样想:

[pod/hes-mpi-fhir-api-2-4p55p/hes-mpi-fhir api]{{“时间”:“202…”


还有其他方法可以得到这个吗?

在终端中,文本可以使用ANSI代码着色。您只需在
[…]前后插入它们即可:

sed $'s/^/\033[31m/;s/]/]\033[m/' yourLogFile

  • \033[31m
    开始为红色
  • \033[m
    禁用所有颜色
  • 我们需要C样式的字符串
    $'…'
    ,以便解释这些转义序列
  • sed s/abc/xyz/
    将第一次出现的
    abc
    替换为
    xyz
  • 可以使用
    组合多个
    s/../…/
  • ^
    是一行的开头
    • 使用awk:

      $ awk 'BEGIN{FS="] {"; def="\x1B[m"; red="\x1B[31m"; yellow="\x1B[33m"}{printf("%s%s] %s{%s%s\n",red,$1,yellow,$2,def)}' log.txt
      

      您搜索了什么,找到了什么?您尝试了什么,失败的原因是什么?您能解释一下每个部分的用途吗?我应该做些什么来突出显示例如
      “ERROR”
      标记?@Jordi添加了解释。