[logback-user] Sequence number

David Roussel nabble at diroussel.xsmail.com
Wed Jul 3 07:38:57 CEST 2013


One LocalSequenceNumberConverter  will be created by the logging context when logback starts up. From then on it will be shared by all threads. Using an atomic long means that each log statement, regardless of thread will get a unique number. So the sequence numbers will be unique, they will not be allocated locally to the thread, but globally for that logging context. 

David

On 3 Jul 2013, at 00:03, Felipe <felipenaranja at gmail.com> wrote:

> One more question: will that sequenceNumber be handled only on the context of the initial Thread? For example: if two logging instances were running at the same time, will each of them handle its own sequentials? Any explanation on the underlying mechanics would be appreciated.
> 
> Thanks again in advance.
> 
> 
> On Tue, Jul 2, 2013 at 6:40 PM, Felipe <felipenaranja at gmail.com> wrote:
>> So many answers in so little time. Thank you very much!
>> 
>> 
>> On Tue, Jul 2, 2013 at 6:20 PM, ceki <ceki at qos.ch> wrote:
>>> Indeed, Integer.MAX_VALUE is 2^31. On a recent PC, an application doing nothing but logging, can log around 100'000 messages per second. The sequence number of such an application will reach 2^31 after about two hours of continuous logging (or approx. 200 GB of logs).
>>> 
>>> As David mentioned atomic long is probably a better alternative.
>>> 
>>> import java.util.concurrent.atomic.AtomicLong;
>>> import ch.qos.logback.classic.spi.ILoggingEvent;
>>> import ch.qos.logback.classic.pattern.ClassicConverter
>>> 
>>> public class LocalSequenceNumberConverter extends ClassicConverter {
>>> 
>>>   AtomicLong sequenceNumber = new AtomicLong(0);
>>> 
>>> 
>>>   @Override
>>>   public String convert(ILoggingEvent event) {
>>>     return Long.toString(sequenceNumber.getAndIncrement());
>>>   }
>>> }
>>> 
>>> A volatile long is not guaranteed to work properly because incrementing a long is not atomic whereas incrementing a int is.
>>> 
>>> 
>>> 
>>> On 03.07.2013 00:01, David Roussel wrote:
>>>> Or use AtomicLong.
>>>> 
>>>> David
>>>> 
>>>> On 2 Jul 2013, at 22:50, ceki <ceki at qos.ch> wrote:
>>>> 
>>>>> 
>>>>> In your custom converter, declare a sequenceNumber as follows:
>>>>> 
>>>>> *volatile* int sequenceNumber = 0;
>>>>> 
>>>>> increment sequenceNumber for each new event:
>>>>> 
>>>>> sequenceNumber++;
>>>>> 
>>>>> That's it.
>>>>> 
>>>>> On 02.07.2013 23:21, Felipe wrote:
>>>>>> Hi everyone:
>>>>>> 
>>>>>> I'm writing to all of you because I want to display a sequence number
>>>>>> for each event. I've done some research regarding this and I know for
>>>>>> sure that LogBack is not able to natively handle this as Log4j2 does
>>>>>> with %sn, or as JUL partially does with getSequenceNumber() (I won't be
>>>>>> changing LogBack because of this, anyway).
>>>>>> 
>>>>>> I know for sure LogBack is lacking this functionality because of this
>>>>>> issue on JIRA:
>>>>>> 
>>>>>> http://jira.qos.ch/browse/LOGBACK-546
>>>>>> 
>>>>>> That issue is a four years old boy now.
>>>>>> 
>>>>>> I found a link on Stack Overflow
>>>>>> (http://stackoverflow.com/questions/11162951/line-numbers-with-logback)
>>>>>> which lead me to the following link:
>>>>>> 
>>>>>> http://logback.qos.ch/manual/layouts.html#customConversionSpecifier
>>>>>> 
>>>>>> I tried a static number but it was sred across threads so it didn't
>>>>>> 
>>>>>> work that well. I also thought of ThreadLocal but I read a story which
>>>>>> freaked me out:
>>>>>> 
>>>>>> http://niklasschlimm.blogspot.com/2012/04/threading-stories-threadlocal-in-web.html
>>>>>> 
>>>>>> Does anyone know of a good way to achieve sequentials?
>>>>>> 
>>>>>> Best regards and thanks in advance.
>>>>>> 
>>>>>> --
>>>>>> Felipe
>>> 
>>> 
>>> -- 
>>> Ceki
>>> 65% of statistics are made up on the spot
>>> _______________________________________________
>>> Logback-user mailing list
>>> Logback-user at qos.ch
>>> http://mailman.qos.ch/mailman/listinfo/logback-user
>> 
>> 
>> 
>> -- 
>> Felipe
> 
> 
> 
> -- 
> Felipe
> _______________________________________________
> Logback-user mailing list
> Logback-user at qos.ch
> http://mailman.qos.ch/mailman/listinfo/logback-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20130703/47252e8f/attachment.html>


More information about the Logback-user mailing list