Read Logback Source series article (v)--appender--Reprint

Source: Internet
Author: User

Original address: http://kyfxbl.iteye.com/blog/1173788

Tomorrow to take a wife abroad to travel for a few days, so this period of time is not updated blog, before leaving before the last Post

In the previous article we talked about the logger class of the info () method through layer calls, and finally commissioned Appender to record the log, this blog we will continue to say, appender component is how to log

In fact Appender may be one of the most important components of the logback framework, although logger is the interface for logging, but if a logger is not associated with any appender, then this logger cannot log any information. In addition, although Logback provides many extensibility points, in applications, we may rarely extend the filter, and rarely extend layout and encoder, but our chances of expanding Appender are many

The old ritual, first of all, look at the big picture of Appender, here to show that the implementation of the Appender interface has 2 base classes, one is Appenderbase, the other is Unsynchronizedappenderbase, these 2 classes very close, 80 The code above is the same. If we're going to customize appender ourselves, just write a class that inherits from the 2 base classes.



First, there is a Appender interface, and then, as stated above, the Unsynchronizedappenderbase class implements this interface, but it is an abstract class itself, and it needs to inherit it to get the real implementation class. The Appender interface inherits the Filterattachable interface, and the Unsynchronizedappenderbase class holds a Filterattachableimpl class, Delegate this class to implement the method defined in the Filterattachable interface

Then Outputstreamappender is inherited from the Unsynchronizedappenderbase Appender implementation class, although it is not an abstract class, but the actual is not directly used, Its implementation class is the most common Consoleappender and Fileappender.

As long as you have used logback friends know, Appender element also need to configure encoder element, where the encoder interface is corresponding to this encoder element, because in fact, appender component is not the final actual log information components, It wants to delegate the encoder component to complete loggingevent formatting and logging

After describing the general structure, let's take a look at the Doappend () method from the Appender interface, which is how to finally record the log in a step-by-step manner.

The first is the Doappend () method inside the Unsynchronizedappenderbase, which mainly records the status state, then checks whether the filter on the Appender satisfies the filtering condition, and then calls the Appender () that implements the subclass. Method. It looks familiar, doesn't it? A design pattern-template method

Java code
  1. Public void Doappend (E eventobject) {
  2. //warning:the guard Check must is the first statement in the
  3. //Doappend () method.
  4. //Prevent re-entry.
  5. if (Boolean.TRUE.equals (Guard.get ())) {
  6. return;
  7. }
  8. try {
  9. Guard.set (boolean.true);
  10. if (! this.started) {
  11. if (statusrepeatcount++ < allowed_repeats) {
  12. Addstatus (new Warnstatus (
  13. "attempted to append to non started appender [" + Name + "].",
  14. this ));
  15. }
  16. return;
  17. }
  18. if (getfilterchaindecision (eventobject) = = Filterreply.deny) {
  19. return;
  20. }
  21. //OK, we now invoke derived class ' implementation of append
  22. this.append (EventObject);
  23. } catch (Exception e) {
  24. if (exceptioncount++ < allowed_repeats) {
  25. Adderror ("Appender [" + Name + "] failed to append.", e);
  26. }
  27. } finally {
  28. Guard.set (Boolean.false);
  29. }
  30. }
  31. abstract protected void append (E eventobject);


The above code is very simple, needless to say, we just look at the implementation of the class append () method is how to implement, here we choose Outputstreamappender Implementation class

Java code
    1. @Override
    2. protected void append (E eventobject) {
    3. if (!isstarted ()) {
    4. return;
    5. }
    6. Subappend (EventObject);
    7. }


First check whether this appender has been started, if not started directly back, if it has been started, then into a subappend () method

Java code
  1. /**
  2. * Actual writing occurs here.
  3. * <p>
  4. * Most subclasses of <code>WriterAppender</code> would need to override this
  5. * method.
  6. *
  7. * @since 0.9.0
  8. */
  9. protected void Subappend (E event) {
  10. if (!isstarted ()) {
  11. return;
  12. }
  13. try {
  14. //This step avoids LBCLASSIC-139
  15. if (event instanceof deferredprocessingaware) {
  16. ((Deferredprocessingaware) event). Preparefordeferredprocessing ();
  17. }
  18. //The synchronization prevents the OutputStream from being closed while we
  19. //Is writing. It also prevents multiple thread from entering the same
  20. //Converter.  Converters assume that they is in a synchronized block.
  21. synchronized (lock) {
  22. Writeout (event);
  23. }
  24. } catch (IOException IoE) {
  25. //As soon as an exception occurs, move to non-started state
  26. //And add a single errorstatus to the SM.
  27. this.started = false;
  28. Addstatus (new ErrorStatus ("IO failure in Appender", this , IoE));
  29. }
  30. }


This method actually does not do anything. After doing some checks, and then into the Writeout () method ...

Java code
    1. protected void Writeout (E event) throws IOException {
    2. This.encoder.doEncode (event);
    3. }


The Writeout () method delegate is configured to its encoder component to record

Java code
    1. Public void Doencode (E event) throws IOException {
    2. String txt = layout.dolayout (event);
    3. Outputstream.write (converttobytes (TXT));
    4. Outputstream.flush ();
    5. }


Come here, finally finished. The encoder component also delegates its layout component to format the Loggingevent, returns a string, and then writes the formatted log information to the destination through the Outputstream.write () method.

Patience to see the friends here, may have been a bit dizzy, how appender need such trouble? What we're talking about here is just Consoleappender's doappend () whole process, not all appender are so complicated, and of course there are some more complex.

Here's a simple appender, which I wrote myself Myappender

Java code
    1. public class myappender  EXTENDS&NBSP;APPENDERBASE<LOGGINGEVENT>&NBSP;{&NBSP;&NBSP;
    2.   
    3.      @Override   
    4.     protected void  Append (Loggingevent eventobject)  {  
    5.          system.out.println (Eventobject.getmessage ());   
    6.     }   
    7.   
    8. }  


Well, very simple is not, if the Appender configuration to Logback.xml, then when the Logger.info () call, you will first walk into the Appenderbase class of Doappend () method, filter check and so on, Then enter the Myappender append () method, do not do other operations, directly to print the message to the console. Of course, because this class is extremely simplified, there is no encoder and layout, there is no way to control the output of the log time, there is no way to%thread such as the resolution processing. But this class can clearly express how the Appender component works: By invoking the filter chain by the Appenderbase class, and then by Appender implementing the class to delegate encoder parse Loggingevent, and then output to the destination

This is the end of the blog post here. So far, 5 blog posts have been:
1, first introduce how logback and slf4j docking
2, then introduce Logback's loggerfactory, that is, how Loggercontext was created
3, Next introduction loggerfactory how to create Logger
4. Then logger how to log, which involves cascading calls to Appender, and calling Turbofilter to filter the issue
5, this blog and the most common consoleappender as an example, introduced the Appender component how to output log information to the destination

The next blog topic has a variety of branches, you can talk about Dbappender and Fileappender is how to log, as this blog supplement, deepen understanding; You can continue to go further and talk about encoder and layout components , or go back and introduce how Logback was initialized.

When I accompany my wife to travel back, and then continue to update this series

Read Logback Source series article (v)--appender--Reprint

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.