amazon-web-services - 如何在 fluentd tail/s3 插件中删除 unicode
问题描述
我有 fluentd 配置,源为尾类型,目标为 aws s3。我可以将日志存储在 S3 中。
我们已经根据winston logger中的日志级别在应用程序日志中启用了着色,但是在存储在S3中时,我得到了颜色的unicode值,例如\u001b[34m debug \u001b[39m。特殊字符也会发生同样的情况(\u003c 表示 >)
Fluentd Config
--------------
<source>
@type tail
path /var/log/containers/abc-*.log
pos_file /var/log/abc.log.pos
tag abc.**
<parse>
@type none
</parse>
read_from_head true
</source>
<match abc.**>
@type s3
aws_key_id "#{ENV['AWS_ACCESS_KEY']}"
aws_sec_key "#{ENV['AWS_SECRET_ACCESS_KEY']}"
s3_bucket "#{ENV['S3_LOGS_BUCKET_NAME']}"
s3_region "#{ENV['S3_LOGS_BUCKET_REGION']}"
path "#{ENV['S3_LOGS_BUCKET_PREFIX']}"
s3_object_key_format %{path}/abc/%Y%m%d/%{index}.json
buffer_chunk_limit 20m
buffer_path /var/log/fluentd-buffer
store_as json
flush_interval 600s
time_slice_format %Y/%m/%d
utc
<format>
@type single_value
</format>
<instance_profile_credentials>
ip_address 169.254.169.254
port 80
</instance_profile_credentials>
</match>
当前存储在 S3 中的日志:
{"log":"2021-04-10T12:34:51.050Z - \u001b[34mdebug\u001b[39m: \u003e\u003e\u003e\u003e testlog1 from app \n","stream":"stdout","time":"2021-04-10T12:34:51.050571552Z"}
{"log":"2021-04-10T12:34:51.067Z - \u001b[34mdebug\u001b[39m: \u003c\u003c\u003c\u003c testlog2 from app\n","stream":"stdout","time":"2021-04-10T12:34:51.068105637Z"}
预期的
{"log":"2021-04-10T12:34:51.050Z - debug: <<<< exec start from app \n","stream":"stdout","time":"2021-04-10T12:34:51.050571552Z"}
{"log":"2021-04-10T12:34:51.067Z - debug: <<<< exec end from app\n","stream":"stdout","time":"2021-04-10T12:34:51.068105637Z"}
需要有关如何打印原始值的帮助。
解决方案
像这样尝试 fluentd record_transformer过滤器插件:
<filter abc.**>
@type record_transformer
enable_ruby true
<record>
message ${ record["message"].gsub(/(\\u\d+b\[\d+m)|(\\u\d+e)/, '') }
</record>
</filter>
推荐阅读
- java - android - 代码覆盖率不包括 kotlin 文件
- testing - 从 json 数组中提取和比较数据在空手道中不起作用
- rest - 当一切都同步时,为什么我的 API 调用不返回?
- javascript - 在 Leafletjs(不是子域)中使用多个域更快地加载切片
- ssl - heroku 免费套餐和 SSL 证书解决方法?
- python - colab中关于“libtensorflow_framework”的问题
- database - 如何在GCP(谷歌云平台)中每天重复运行python文件?
- r-markdown - Bookdown:密码保护 HTML 中的*单个*页面/章节
- django - models.OneToOneField(User, on_delete=models.CASCADE) 不工作。用户表正在更新,但 UserModelInfo 表未更新
- javascript - 如何使用 Material UI Fade 淡入文本字段输入的组件?