| 1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| 2 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 3 |
<html xmlns="http://www.w3.org/1999/xhtml"> |
| 4 |
<head> |
| 5 |
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> |
| 6 |
<title>Chapter 5: Encoders</title> |
| 7 |
<link rel="stylesheet" type="text/css" href="../css/common.css" /> |
| 8 |
<link rel="stylesheet" type="text/css" href="../css/screen.css" media="screen" /> |
| 9 |
<link rel="stylesheet" type="text/css" href="../css/_print.css" media="print" /> |
| 10 |
<link rel="stylesheet" type="text/css" href="../css/prettify.css" media="screen" /> |
| 11 |
</head> |
| 12 |
<body onload="prettyPrint(); decorate();"> |
| 13 |
<script type="text/javascript">prefix='../';</script> |
| 14 |
<script type="text/javascript" src="../js/prettify.js"></script> |
| 15 |
<script type="text/javascript" src="../templates/header.js" ></script> |
| 16 |
<script type="text/javascript" src="../js/jquery-min.js"></script> |
| 17 |
<script type="text/javascript" src="../js/decorator.js"></script> |
| 18 |
<div id="left"> |
| 19 |
<noscript>Please turn on Javascript to view this menu</noscript> |
| 20 |
<script src="../templates/left.js" type="text/javascript"></script> |
| 21 |
</div> |
| 22 |
<div id="right"> |
| 23 |
<script src="menu.js" type="text/javascript"></script> |
| 24 |
</div> |
| 25 |
|
| 26 |
<div id="content"> |
| 27 |
|
| 28 |
<h1>Chapter 5: Encoders</h1> |
| 29 |
|
| 30 |
<a href="encoders_ja.html">和訳 (Japanese translation)</a> |
| 31 |
|
| 32 |
<div class="quote"> |
| 33 |
<p><b>ACTION THIS DAY</b> Make sure they have all they want on |
| 34 |
extreme priority and report to me that this has been done. |
| 35 |
</p> |
| 36 |
<p>—CHURCHILL on October 1941 to General Hastings Ismay in |
| 37 |
response to a request for more resources signed by Alan Turing |
| 38 |
and his cryptanalyst colleagues at Bletchley Park</p> |
| 39 |
</div> |
| 40 |
|
| 41 |
<script src="../templates/creative.js" type="text/javascript"></script> |
| 42 |
<script src="../templates/setup.js" type="text/javascript"></script> |
| 43 |
|
| 44 |
|
| 45 |
<h2 class="doAnchor">What is an encoder</h2> |
| 46 |
|
| 47 |
<p>Encoders are responsible for transforming an event into a byte |
| 48 |
array as well as writing out that byte array into an |
| 49 |
<code>OutputStream</code>. Encoders were introduced in logback |
| 50 |
version 0.9.19. In previous versions, most appenders relied on a |
| 51 |
layout to transform an event into a string and write it out using |
| 52 |
a <code>java.io.Writer</code>. In previous versions of logback, |
| 53 |
users would nest a <code>PatternLayout</code> within |
| 54 |
<code>FileAppender</code>. Since logback 0.9.19, |
| 55 |
<code>FileAppender</code> and sub-classes <a |
| 56 |
href="../codes.html#layoutInsteadOfEncoder">expect an encoder and no |
| 57 |
longer take a layout</a>. |
| 58 |
</p> |
| 59 |
|
| 60 |
<p>Why the breaking change?</p> |
| 61 |
|
| 62 |
<p>Layouts, as discussed in detail in the next chapter, are only |
| 63 |
able to transform an event into a String. Moreover, given that a |
| 64 |
layout has no control over when events get written out, layouts |
| 65 |
cannot aggregate events into batches. Contrast this with encoders |
| 66 |
which not only have total control over the format of the bytes |
| 67 |
written out, but also control when (and if) those bytes get written |
| 68 |
out. |
| 69 |
</p> |
| 70 |
|
| 71 |
<p>At the present time, <code>PatternLayoutEncoder</code> is the |
| 72 |
only really useful encoder. It merely wraps a |
| 73 |
<code>PatternLayout</code> which does most of the work. Thus, it |
| 74 |
may seem that encoders do not bring much to the table except |
| 75 |
needless complexity. However, we hope that with the advent of new |
| 76 |
and powerful encoders this impression will change.</p> |
| 77 |
|
| 78 |
<h2 class="doAnchor" name="interface">Encoder interface</h2> |
| 79 |
|
| 80 |
<p>Encoders are responsible for transforming an incoming event |
| 81 |
into a byte array <b>and</b> writing out the resulting byte array |
| 82 |
onto the appropriate <code>OutputStream</code>. Thus, encoders |
| 83 |
have total control of what and when bytes gets written to the |
| 84 |
<code>OutputStream</code> maintained by the owning appender. Here |
| 85 |
is the <a |
| 86 |
href="../xref/ch/qos/logback/core/encoder/Encoder.html">Encoder |
| 87 |
interface:</a> |
| 88 |
|
| 89 |
</p> |
| 90 |
<pre class="prettyprint source">package ch.qos.logback.core.encoder; |
| 91 |
|
| 92 |
public interface Encoder<E> extends ContextAware, LifeCycle { |
| 93 |
|
| 94 |
/** |
| 95 |
* This method is called when the owning appender starts or whenever output |
| 96 |
* needs to be directed to a new OutputStream, for instance as a result of a |
| 97 |
* rollover. |
| 98 |
*/ |
| 99 |
void init(OutputStream os) throws IOException; |
| 100 |
|
| 101 |
/** |
| 102 |
* Encode and write an event to the appropriate {@link OutputStream}. |
| 103 |
* Implementations are free to defer writing out of the encoded event and |
| 104 |
* instead write in batches. |
| 105 |
*/ |
| 106 |
void doEncode(E event) throws IOException; |
| 107 |
|
| 108 |
|
| 109 |
/** |
| 110 |
* This method is called prior to the closing of the underling |
| 111 |
* {@link OutputStream}. Implementations MUST not close the underlying |
| 112 |
* {@link OutputStream} which is the responsibility of the owning appender. |
| 113 |
*/ |
| 114 |
void close() throws IOException; |
| 115 |
}</pre> |
| 116 |
|
| 117 |
<p>As you can see, the <code>Encoder</code> interface consists of |
| 118 |
few methods, but surprisingly many useful things can be |
| 119 |
accomplished with these methods. |
| 120 |
</p> |
| 121 |
|
| 122 |
|
| 123 |
<h2 class="doAnchor">LayoutWrappingEncoder</h2> |
| 124 |
|
| 125 |
<p>Until logback version 0.9.19, many appenders relied on the |
| 126 |
Layout instances to control the format of log output. As there |
| 127 |
exists substantial amount of code based on the layout interface, |
| 128 |
we needed a way for encoders to inter-operate with layouts. <a |
| 129 |
href="../xref/ch/qos/logback/core/encoder/LayoutWrappingEncoder.html">LayoutWrappingEncoder</a> |
| 130 |
bridges the gap between encoders and layouts. It implements the |
| 131 |
encoder interface and wraps a layout to which it delegates the |
| 132 |
work of transforming an event into string. |
| 133 |
</p> |
| 134 |
|
| 135 |
<p>Below is an excerpt from the <code>LayoutWrappingEncoder</code> |
| 136 |
class illustrating how delegation to the wrapped layout instance |
| 137 |
is done.</p> |
| 138 |
|
| 139 |
<pre class="prettyprint source">package ch.qos.logback.core.encoder; |
| 140 |
|
| 141 |
public class LayoutWrappingEncoder<E> extends EncoderBase<E> { |
| 142 |
|
| 143 |
protected Layout<E> layout; |
| 144 |
private Charset charset; |
| 145 |
|
| 146 |
// encode a given event as a byte[] |
| 147 |
public byte[] encode(E event) { |
| 148 |
String txt = layout.doLayout(event); |
| 149 |
return convertToBytes(txt); |
| 150 |
} |
| 151 |
|
| 152 |
private byte[] convertToBytes(String s) { |
| 153 |
if (charset == null) { |
| 154 |
return s.getBytes(); |
| 155 |
} else { |
| 156 |
return s.getBytes(charset); |
| 157 |
} |
| 158 |
} |
| 159 |
}</pre> |
| 160 |
|
| 161 |
<p>The <code>doEncode</code>() method starts by having the wrapped |
| 162 |
layout convert the incoming event into string. The resulting text |
| 163 |
string is converted to bytes according to the charset encoding |
| 164 |
chosen by the user.</p> |
| 165 |
|
| 166 |
|
| 167 |
<h2 class="doAnchor">PatternLayoutEncoder</h2> |
| 168 |
|
| 169 |
<p>Given that <code>PatternLayout</code> is the most commonly used |
| 170 |
layout, logback caters for this common use-case with |
| 171 |
<code>PatternLayoutEncoder</code>, an extension of |
| 172 |
<code>LayoutWrappingEncoder</code> restricted to wrapping |
| 173 |
instances of <code>PatternLayout</code>. |
| 174 |
</p> |
| 175 |
|
| 176 |
<p>As of logback version 0.9.19, whenever a |
| 177 |
<code>FileAppender</code> or one of its sub-classes was configured |
| 178 |
with a <code>PatternLayout</code>, a |
| 179 |
<code>PatternLayoutEncoder</code> must be used instead. This is |
| 180 |
explained in the <a |
| 181 |
href="../codes.html#layoutInsteadOfEncoder">relevant entry in the |
| 182 |
logback error codes</a>. |
| 183 |
</p> |
| 184 |
|
| 185 |
<h4 class="doAnchor" name="immediateFlush"><span class="prop">immediateFlush</span> property</h4> |
| 186 |
|
| 187 |
<p>As of <span class="label notice">logback 1.2.0</span>, the |
| 188 |
<span class="prop">immediateFlush</span> property is part of the |
| 189 |
enclosing Appender.</p> |
| 190 |
|
| 191 |
<h4 class="doAnchor" name="outputPatternAsHeader">Output pattern |
| 192 |
string as header</h4> |
| 193 |
|
| 194 |
<p>In order to facilitate parsing of log files, logback can insert |
| 195 |
the pattern used for the log output at the top of log files. This |
| 196 |
feature is <b>disabled</b> by default. It can be enabled by |
| 197 |
setting the <span class="prop">outputPatternAsHeader</span> |
| 198 |
property to 'true' for relevant |
| 199 |
<code>PatternLayoutEncoder</code>. Here is an example:</p> |
| 200 |
|
| 201 |
<pre class="prettyprint"><appender name="FILE" class="ch.qos.logback.core.FileAppender"> |
| 202 |
<file>foo.log</file> |
| 203 |
<encoder> |
| 204 |
<pattern>%d %-5level [%thread] %logger{0}: %msg%n</pattern> |
| 205 |
<b><outputPatternAsHeader>true</outputPatternAsHeader></b> |
| 206 |
</encoder> |
| 207 |
</appender></pre> |
| 208 |
|
| 209 |
<p>This will result output akin to the following in the log file:</p> |
| 210 |
|
| 211 |
<pre>#logback.classic pattern: %d [%thread] %-5level %logger{36} - %msg%n |
| 212 |
2012-04-26 14:54:38,461 [main] DEBUG com.foo.App - Hello world |
| 213 |
2012-04-26 14:54:38,461 [main] DEBUG com.foo.App - Hi again |
| 214 |
...</pre> |
| 215 |
|
| 216 |
<p>The line starting with "#logback.classic pattern" is newly |
| 217 |
inserted pattern line.</p> |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
<script src="../templates/footer.js" type="text/javascript"></script> |
| 223 |
|
| 224 |
</div> |
| 225 |
</body> |
| 226 |
</html> |