Skip to content

Instantly share code, notes, and snippets.

@anton-x-t
Forked from popstas/docker-logs-localtime
Last active August 3, 2023 07:37
Show Gist options
  • Save anton-x-t/859a69d0426ed1e4040660f57229c76c to your computer and use it in GitHub Desktop.
Save anton-x-t/859a69d0426ed1e4040660f57229c76c to your computer and use it in GitHub Desktop.

Revisions

  1. anton-x-t revised this gist Aug 3, 2023. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions docker-logs-localtime
    Original file line number Diff line number Diff line change
    @@ -54,9 +54,7 @@ process.stdin.on('data', function(data) {
    const match = data.match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z/g);
    if (match) {
    match.forEach(dateUtc => {
    const d = new Date(dateUtc);
    const offset = new Date().getTimezoneOffset() * 60000;
    const dateLocal = toIsoString(new Date(d.getTime() - offset));
    const dateLocal = toIsoString(new Date(dateUtc));
    data = data.replace(dateUtc, dateLocal);
    });
    }
  2. anton-x-t revised this gist Aug 2, 2023. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions docker-logs-localtime
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,10 @@
    * thank you Steven Moseley,
    * https://stackoverflow.com/a/17415677
    *
    * ISO 8601 - Wikipedia,
    * thank you Wikipedia Contributors,
    * https://en.wikipedia.org/wiki/ISO_8601
    *
    * docker-logs-localtime,
    * thank you popstas,
    * https://gist.github.com/popstas/ffcf282492fd78389d1df2ab7f31052a
  3. anton-x-t revised this gist Aug 2, 2023. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions docker-logs-localtime
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,10 @@
    * thank you anton-x-t,
    * https://gist.github.com/anton-x-t/859a69d0426ed1e4040660f57229c76c
    *
    * MDN Date, getMilliseconds(),
    * thank you MDN,
    * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#date_components_and_time_zones
    *
    * How to ISO 8601 format a Date with Timezone Offset in JavaScript?,
    * thank you Steven Moseley,
    * https://stackoverflow.com/a/17415677
  4. anton-x-t revised this gist Aug 2, 2023. 1 changed file with 42 additions and 21 deletions.
    63 changes: 42 additions & 21 deletions docker-logs-localtime
    Original file line number Diff line number Diff line change
    @@ -1,35 +1,56 @@
    #!/usr/bin/env node
    // replace all UTC dates to local dates in pipe
    // usage: docker logs -t container_name | docker-logs-localtime
    /*
    * replace all UTC dates to local datetime with timezone offset through pipe
    * usage: docker logs -ft container_name | docker-logs-localtime
    *
    * Credits:
    *
    * docker-logs-localtime,
    * thank you anton-x-t,
    * https://gist.github.com/anton-x-t/859a69d0426ed1e4040660f57229c76c
    *
    * How to ISO 8601 format a Date with Timezone Offset in JavaScript?,
    * thank you Steven Moseley,
    * https://stackoverflow.com/a/17415677
    *
    * docker-logs-localtime,
    * thank you popstas,
    * https://gist.github.com/popstas/ffcf282492fd78389d1df2ab7f31052a
    *
    * docker-logs-localtime,
    * thank you HuangYingNing,
    * https://github.com/HuangYingNing/docker-logs-localtime
    */
    function toIsoString(date) {
    var tzo = -date.getTimezoneOffset(),
    dif = tzo >= 0 ? '+' : '-',
    pad = function(num) {
    return (num < 10 ? '0' : '') + num;
    };

    // install:
    // curl https://gist.github.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
    // Thank you popstas for the gist! https://gist.github.com/popstas/ffcf282492fd78389d1df2ab7f31052a

    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(':')
    );
    };
    return date.getFullYear() +
    '-' + pad(date.getMonth() + 1) +
    '-' + pad(date.getDate()) +
    'T' + pad(date.getHours()) +
    ':' + pad(date.getMinutes()) +
    ':' + pad(date.getSeconds()) +
    '.' + pad(date.getMilliseconds()) +
    dif + pad(Math.floor(Math.abs(tzo) / 60)) +
    ':' + pad(Math.abs(tzo) % 60);
    }

    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);
    data = data.replace(/\d{2}:\d{2}:\d{2}\.\d{3} /g, '');
    const match = data.match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z/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();
    const dateLocal = toIsoString(new Date(d.getTime() - offset));
    data = data.replace(dateUtc, dateLocal);
    });
    }
    process.stdout.write(data);
    });
    });
  5. anton-x-t revised this gist Jul 31, 2023. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions docker-logs-localtime
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,7 @@
    // curl https://gist.github.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
    // Thank you popstas for the gist! https://gist.github.com/popstas/ffcf282492fd78389d1df2ab7f31052a

    const pad = d => (d > 9 ? d : '0' + d);

  6. anton-x-t revised this gist Jul 31, 2023. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions docker-logs-localtime
    Original file line number Diff line number Diff line change
    @@ -11,9 +11,9 @@ 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(':')
    [this.getFullYear(), pad(this.getMonth() + 1), pad(this.getDate())].join('-')// +
    //' ' +
    //[pad(this.getHours()), pad(this.getMinutes()), pad(this.getSeconds())].join(':')
    );
    };

  7. @popstas popstas revised this gist Mar 4, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions docker-logs-localtime
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,8 @@
    // install:
    // curl https://gist.github.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() {
  8. @popstas popstas revised this gist Jan 30, 2020. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion docker-logs-localtime
    Original file line number Diff line number Diff line change
    @@ -2,11 +2,14 @@
    // replace all UTC dates to local dates in pipe
    // usage: docker logs -t container_name | docker-logs-localtime

    // install:
    // curl https://gist.github.com/popstas/ffcf282492fd78389d1df2ab7f31052a/raw/505cdf97c6a1edbb10c3b2b64e1836e0627b87a0/docker-logs-localtime > /usr/local/bin/docker-logs-localtime && chmod +x /usr/local/bin/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('-') +
    [this.getFullYear(), pad(this.getMonth() + 1), pad(this.getDate())].join('-') +
    ' ' +
    [pad(this.getHours()), pad(this.getMinutes()), pad(this.getSeconds())].join(':')
    );
  9. @popstas popstas revised this gist Jan 30, 2020. 1 changed file with 4 additions and 6 deletions.
    10 changes: 4 additions & 6 deletions docker-logs-localtime
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,12 @@
    #!/usr/bin/env node
    // replace all UTC dates to local dates in pipe

    // install:
    // curl https://gist.github.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('-') +
    [this.getFullYear(), pad(this.getDate()), pad(this.getMonth() + 1)].join('-') +
    ' ' +
    [pad(this.getHours()), pad(this.getMinutes()), pad(this.getSeconds())].join(':')
    );
    @@ -23,7 +19,9 @@ process.stdin.on('data', function(data) {
    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();
    const d = new Date(dateUtc);
    const offset = new Date().getTimezoneOffset() * 60000;
    const dateLocal = new Date(d.getTime() - offset).outDateTime();
    data = data.replace(dateUtc, dateLocal);
    });
    }
  10. @popstas popstas revised this gist Feb 6, 2019. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion docker-logs-localtime
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,16 @@
    #!/usr/bin/env node
    // replace all UTC dates to local dates in pipe

    // install:
    // curl https://gist.github.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.getDate()), pad(this.getMonth() + 1)].join('-') +
    [this.getFullYear(), pad(this.getMonth() + 1), pad(this.getDate())].join('-') +
    ' ' +
    [pad(this.getHours()), pad(this.getMinutes()), pad(this.getSeconds())].join(':')
    );
  11. @popstas popstas created this gist Jan 23, 2019.
    27 changes: 27 additions & 0 deletions docker-logs-localtime
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    #!/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);
    });