Available in Classic and VPC
In Log4j2 SDK v2, it describes how to use Log4j2 v2, which is provided by Effective Log Search & Analytics 2.0 service.
The following describes how to use SDK:
1. Preparation
2. Add dependency
3. Set basic appender
4. Set AsyncAppender
5. Use SDK
1. Preparation
The following describes the preparations for installing Log4j2 SDK:
- In the NAVER Cloud Platform's console, Create project.
- Check the txtToken value in Project details page.
- Download Log4j2 SDK.
2. Add dependency
To install Log4j2 SDK v2:
- Unzip the downloaded SDK file.
- Add dependency to the pom.xml file as follows:
- Add directories of core module (nelo2-java-sdk-core-1.6.6.jar) and Log4j2 module (nelo2-java-sdk-log4j2-2.8.5.jar) to
<systemPath>.
<dependencies> <dependency> <groupId>nelo2-java-sdk-core</groupId> <artifactId>nelo2-java-sdk-core</artifactId> <version>1.6.6</version> <scope>system</scope> <systemPath>/nelo2-java-sdk-core-1.6.6.jar</systemPath> </dependency> <dependency> <groupId>nelo2-java-sdk</groupId> <artifactId>nelo2-java-sdk-log4j2</artifactId> <version>2.8.5</version> <scope>system</scope> <systemPath>/nelo2-java-sdk-log4j2-2.8.5.jar</systemPath> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>org.apache.thrift</groupId> <artifactId>libthrift</artifactId> <version>0.9.3</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.2.6</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.3.1</version> </dependency> </dependencies> - Add directories of core module (nelo2-java-sdk-core-1.6.6.jar) and Log4j2 module (nelo2-java-sdk-log4j2-2.8.5.jar) to
This SDK includes log4j-slf4j-impl, which is the SLF4J binding library for Log4j2.
- Since SLF4J supports only one binding at a time, you must not use it together with other SLF4J binding libraries.
- If the existing reference library overlaps with those referenced by the NELO2 Log4j2 SDK, problems may occur. In this case, it is recommended to use the later version.
3. Set basic appender
To set the basic appender:
- File name:
log4j2.xml
Enter configuration as the following example:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" shutDownHook="disable">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<ThriftAppender>
<name>nelo</name>
<collectorUrl>COLLECTOR_HOST</collectorUrl>
<port>COLLECTOR_PORT</port>
<projectName>PROJECT_KEY</projectName>
<version>PROJECT_VERSION</version>
<logType>LOG_TYPE</logType>
<logSource>LOG_SOURCE</logSource>
</ThriftAppender>
<HttpAppender>
<name>http</name>
<collectorUrl>COLLECTOR_HOST</collectorUrl>
<port>COLLECTOR_PORT</port>
<projectName>PROJECT_KEY</projectName>
<version>PROJECT_VERSION</version>
<logType>LOG_TYPE</logType>
<logSource>LOG_SOURCE</logSource>
</HttpAppender>
</Appenders>
<Loggers>
<Logger name="nelo" level="error" additivity="false">
<AppenderRef ref="nelo"/>
<AppenderRef ref="console"/>
</Logger>
<Root level="warn">
<AppenderRef ref="nelo"/>
<AppenderRef ref="console"/>
</Root>
</Loggers>
</Configuration>
The following describes each setting option:
| Setting options | Description |
|---|---|
Appender |
Select the appender class name based on the transfer protocol.
|
projectName |
Project key. |
version |
Project version. (Only English letters, numbers, -, _, . are allowed, and it should start with an English letter, number, or _.) |
collectorUrl |
Log collecting server URL.
|
port |
Collection server port.
|
enable |
Status of using SDK (default: true). |
logType |
Set logType. |
logSource |
Set logSource. |
errorCodeType |
Error code type.
|
debug |
Display debugging information of Effective Log Search & Analytics 2.0 (default: false).
|
timeout |
Socket timeout used for transmission. The default value is 5000 ms (5 seconds). |
keepAlive |
Socket keepAlive timeout used for transmission. The default value is 60,000 ms (1 minute), and the maximum value is 180,000 ms (3 minutes). |
isBulkEnabled |
Set whether to use bulk mode (default: true).
|
bulkSize |
Maximum number of logs to be sent in a single bulk request when using bulk mode. The default value is 1000, and the maximum value is 100,000. |
bulkInterval |
Cycle to call bulk requests when using bulk mode. The default value is 1000 ms (1 second), and the maximum value is 10,000 ms (10 seconds). |
alwaysIncludeLocation |
Set whether to add the Location field to all logs (default: true).
|
mdcConversionRule |
Set rule for renaming MDC keys.
|
Note the following in regards to the memory usage:
- The provided SDK uses
bulktransfer mode by default, and the default value ofbulkSizeis 1000. As each Effective Log Search & Analytics 2.0 log includes multiple fields such asprojectName, it occupies about 1 KB of memory even when the log body is very short. Therefore, with the default settings, a NELO2 bulk occupies(로그 사이즈 + 1 kb)x1000of memory. - When starting a Java process, you can specify the maximum heap size using the
-Xmxoption. At this point, consider additional memory usage. - Be more cautious when you set the
bulkSizeoption even larger.
Bulk and single modes
The NELO2 Log4j2 SDK supports both single mode, which sends logs individually, and bulk mode, which sends logs in batches. You can select bulk or single mode by setting isBulkEnabled to true or false in xml appender settings (default: true(bulk mode)).
Following the protocol, note the following performance indicator:
- Throughput when sending 1 KB logs from a single thread for 1 minute
- thrift
- Single mode: 2636.00 logs/sec
- Bulk mode: 6369.10 logs/sec
- http
- Single mode: 583.36 logs/sec
- Bulk mode: 4618.90 logs/sec
- thrift
- The specifications for the equipment used in the performance test above are as follows:
- Log transfer server: 2 GHZ, 12 core CPU, and 48 G memory.
- Log collecting server: 2.26 GHZ, 12 core CPU, and 48 G memory.
- The transfer performance may differ depending on the load.
- The test was run with no load, and it shows relatively low performance when transferring with the actual instance in use.
- Different performance due to the instance load can be perceived more clearly in the single mode than the bulk mode. Therefore, use the bulk mode, which is the default (recommended).
- The maximum size of the packet that the collecting server allows is 30 MB. Configure an appropriate
bulkSizebased on the client server's log pattern. (Default: 1000)
4. Set AsyncAppender
In addition to the default appender, you can configure Log4j2's AsyncAppender to perform log transmission in a separate thread.
To configure AsyncAppender, refer to the following example.
Set AsyncAppender's includeLocation to true. Otherwise, an error occurs during the transfer.
For more information on AsyncAppender settings, see Log4j2 manual.
Example:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" shutDownHook="disable">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<ThriftAppender>
<name>nelo</name>
<collectorUrl>COLLECTOR_HOST</collectorUrl>
<port>COLLECTOR_PORT</port>
<projectName>PROJECT_KEY</projectName>
<version>PROJECT_VERSION</version>
<logType>LOG_TYPE</logType>
<logSource>LOG_SOURCE</logSource>
</ThriftAppender>
<HttpAppender>
<name>http</name>
<collectorUrl>COLLECTOR_HOST</collectorUrl>
<port>COLLECTOR_PORT</port>
<projectName>PROJECT_KEY</projectName>
<version>PROJECT_VERSION</version>
<logType>LOG_TYPE</logType>
<logSource>LOG_SOURCE</logSource>
</HttpAppender>
<Async name="async">
<AppenderRef ref="nelo"/>
<includeLocation>true</includeLocation>
</Async>
</Appenders>
<Loggers>
<Logger name="nelo" level="error" additivity="false">
<AppenderRef ref="async"/>
<AppenderRef ref="console"/>
</Logger>
<Root level="warn">
<AppenderRef ref="nelo"/>
<AppenderRef ref="console"/>
</Root>
</Loggers>
</Configuration>
5. Use SDK
Refer to the following actual codes that use Effective Log Search & Analytics 2.0 Log4j2 SDK and transfer logs to the projects:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static final Logger logger = LoggerFactory.getLogger("nelo");
...
logger.debug("Effective Log Search & Analytics log4j2 SDK Debug Message");
try {
String npe = null;
npe.toString();
} catch(Exception e) {
logger.error("Effective Log Search & Analytics log4j2 SDK Exception", e);
}
Effective Log Search & Analytics 2.0 Log4j2 SDK's service limits are as follows:
- While using AsyncAppender, when the log occurrence speed is faster than the transfer speed, any logs that exceed
queueSize(default: 128) are not transferred. - Select between AsyncAppender and the basic appender depending on the following criteria (recommended):
- Basic appender: When you want to minimize log loss.
- AsyncAppende: When you are worried about the performance degradation in an application when there are system failures in Effective Log Search & Analytics 2.0.
Log4j2 SDK v2 troubleshooting
Check potential problems and solutions related to Log4j2 SDK v2.
When logs are transferred to the Effective Log Search & Analytics 2.0 server but you cannot view them from the web
When you cannot view the transferred logs from the web, take the following measures:
- Check if the
projectNameis correct when there is no error in the result message after transferring the logs to Effective Log Search & Analytics 2.0's collecting server. - Check if the actual error date is transferred.
- Check if the following transfer log is exported after setting the Effective Log Search & Analytics 2.0 Nelo appender's
debugattribute totruein the configuration file (log4j.xml):
- Check if the following transfer log is exported after setting the Effective Log Search & Analytics 2.0 Nelo appender's
<!-- define nelo appender -->
<ThriftAppender>
<name>nelo</name>
<collectorUrl>COLLECTOR_HOST</collectorUrl>
<port>COLLECTOR_PORT</port>
<projectName>PROJECT_KEY</projectName>
<version>PROJECT_VERSION</version>
<logType>LOG_TYPE</logType>
<logSource>LOG_SOURCE</logSource>
<debug>true</debug>
</ThriftAppender>
[NELO2] Log Append : sent event, return value :
…