Pino: how to display different timezone or format-time string in the prettified logs ?

Created on 31 Jul 2017  Â·  17Comments  Â·  Source: pinojs/pino

I try to generate local unix-timestamp and format output, but after pino logger output will convert to europe...

``js const { convert, format, parse, now, tz } = require('cctz'); const _pino = require('pino'); const pino = _pino({ level: 'debug', timestamp: () => { //let _now = new Date(); //let offset = _now.getTimezoneOffset() * 60 * 1000; //return, "time": ${_now - offset}`;
// 2017-07-31T18:31:14.140Z

    //return ', "time":' + format('%F %T %z', convert(now(), tz('Asia/Taipei')));
    // "time":2017-07-31 18:31:14 +0800

    return ', "time":"' + format('%F %T %z', convert(now(), tz('Asia/Taipei'))) + '"'; 
    // 2017-07-31T10:31:14.000Z
},

});

Most helpful comment

@gbhipolitoglyph you can use pino-pretty
in v1.x | pino-pretty -t -n [-d 'format-string']
next v2.x | pino-pretty -t 'SYS:format-string'

All 17 comments

I am a bit lost on what you are trying to achieve, can you provide a full snippet showing the problem and what you are expecting to be different?

const { convert, format, parse, CivilTime, now, tz } = require('cctz');
let _pino = require('pino');
let pino = _pino({
    level: 'debug',
    timestamp: () => {
        return ', "time":' + format('%F %T %z', convert(now(), tz('Asia/Taipei')));
    },
});
pino.debug('test');
// {..., "time":2017-07-31 18:49:51 +0800,"msg":"test"...}

I want to show that [2017-07-31 18:49:51 +0800] DEBUG .... : test
so i change
return ', "time":"' + format('%F %T %z', convert(now(), tz('Asia/Taipei'))) + '"';
it will output
[2017-07-31T10:49:51.000Z] DEBUG .... : test

are you piping to the pino prettifier?
Il giorno lun 31 lug 2017 alle 12:56 victor0801x notifications@github.com
ha scritto:

const { convert, format, parse, CivilTime, now, tz } = require('cctz');let _pino = require('pino');let pino = _pino({
level: 'debug',
timestamp: () => {
return ', "time":' + format('%F %T %z', convert(now(), tz('Asia/Taipei')));
},
});pino.debug('test');// {..., "time":2017-07-31 18:49:51 +0800,"msg":"test"...}

I want to show that [2017-07-31 18:49:51 +0800] DEBUG .... : test
so i change

return ', "time":"' + format('%F %T %z', convert(now(),
tz('Asia/Taipei'))) + '"';

it will output
[2017-07-31T10:49:51.000Z] DEBUG .... : test

—
You are receiving this because you commented.

Reply to this email directly, view it on GitHub
https://github.com/pinojs/pino/issues/274#issuecomment-319035370, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AADL41dQaz9WPApdlRbnLgyjhXCg1fayks5sTbLhgaJpZM4OoNWm
.

sorry, my English is poor,
I execute my xxx.js by node xxx.js | pino
is it the pino prettifier ?
befor I change typeof time to string, it will output correct layout with my local-time and timezone
after I change tyoeof time to string to fit pino output, it will change time to europe-time

Yes, that's what you are doing?
You want to display a different timezone in the prettified logs?

yes
yes, that what i mean

Currently the prettifier does not change the timezone. You should not change the timestamp, but rather we should fix the prettifier and make it able to format the time string.

I see, thanks for quickly replies.
I'm very exciting you will make it possible.
Thanks again.

@victor0801x it doesn't seem like this issue is actually resolved. Did you come up with a solution you haven't shared? Because @mcollina is right, our prettifier needs a patch to support this idea.

Nope, as mcollna said, it's not support yet. so I think it's not an issue, more like a feature request

@jsumners which way is better? or others

  1. keep no change on timestamp, add pretty support parsing format-time string,
    and pino/bin accept more options(-z timezone or --date-format ISO8601), then parse to format-string. idea from https://www.npmjs.com/package/date-format
  2. change timestamp that accept an object with {layout: 'dateFormat'}, then pretty just output format-string

The best approach would be 1. Would you like to send a PR?

I imagine you're going to want to use a library that already exists to deal with format string parsing and time zones. Personally, I'd prefer that library be https://www.npmjs.com/package/js-joda instead of the heavy and slow Moment.

sorry for lately reply, recently is little busy.
@jsumners js-joda is powerful, but I didn't find a way pass number(unix timestamp) to parse format.
in my benchmark, date-format.js is faster than others and simple

const dateFns = require('date-fns');
const dateformat = require('dateformat');
const dateFormat = require('date-format');
const moment = require('moment');
const { ZonedDateTime, LocalDateTime, ZoneId } = require('js-joda');
const intl = new Intl.DateTimeFormat('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour12: false,
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
    timezone: 'Asia/Taipei'
});
console.log(intl.format(Date.now()))
const bench = require('fastbench')
let run = bench([
    function intlAPI(done) {
        intl.format(Date.now());
        process.nextTick(done);
    },
    function date_fns(done) {
        dateFns.format(Date.now(), 'YYYY-MM-DD HH:mm:ss.SSS ZZ');
        process.nextTick(done);
    },
    function date_format(done) {
        dateFormat.asString('yyyy-MM-dd hh:mm:ss.SSS O', new Date(Date.now()));
        process.nextTick(done);
    },
    function momentTZ(done) {
        moment(Date.now()).format('YYYY-MM-DD HH:mm:ss.SSS ZZ');
        process.nextTick(done);
    },
], 100000);
run()
2017-08-02 23:26:20
intlAPI*100000: 444.292ms
date_fns*100000: 686.966ms
date_format*100000: 416.747ms
momentTZ*100000: 725.200ms



md5-b7808c8cf421b75db6438bd1b3a43e4f



(Intl.DateTimeFormat) x 283,159 ops/sec ±0.27% (88 runs sampled)
(date-fns) x 134,857 ops/sec ±0.92% (90 runs sampled)
(date-format) x 281,630 ops/sec ±1.03% (82 runs sampled)
(moment) x 156,726 ops/sec ±1.48% (88 runs sampled)
(dateformat) x 107,344 ops/sec ±0.54% (90 runs sampled)
```

Closed by #280.

sorry so how do we now set the timezone piping pino in the CLI? thanks

@gbhipolitoglyph you can use pino-pretty
in v1.x | pino-pretty -t -n [-d 'format-string']
next v2.x | pino-pretty -t 'SYS:format-string'

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mcollina picture mcollina  Â·  6Comments

sachinavinaw picture sachinavinaw  Â·  7Comments

JamesKyburz picture JamesKyburz  Â·  7Comments

dunklesToast picture dunklesToast  Â·  3Comments

callumlocke picture callumlocke  Â·  4Comments