Coverage Report

Created: 2025-08-24 06:21

/src/logging-log4cxx/src/main/include/log4cxx/net/xmlsocketappender.h
Line
Count
Source (jump to first uncovered line)
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
18
#ifndef _LOG4CXX_NET_XML_SOCKET_APPENDER_H
19
#define _LOG4CXX_NET_XML_SOCKET_APPENDER_H
20
21
#include <log4cxx/net/socketappenderskeleton.h>
22
#include <log4cxx/helpers/writer.h>
23
24
namespace LOG4CXX_NS
25
{
26
namespace net
27
{
28
29
/**
30
Sends spi::LoggingEvent elements
31
to a remote a log server, usually in XML format.
32
33
Here is an example configuration that writes JSON to the
34
<a href="https://docs.fluentbit.io/manual/pipeline/inputs/tcp">TCP input plugin of a fluent-bit log server</a>
35
running on the same system as the application:
36
~~~{.xml}
37
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
38
<appender name="A1" class="XMLSocketAppender">
39
  <param name="RemoteHost" value="localhost" />
40
  <param name="Port"       value="5170" />
41
  <layout class="JSONLayout"/>
42
</appender>
43
<root>
44
  <priority value ="INFO" />
45
  <appender-ref ref="A1" />
46
</root>
47
</log4j:configuration>
48
~~~
49
50
<p>XMLSocketAppender has the following properties:
51
52
- The event will be logged with the same time stamp,
53
NDC, location info as if it were logged locally by
54
the client.
55
56
- Remote logging uses the TCP protocol. Consequently, if
57
the server is reachable, then log events will eventually arrive
58
at the server.
59
60
- If the remote server is down, the logging requests are
61
simply dropped. However, if and when the server comes back up,
62
then event transmission is resumed transparently. This
63
transparent reconneciton is performed by a <em>connector</em>
64
thread which periodically attempts to connect to the server.
65
66
- Logging events are automatically <em>buffered</em> by the
67
native TCP implementation. This means that if the link to server
68
is slow but still faster than the rate of (log) event production
69
by the client, the client will not be affected by the slow
70
network connection. However, if the network connection is slower
71
then the rate of event production, then the client can only
72
progress at the network rate. In particular, if the network link
73
to the the server is down, the client will be blocked.
74
@n @n On the other hand, if the network link is up, but the server
75
is down, the client will not be blocked when making log requests
76
but the log events will be lost due to server unavailability.
77
78
- Even if an <code>XMLSocketAppender</code> is no longer
79
attached to any logger, it will not be destroyed in
80
the presence of a connector thread. A connector thread exists
81
only if the connection to the server is down. To avoid this
82
destruction problem, you should #close the the
83
<code>XMLSocketAppender</code> explicitly. See also next item.
84
@n @n Long lived applications which create/destroy many
85
<code>XMLSocketAppender</code> instances should be aware of this
86
destruction problem. Most other applications can safely
87
ignore it.
88
89
- If the application hosting the <code>XMLSocketAppender</code>
90
exits before the <code>XMLSocketAppender</code> is closed either
91
explicitly or subsequent to destruction, then there might
92
be untransmitted data in the pipe which might be lost.
93
@n @n To avoid lost data, it is usually sufficient to
94
#close the <code>XMLSocketAppender</code> either explicitly or by
95
calling the LogManager#shutdown method
96
before exiting the application.
97
*/
98
99
class LOG4CXX_EXPORT XMLSocketAppender : public SocketAppenderSkeleton
100
{
101
  public:
102
    /**
103
    The default port number of remote logging server (4560).
104
    */
105
    static int DEFAULT_PORT;
106
107
    /**
108
    The default reconnection delay (30000 milliseconds or 30 seconds).
109
    */
110
    static int DEFAULT_RECONNECTION_DELAY;
111
112
    /**
113
    Unused
114
    */
115
    static const int MAX_EVENT_LEN;
116
117
    DECLARE_LOG4CXX_OBJECT(XMLSocketAppender)
118
0
    BEGIN_LOG4CXX_CAST_MAP()
119
0
    LOG4CXX_CAST_ENTRY(XMLSocketAppender)
120
0
    LOG4CXX_CAST_ENTRY_CHAIN(AppenderSkeleton)
121
0
    END_LOG4CXX_CAST_MAP()
122
123
    XMLSocketAppender();
124
    ~XMLSocketAppender();
125
126
    /**
127
    Connects to remote server at <code>address</code> and <code>port</code>.
128
    */
129
    XMLSocketAppender(helpers::InetAddressPtr address, int port);
130
131
    /**
132
    Connects to remote server at <code>host</code> and <code>port</code>.
133
    */
134
    XMLSocketAppender(const LogString& host, int port);
135
136
137
  protected:
138
    void setSocket(LOG4CXX_NS::helpers::SocketPtr& socket, helpers::Pool& p) override;
139
140
    void cleanUp(helpers::Pool& p) override;
141
142
    int getDefaultDelay() const override;
143
144
    int getDefaultPort() const override;
145
146
    void append(const spi::LoggingEventPtr& event, helpers::Pool& pool) override;
147
148
  private:
149
    //  prevent copy and assignment statements
150
    XMLSocketAppender(const XMLSocketAppender&);
151
    XMLSocketAppender& operator=(const XMLSocketAppender&);
152
153
    struct XMLSocketAppenderPriv;
154
}; // class XMLSocketAppender
155
156
LOG4CXX_PTR_DEF(XMLSocketAppender);
157
158
} // namespace net
159
} // namespace log4cxx
160
161
#endif // _LOG4CXX_NET_XML_SOCKET_APPENDER_H
162