1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache license, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the license for the specific language governing permissions and
15 * limitations under the license.
16 */
17 package org.apache.logging.log4j.core.appender;
18
19 import java.util.concurrent.TimeUnit;
20 import java.util.concurrent.locks.Lock;
21 import java.util.concurrent.locks.ReadWriteLock;
22 import java.util.concurrent.locks.ReentrantReadWriteLock;
23
24 import org.apache.logging.log4j.core.Filter;
25 import org.apache.logging.log4j.core.LogEvent;
26 import org.apache.logging.log4j.core.StringLayout;
27 import org.apache.logging.log4j.core.config.Property;
28
29 /**
30 * Appends log events as strings to a writer.
31 *
32 * @param <M>
33 * The kind of {@link WriterManager} under management
34 */
35 public abstract class AbstractWriterAppender<M extends WriterManager> extends AbstractAppender {
36
37 /**
38 * Immediate flush means that the underlying writer will be flushed at the
39 * end of each append operation. Immediate flush is slower but ensures that
40 * each append request is actually written. If <code>immediateFlush</code>
41 * is set to {@code false}, then there is a good chance that the last few
42 * logs events are not actually written to persistent media if and when the
43 * application crashes.
44 */
45 protected final boolean immediateFlush;
46 private final M manager;
47 private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
48 private final Lock readLock = readWriteLock.readLock();
49
50 /**
51 * Instantiates.
52 *
53 * @param name
54 * The name of the Appender.
55 * @param layout
56 * The layout to format the message.
57 * @param properties
58 * Optional properties.
59 * @param manager
60 * The OutputStreamManager.
61 */
62 protected AbstractWriterAppender(final String name, final StringLayout layout, final Filter filter,
63 final boolean ignoreExceptions, final boolean immediateFlush, final Property[] properties, final M manager) {
64 super(name, filter, layout, ignoreExceptions, properties);
65 this.manager = manager;
66 this.immediateFlush = immediateFlush;
67 }
68
69 /**
70 * Instantiates.
71 *
72 * @param name
73 * The name of the Appender.
74 * @param layout
75 * The layout to format the message.
76 * @param manager
77 * The OutputStreamManager.
78 * @deprecated Use {@link #AbstractWriterAppender(String, StringLayout, Filter, boolean, boolean, Property[], WriterManager)}.
79 */
80 @Deprecated
81 protected AbstractWriterAppender(final String name, final StringLayout layout, final Filter filter,
82 final boolean ignoreExceptions, final boolean immediateFlush, final M manager) {
83 super(name, filter, layout, ignoreExceptions, Property.EMPTY_ARRAY);
84 this.manager = manager;
85 this.immediateFlush = immediateFlush;
86 }
87
88 /**
89 * Actual writing occurs here.
90 * <p>
91 * Most subclasses will need to override this method.
92 * </p>
93 *
94 * @param event
95 * The LogEvent.
96 */
97 @Override
98 public void append(final LogEvent event) {
99 readLock.lock();
100 try {
101 final String str = getStringLayout().toSerializable(event);
102 if (str.length() > 0) {
103 manager.write(str);
104 if (this.immediateFlush || event.isEndOfBatch()) {
105 manager.flush();
106 }
107 }
108 } catch (final AppenderLoggingException ex) {
109 error("Unable to write " + manager.getName() + " for appender " + getName(), event, ex);
110 throw ex;
111 } finally {
112 readLock.unlock();
113 }
114 }
115
116 /**
117 * Gets the manager.
118 *
119 * @return the manager.
120 */
121 public M getManager() {
122 return manager;
123 }
124
125 public StringLayout getStringLayout() {
126 return (StringLayout) getLayout();
127 }
128
129 @Override
130 public void start() {
131 if (getLayout() == null) {
132 LOGGER.error("No layout set for the appender named [{}].", getName());
133 }
134 if (manager == null) {
135 LOGGER.error("No OutputStreamManager set for the appender named [{}].", getName());
136 }
137 super.start();
138 }
139
140 @Override
141 public boolean stop(final long timeout, final TimeUnit timeUnit) {
142 setStopping();
143 boolean stopped = super.stop(timeout, timeUnit, false);
144 stopped &= manager.stop(timeout, timeUnit);
145 setStopped();
146 return stopped;
147 }
148 }