Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Replace all UTC dates in docker logs output to local dates in pipe
#!/usr/bin/env node
// replace all UTC dates to local dates in pipe
// install:
// curl > /usr/local/bin/docker-logs-localtime && chmod +x /usr/local/bin/docker-logs-localtime
// usage: docker logs -t container_name | docker-logs-localtime
const pad = d => (d > 9 ? d : '0' + d);
Date.prototype.outDateTime = function() {
return (
[this.getFullYear(), pad(this.getMonth() + 1), pad(this.getDate())].join('-') +
' ' +
[pad(this.getHours()), pad(this.getMinutes()), pad(this.getSeconds())].join(':')
process.stdin.on('data', function(data) {
data = data.replace(/\.\d+Z /g, ' ');
const match = data.match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/g);
if (match) {
match.forEach(dateUtc => {
const dateLocal = new Date(dateUtc).outDateTime();
data = data.replace(dateUtc, dateLocal);

This comment has been minimized.

Copy link

dojo90 commented Jan 31, 2019

I always get the following error if using:
-ash: docker-logs-localtime: command not found

When I use docker logs -t container_name > docker-logs-localtime, the output is not changed..


This comment has been minimized.

Copy link
Owner Author

popstas commented Feb 6, 2019


  1. You should pipe to script, not output, | in place of >
  2. You should place script contents to file docker-logs-localtime and make it executable:
curl > ./docker-logs-localtime
chmod +x ..docker-logs-localtime

docker logs -t container_name | ./docker-logs-localtime

Place docker-logs-localtime into PATH directory for run it globally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.