Skip to content

Instantly share code, notes, and snippets.

Embed
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 https://gist.githubusercontent.com/popstas/ffcf282492fd78389d1df2ab7f31052a/raw/505cdf97c6a1edbb10c3b2b64e1836e0627b87a0/docker-logs-localtime > /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.resume();
process.stdin.setEncoding('utf8');
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);
});
}
process.stdout.write(data);
});
@dojo90

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..

@popstas

This comment has been minimized.

Copy link
Owner Author

popstas commented Feb 6, 2019

@dojo90,

  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 https://gist.githubusercontent.com/popstas/ffcf282492fd78389d1df2ab7f31052a/raw/505cdf97c6a1edbb10c3b2b64e1836e0627b87a0/docker-logs-localtime > ./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.