-
-
Save popstas/ffcf282492fd78389d1df2ab7f31052a to your computer and use it in GitHub Desktop.
#!/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); | |
}); |
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
@raihan519, fixed!
log data will lose...
@hynnet, please example.
Are you add -t
to docker logs
command?
@hynnet, please example.
Are you add
-t
todocker logs
command?
(https://user-images.githubusercontent.com/1593300/75873710-4cd50680-5e08-11ea-8158-852511114d52.png)
sh script version is ok:
https://github.com/HuangYingNing/docker-logs-localtime
@hynnet, then use sh version :)
Scripts working same for me, but my script also removes nanoseconds:
Does this script works on windows?
Should work, not tested
Hallo, when I try this script, I get an error:
double free or corruption (out)
Aborted (core dumped)
Do you have an idea why?
It works for
echo
command, but not fordocker logs
. What could be the problem I'm having?
Fixed! I found that it is because the output of docker logs
in my case is stderr(not stdout). So my workaround is redirecting the stderr to stdout by adding 2>&1
at the tail of docker logs
command. For example:
$ docker logs -t -n 1 doccano 2>&1 | ./docker-logs-localtime
2022-03-02 14:42:21 django.db.utils.IntegrityError: UNIQUE constraint failed
It works for me ( Docker version 20.10.13, build a224086 @ WSL2 Ubuntu 20.04)
Thanks for the good work 👍 💻
Thank you very much!
Fork with timezone offset:
https://gist.github.com/anton-x-t/859a69d0426ed1e4040660f57229c76c
Hi popstas,
Thank you so much for the information, it worked for me 👍