-
-
Save anton-x-t/859a69d0426ed1e4040660f57229c76c to your computer and use it in GitHub Desktop.
docker-logs-localtime - Replace all UTC dates in docker logs output to local dates in pipe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env node | |
| // replace all UTC dates to local dates in pipe | |
| // 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.getDate()), pad(this.getMonth() + 1)].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); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment