You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by Mikael Ståldal <mi...@magine.com> on 2016/03/23 16:13:33 UTC
Allocation free timestamp formatting for GELF
I am trying to make GelfLayout allocation free, and got stuck at timestamp
formatting. GELF need the timestamp as number of seconds with milliseconds
as decimals.
E.g. 1458741206653 -> "1458741206.653"
The current implementation (in GelfLayout.java) is obviously not allocation
free:
static CharSequence formatTimestamp(final long timeMillis) {
return new BigDecimal(timeMillis).divide(TIME_DIVISOR).toPlainString();
}
(see also unit test in GelfLayoutTest.java)
Any ideas on how do fix this?
--
[image: MagineTV]
*Mikael Ståldal*
Senior software developer
*Magine TV*
mikael.staldal@magine.com
Grev Turegatan 3 | 114 46 Stockholm, Sweden | www.magine.com
Privileged and/or Confidential Information may be contained in this
message. If you are not the addressee indicated in this message
(or responsible for delivery of the message to such a person), you may not
copy or deliver this message to anyone. In such case,
you should destroy this message and kindly notify the sender by reply
email.
Re: Allocation free timestamp formatting for GELF
Posted by Remko Popma <re...@gmail.com>.
"that logic" is "the logic that inserts a period at StringBuilder.length()
- 3".
On Thu, Mar 24, 2016 at 1:34 AM, Remko Popma <re...@gmail.com> wrote:
> In that case you may need to surround that logic with a check that the
> long value exceeds 999. Could this ever happen though?
>
> On Thu, Mar 24, 2016 at 1:18 AM, Mikael Ståldal <mikael.staldal@magine.com
> > wrote:
>
>> That seems to work. The only issue is that it doesn't handle values < 999
>> (that will be "0" now), but perhaps that's acceptable?
>>
>> See GelfLayout.java
>>
>> On Wed, Mar 23, 2016 at 4:37 PM, Remko Popma <re...@gmail.com>
>> wrote:
>>
>>> How about printing the long into a StringBuilder, then insert a period
>>> at StringBuilder.length() - 3?
>>>
>>> On Thu, Mar 24, 2016 at 12:13 AM, Mikael Ståldal <
>>> mikael.staldal@magine.com> wrote:
>>>
>>>> I am trying to make GelfLayout allocation free, and got stuck at
>>>> timestamp formatting. GELF need the timestamp as number of seconds with
>>>> milliseconds as decimals.
>>>>
>>>> E.g. 1458741206653 -> "1458741206.653"
>>>>
>>>> The current implementation (in GelfLayout.java) is obviously not
>>>> allocation free:
>>>>
>>>> static CharSequence formatTimestamp(final long timeMillis) {
>>>> return new BigDecimal(timeMillis).divide(TIME_DIVISOR).toPlainString();
>>>> }
>>>>
>>>> (see also unit test in GelfLayoutTest.java)
>>>>
>>>> Any ideas on how do fix this?
>>>>
>>>> --
>>>> [image: MagineTV]
>>>>
>>>> *Mikael Ståldal*
>>>> Senior software developer
>>>>
>>>> *Magine TV*
>>>> mikael.staldal@magine.com
>>>> Grev Turegatan 3 | 114 46 Stockholm, Sweden | www.magine.com
>>>>
>>>> Privileged and/or Confidential Information may be contained in this
>>>> message. If you are not the addressee indicated in this message
>>>> (or responsible for delivery of the message to such a person), you may
>>>> not copy or deliver this message to anyone. In such case,
>>>> you should destroy this message and kindly notify the sender by reply
>>>> email.
>>>>
>>>
>>>
>>
>>
>> --
>> [image: MagineTV]
>>
>> *Mikael Ståldal*
>> Senior software developer
>>
>> *Magine TV*
>> mikael.staldal@magine.com
>> Grev Turegatan 3 | 114 46 Stockholm, Sweden | www.magine.com
>>
>> Privileged and/or Confidential Information may be contained in this
>> message. If you are not the addressee indicated in this message
>> (or responsible for delivery of the message to such a person), you may
>> not copy or deliver this message to anyone. In such case,
>> you should destroy this message and kindly notify the sender by reply
>> email.
>>
>
>
Re: Allocation free timestamp formatting for GELF
Posted by Mikael Ståldal <mi...@magine.com>.
I do:
static CharSequence formatTimestamp(final long timeMillis) {
if (timeMillis < 1000) {
return "0";
} else {
StringBuilder builder = getTimestampStringBuilder();
builder.append(timeMillis);
builder.insert(builder.length() - 3, '.');
return builder;
}
}
On Wed, Mar 23, 2016 at 5:34 PM, Remko Popma <re...@gmail.com> wrote:
> In that case you may need to surround that logic with a check that the
> long value exceeds 999. Could this ever happen though?
>
> On Thu, Mar 24, 2016 at 1:18 AM, Mikael Ståldal <mikael.staldal@magine.com
> > wrote:
>
>> That seems to work. The only issue is that it doesn't handle values < 999
>> (that will be "0" now), but perhaps that's acceptable?
>>
>> See GelfLayout.java
>>
>> On Wed, Mar 23, 2016 at 4:37 PM, Remko Popma <re...@gmail.com>
>> wrote:
>>
>>> How about printing the long into a StringBuilder, then insert a period
>>> at StringBuilder.length() - 3?
>>>
>>> On Thu, Mar 24, 2016 at 12:13 AM, Mikael Ståldal <
>>> mikael.staldal@magine.com> wrote:
>>>
>>>> I am trying to make GelfLayout allocation free, and got stuck at
>>>> timestamp formatting. GELF need the timestamp as number of seconds with
>>>> milliseconds as decimals.
>>>>
>>>> E.g. 1458741206653 -> "1458741206.653"
>>>>
>>>> The current implementation (in GelfLayout.java) is obviously not
>>>> allocation free:
>>>>
>>>> static CharSequence formatTimestamp(final long timeMillis) {
>>>> return new BigDecimal(timeMillis).divide(TIME_DIVISOR).toPlainString();
>>>> }
>>>>
>>>> (see also unit test in GelfLayoutTest.java)
>>>>
>>>> Any ideas on how do fix this?
>>>>
>>>> --
>>>> [image: MagineTV]
>>>>
>>>> *Mikael Ståldal*
>>>> Senior software developer
>>>>
>>>> *Magine TV*
>>>> mikael.staldal@magine.com
>>>> Grev Turegatan 3 | 114 46 Stockholm, Sweden | www.magine.com
>>>>
>>>> Privileged and/or Confidential Information may be contained in this
>>>> message. If you are not the addressee indicated in this message
>>>> (or responsible for delivery of the message to such a person), you may
>>>> not copy or deliver this message to anyone. In such case,
>>>> you should destroy this message and kindly notify the sender by reply
>>>> email.
>>>>
>>>
>>>
>>
>>
>> --
>> [image: MagineTV]
>>
>> *Mikael Ståldal*
>> Senior software developer
>>
>> *Magine TV*
>> mikael.staldal@magine.com
>> Grev Turegatan 3 | 114 46 Stockholm, Sweden | www.magine.com
>>
>> Privileged and/or Confidential Information may be contained in this
>> message. If you are not the addressee indicated in this message
>> (or responsible for delivery of the message to such a person), you may
>> not copy or deliver this message to anyone. In such case,
>> you should destroy this message and kindly notify the sender by reply
>> email.
>>
>
>
--
[image: MagineTV]
*Mikael Ståldal*
Senior software developer
*Magine TV*
mikael.staldal@magine.com
Grev Turegatan 3 | 114 46 Stockholm, Sweden | www.magine.com
Privileged and/or Confidential Information may be contained in this
message. If you are not the addressee indicated in this message
(or responsible for delivery of the message to such a person), you may not
copy or deliver this message to anyone. In such case,
you should destroy this message and kindly notify the sender by reply
email.
Re: Allocation free timestamp formatting for GELF
Posted by Remko Popma <re...@gmail.com>.
In that case you may need to surround that logic with a check that the long
value exceeds 999. Could this ever happen though?
On Thu, Mar 24, 2016 at 1:18 AM, Mikael Ståldal <mi...@magine.com>
wrote:
> That seems to work. The only issue is that it doesn't handle values < 999
> (that will be "0" now), but perhaps that's acceptable?
>
> See GelfLayout.java
>
> On Wed, Mar 23, 2016 at 4:37 PM, Remko Popma <re...@gmail.com>
> wrote:
>
>> How about printing the long into a StringBuilder, then insert a period at
>> StringBuilder.length() - 3?
>>
>> On Thu, Mar 24, 2016 at 12:13 AM, Mikael Ståldal <
>> mikael.staldal@magine.com> wrote:
>>
>>> I am trying to make GelfLayout allocation free, and got stuck at
>>> timestamp formatting. GELF need the timestamp as number of seconds with
>>> milliseconds as decimals.
>>>
>>> E.g. 1458741206653 -> "1458741206.653"
>>>
>>> The current implementation (in GelfLayout.java) is obviously not
>>> allocation free:
>>>
>>> static CharSequence formatTimestamp(final long timeMillis) {
>>> return new BigDecimal(timeMillis).divide(TIME_DIVISOR).toPlainString();
>>> }
>>>
>>> (see also unit test in GelfLayoutTest.java)
>>>
>>> Any ideas on how do fix this?
>>>
>>> --
>>> [image: MagineTV]
>>>
>>> *Mikael Ståldal*
>>> Senior software developer
>>>
>>> *Magine TV*
>>> mikael.staldal@magine.com
>>> Grev Turegatan 3 | 114 46 Stockholm, Sweden | www.magine.com
>>>
>>> Privileged and/or Confidential Information may be contained in this
>>> message. If you are not the addressee indicated in this message
>>> (or responsible for delivery of the message to such a person), you may
>>> not copy or deliver this message to anyone. In such case,
>>> you should destroy this message and kindly notify the sender by reply
>>> email.
>>>
>>
>>
>
>
> --
> [image: MagineTV]
>
> *Mikael Ståldal*
> Senior software developer
>
> *Magine TV*
> mikael.staldal@magine.com
> Grev Turegatan 3 | 114 46 Stockholm, Sweden | www.magine.com
>
> Privileged and/or Confidential Information may be contained in this
> message. If you are not the addressee indicated in this message
> (or responsible for delivery of the message to such a person), you may not
> copy or deliver this message to anyone. In such case,
> you should destroy this message and kindly notify the sender by reply
> email.
>
Re: Allocation free timestamp formatting for GELF
Posted by Mikael Ståldal <mi...@magine.com>.
That seems to work. The only issue is that it doesn't handle values < 999
(that will be "0" now), but perhaps that's acceptable?
See GelfLayout.java
On Wed, Mar 23, 2016 at 4:37 PM, Remko Popma <re...@gmail.com> wrote:
> How about printing the long into a StringBuilder, then insert a period at
> StringBuilder.length() - 3?
>
> On Thu, Mar 24, 2016 at 12:13 AM, Mikael Ståldal <
> mikael.staldal@magine.com> wrote:
>
>> I am trying to make GelfLayout allocation free, and got stuck at
>> timestamp formatting. GELF need the timestamp as number of seconds with
>> milliseconds as decimals.
>>
>> E.g. 1458741206653 -> "1458741206.653"
>>
>> The current implementation (in GelfLayout.java) is obviously not
>> allocation free:
>>
>> static CharSequence formatTimestamp(final long timeMillis) {
>> return new BigDecimal(timeMillis).divide(TIME_DIVISOR).toPlainString();
>> }
>>
>> (see also unit test in GelfLayoutTest.java)
>>
>> Any ideas on how do fix this?
>>
>> --
>> [image: MagineTV]
>>
>> *Mikael Ståldal*
>> Senior software developer
>>
>> *Magine TV*
>> mikael.staldal@magine.com
>> Grev Turegatan 3 | 114 46 Stockholm, Sweden | www.magine.com
>>
>> Privileged and/or Confidential Information may be contained in this
>> message. If you are not the addressee indicated in this message
>> (or responsible for delivery of the message to such a person), you may
>> not copy or deliver this message to anyone. In such case,
>> you should destroy this message and kindly notify the sender by reply
>> email.
>>
>
>
--
[image: MagineTV]
*Mikael Ståldal*
Senior software developer
*Magine TV*
mikael.staldal@magine.com
Grev Turegatan 3 | 114 46 Stockholm, Sweden | www.magine.com
Privileged and/or Confidential Information may be contained in this
message. If you are not the addressee indicated in this message
(or responsible for delivery of the message to such a person), you may not
copy or deliver this message to anyone. In such case,
you should destroy this message and kindly notify the sender by reply
email.
Re: Allocation free timestamp formatting for GELF
Posted by Remko Popma <re...@gmail.com>.
How about printing the long into a StringBuilder, then insert a period at
StringBuilder.length() - 3?
On Thu, Mar 24, 2016 at 12:13 AM, Mikael Ståldal <mi...@magine.com>
wrote:
> I am trying to make GelfLayout allocation free, and got stuck at timestamp
> formatting. GELF need the timestamp as number of seconds with milliseconds
> as decimals.
>
> E.g. 1458741206653 -> "1458741206.653"
>
> The current implementation (in GelfLayout.java) is obviously not
> allocation free:
>
> static CharSequence formatTimestamp(final long timeMillis) {
> return new BigDecimal(timeMillis).divide(TIME_DIVISOR).toPlainString();
> }
>
> (see also unit test in GelfLayoutTest.java)
>
> Any ideas on how do fix this?
>
> --
> [image: MagineTV]
>
> *Mikael Ståldal*
> Senior software developer
>
> *Magine TV*
> mikael.staldal@magine.com
> Grev Turegatan 3 | 114 46 Stockholm, Sweden | www.magine.com
>
> Privileged and/or Confidential Information may be contained in this
> message. If you are not the addressee indicated in this message
> (or responsible for delivery of the message to such a person), you may not
> copy or deliver this message to anyone. In such case,
> you should destroy this message and kindly notify the sender by reply
> email.
>