Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
docker-logs-localtime - 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
// usage: docker logs -t container_name | docker-logs-localtime
// 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
// alternative: https://github.com/HuangYingNing/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 d = new Date(dateUtc);
const offset = new Date().getTimezoneOffset() * 60000;
const dateLocal = new Date(d.getTime() - offset).outDateTime();
data = data.replace(dateUtc, dateLocal);
});
}
process.stdout.write(data);
});
@dojo90

This comment has been minimized.

Copy link

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

@raihan519

This comment has been minimized.

Copy link

@raihan519 raihan519 commented Jan 28, 2020

Hi popstas,

I recieved an error "/usr/bin/env: node: No such file or directory". Can you help me?

@popstas

This comment has been minimized.

Copy link
Owner Author

@popstas popstas commented Jan 28, 2020

@raihan519, script using nodejs, you should install it, apt install nodejs for Debian/Ubuntu.

If you have not nodejs yet, it will too excessively for this small script :)

@raihan519

This comment has been minimized.

Copy link

@raihan519 raihan519 commented Jan 29, 2020

@raihan519, script using nodejs, you should install it, apt install nodejs for Debian/Ubuntu.

If you have not nodejs yet, it will too excessively for this small script :)

Hi popstas,

Thank you so much for the information, it worked for me 👍

@raihan519

This comment has been minimized.

Copy link

@raihan519 raihan519 commented Jan 29, 2020

Hi popstas,

Another thing, i already add your script when i run docker logs, but it seems the time still using the UTC zone. I want to use the WIB zone (Asia/Jakarta). Do you have a clue how to do it?

# docker logs -t haraka | docker-logs-localtime 
2020-01-29 01:45:05 loaded TLD files: 1=1528 2=8564 3=2448
2020-01-29 01:45:05 loaded 8787 Public Suffixes
2020-01-29 01:45:05 2020-01-29 01:45:05 loglevel: INFO
2020-01-29 01:45:05 2020-01-29 01:45:05 log format: DEFAULT
2020-01-29 01:45:05 2020-01-29 01:45:05 [WARN] [-] [core] smtp.ini.nodes unset, using 1, see https://github.com/haraka/Haraka/wiki/Performance-Tuning
2020-01-29 01:45:05 2020-01-29 01:45:05 Starting up Haraka version 2.8.25
2020-01-29 01:45:05 2020-01-29 01:45:05 [INFO] [-] [core] Loading plugins
2020-01-29 01:45:05 2020-01-29 01:45:05 [INFO] [-] [core] Loading plugin: relay
2020-01-29 01:45:05 2020-01-29 01:45:05 [INFO] [-] [core] Loading plugin: dnsbl
2020-01-29 01:45:05 2020-01-29 01:45:05 [INFO] [-] [core] Loading plugin: helo.checks
2020-01-29 01:45:05 2020-01-29 01:45:05 [INFO] [-] [core] Loading plugin: mail_from.is_resolvable
2020-01-29 01:45:05 2020-01-29 01:45:05 [INFO] [-] [core] Loading plugin: data.headers
2020-01-29 01:45:05 2020-01-29 01:45:05 [INFO] [-] [core] Loading plugin: queue/smtp_forward
2020-01-29 01:45:05 2020-01-29 01:45:05 [NOTICE] [-] [queue/smtp_forward] outbound enabled, will default to disabled in Haraka v3 (see #1472)
2020-01-29 01:45:05 2020-01-29 01:45:05 [NOTICE] [-] [core] worker started worker=1 pid=17
@popstas

This comment has been minimized.

Copy link
Owner Author

@popstas popstas commented Jan 30, 2020

@raihan519, fixed!

@hynnet

This comment has been minimized.

Copy link

@hynnet hynnet commented Mar 4, 2020

log data will lose...

@popstas

This comment has been minimized.

Copy link
Owner Author

@popstas popstas commented Mar 4, 2020

@hynnet, please example.

Are you add -t to docker logs command?

@hynnet

This comment has been minimized.

Copy link

@hynnet hynnet commented Mar 4, 2020

@popstas

This comment has been minimized.

Copy link
Owner Author

@popstas popstas commented Mar 4, 2020

@hynnet, then use sh version :)
Scripts working same for me, but my script also removes nanoseconds:

image

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.