<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><span style="-webkit-text-size-adjust: auto; background-color: rgba(255, 255, 255, 0);">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. <br></span><br><span style="-webkit-text-size-adjust: auto;">David</span></div><div style="-webkit-text-size-adjust: auto; "><br>On 3 Jul 2013, at 00:03, Felipe <<a href="mailto:felipenaranja@gmail.com">felipenaranja@gmail.com</a>> wrote:<br><br></div><blockquote type="cite" style="-webkit-text-size-adjust: auto; "><div><div dir="ltr">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.<div>
<br></div><div style="">Thanks again in advance.</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Jul 2, 2013 at 6:40 PM, Felipe <span dir="ltr"><<a href="mailto:felipenaranja@gmail.com" target="_blank">felipenaranja@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">So many answers in so little time. Thank you very much!</div><div class="gmail_extra"><div><div class="h5">
<br><br><div class="gmail_quote">On Tue, Jul 2, 2013 at 6:20 PM, ceki <span dir="ltr"><<a href="mailto:ceki@qos.ch" target="_blank">ceki@qos.ch</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">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).<br>


<br>
As David mentioned atomic long is probably a better alternative.<br>
<br>
import java.util.concurrent.atomic.<u></u>AtomicLong;<br>
import ch.qos.logback.classic.spi.<u></u>ILoggingEvent;<br>
import ch.qos.logback.classic.<u></u>pattern.ClassicConverter<br>
<br>
public class LocalSequenceNumberConverter extends ClassicConverter {<br>
<br>
  AtomicLong sequenceNumber = new AtomicLong(0);<div><br>
<br>
  @Override<br>
  public String convert(ILoggingEvent event) {<br></div>
    return Long.toString(sequenceNumber.<u></u>getAndIncrement());<br>
  }<br>
}<br>
<br>
A volatile long is not guaranteed to work properly because incrementing a long is not atomic whereas incrementing a int is.<div><br>
<br>
<br>
On 03.07.2013 00:01, David Roussel wrote:<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>
Or use AtomicLong.<br>
<br>
David<br>
<br>
On 2 Jul 2013, at 22:50, ceki <<a href="mailto:ceki@qos.ch" target="_blank">ceki@qos.ch</a>> wrote:<br>
<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>
<br>
In your custom converter, declare a sequenceNumber as follows:<br>
<br>
*volatile* int sequenceNumber = 0;<br>
<br>
increment sequenceNumber for each new event:<br>
<br>
sequenceNumber++;<br>
<br>
That's it.<br>
<br>
On 02.07.2013 23:21, Felipe wrote:<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>
Hi everyone:<br>
<br>
I'm writing to all of you because I want to display a sequence number<br>
for each event. I've done some research regarding this and I know for<br>
sure that LogBack is not able to natively handle this as Log4j2 does<br>
with %sn, or as JUL partially does with getSequenceNumber() (I won't be<br>
changing LogBack because of this, anyway).<br>
<br>
I know for sure LogBack is lacking this functionality because of this<br>
issue on JIRA:<br>
<br>
<a href="http://jira.qos.ch/browse/LOGBACK-546" target="_blank">http://jira.qos.ch/browse/<u></u>LOGBACK-546</a><br>
<br>
That issue is a four years old boy now.<br>
<br>
I found a link on Stack Overflow<br>
(<a href="http://stackoverflow.com/questions/11162951/line-numbers-with-logback" target="_blank">http://stackoverflow.com/<u></u>questions/11162951/line-<u></u>numbers-with-logback</a>)<br>
which lead me to the following link:<br>
<br>
<a href="http://logback.qos.ch/manual/layouts.html#customConversionSpecifier" target="_blank">http://logback.qos.ch/manual/<u></u>layouts.html#<u></u>customConversionSpecifier</a><br>
<br></div>
I tried a static number but it was sred across threads so it didn't<div><br>
work that well. I also thought of ThreadLocal but I read a story which<br>
freaked me out:<br>
<br>
<a href="http://niklasschlimm.blogspot.com/2012/04/threading-stories-threadlocal-in-web.html" target="_blank">http://niklasschlimm.blogspot.<u></u>com/2012/04/threading-stories-<u></u>threadlocal-in-web.html</a><br>
<br>
Does anyone know of a good way to achieve sequentials?<br>
<br>
Best regards and thanks in advance.<br>
<br>
--<br>
Felipe<br>
<br>
<br>
</div></blockquote></blockquote></blockquote><div><div>
<br>
<br>
-- <br>
Ceki<br>
65% of statistics are made up on the spot<br>
______________________________<u></u>_________________<br>
Logback-user mailing list<br>
<a href="mailto:Logback-user@qos.ch" target="_blank">Logback-user@qos.ch</a><br>
<a href="http://mailman.qos.ch/mailman/listinfo/logback-user" target="_blank">http://mailman.qos.ch/mailman/<u></u>listinfo/logback-user</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div></div></div><span class="HOEnZb"><font color="#888888">-- <br>Felipe
</font></span></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>Felipe
</div>
</div></blockquote><blockquote type="cite" style="-webkit-text-size-adjust: auto; "><div><span>_______________________________________________</span><br><span>Logback-user mailing list</span><br><span><a href="mailto:Logback-user@qos.ch">Logback-user@qos.ch</a></span><br><span><a href="http://mailman.qos.ch/mailman/listinfo/logback-user">http://mailman.qos.ch/mailman/listinfo/logback-user</a></span></div></blockquote></body></html>