Promtail timestamp formatting

log info

time:2023-11-06T09:59:12.233121Z

problem

I am a green hand. How do I use timestamp stage to process the time in the log and change it into the format “2006-01-02 15:04:05”
Here is my configuration:

         - regex:
             expression: '(?P<timestamp>\d{4}-\d{2}-\d{2}\w\d{2}:\d{2}:\d{2}.\w+).*'
         - labels:
             timestamp:
         - timestamp:
             format: "2006-01-02 15:04:05"
             source: timestamp             
             location: "Asia/Shanghai"

error logs:

level=debug ts=2023-11-06T07:30:24.6458416Z caller=timestamp.go:196 component=file_pipeline msg="failed to parse time" err="parsing time \"2023-11-06T09:59:12.233121Z\" as \"2006-01-02 15:04:05\": cannot parse \"T09:59:12.233121Z\" as \" \"" format="2006-01-02 15:04:05" value=2023-11-06T09:59:12.233121Z

First obvious thing I can see is your regex capture doesn’t match your timestamp format. You have a \w+ at the end, but you should stop at the third \d{2} instead.

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.