当前位置:网站首页>[log service CLS] Tencent cloud log4j/logback log collection best practices
[log service CLS] Tencent cloud log4j/logback log collection best practices
2022-06-24 15:45:00 【Ma Xuefeng】
One introduction
Log storage analysis plays an important role in application systems , Conventional ELK Too cumbersome for small teams , Maintenance trouble , Tencent cloud provides CLS Log collection and analysis system , Can pass LogListener To collect logs in a non intrusive way , Developers can also use API To collect logs ( There seems to be no... Available at present sdk To collect developer application logs , Or the author missed a part of the document ), Official website documents for API There are relatively few best practice documents for collecting logs , The author combines Tencent cloud CLS and Java The two most common in the field log The tool implements code intrusion free business log reporting .
Two Log4j/Logback Knowledge preparation
log4j and logback yes Java The two most common open source in the development field log library , Built in many logs Appender class , such as Stdout,FileAppender, Developers can also define according to their specific needs Appender, stay log Configure custom in the configuration file Appender Class to implement custom log collection logic .
3、 ... and Architecture diagram
Log collection architecture diagram
Pictured above , Developers can choose to submit logs to Tencent cloud through the customized log collection module CLS.
Four Tencent cloud CLS Logical concept preparation
- Log set : A log set corresponds to a project or application
- Journal topics : A log topic corresponds to a type of application or service
- Log group : A collection containing multiple logs
- Log partition : A log topic can be divided into multiple topic partitions , But at least one partition ( It can improve the retrieval efficiency )
5、 ... and preparation
- Log in to Tencent cloud , obtain secretId,secretKey( More important )
- Get into CLS Console
- Create blog topics according to your personal needs ( Copy the theme ID spare )
6、 ... and Best practices
The sample code address for this article :Github Warehouse address ( For reference only )
Technology selection
- Java1.8+
- Maven3.6+
- log4j1.2.6
- logback1.1.7
CLS Log escalation cloud API
- The author of this article calls API To upload structured logs , Reference upload Structured log API file
- cloud API Signature access reference Official website API Sign the document
- cloud API Signature reference Official website signature demo
- Upload structured logs API need google protobuf Convert the log contents to BP Format , Please check protobuf Introduce
establish maven project , The project structure is shown in the figure below
pom.xml Add the following dependencies
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
</dependency>Log4j Realize log reporting
Log4j Need to implement custom Appender Need to inherit org.apache.log4j.AppenderSkeleton,Log4j Log output is done through event driven , So in the core logic , We just need to get the information carried by the event , And call the cloud API, Upload the information carried in these events , The logic diagram is as follows
Scheme implementation steps
1 stay maven project resources Create under directory log4j.properties file (log4j The file name loaded by the log framework by default ), The specific configuration is as follows
log4j.rootLogger=DEBUG,clsLogAppender, STDOUT log4j.appender.clsLogAppender=com.github.cls.log4j.ClsLog4jAppender log4j.appender.clsLogAppender.project=smartdoc log4j.appender.clsLogAppender.region=ap-nanjing log4j.appender.clsLogAppender.endpoint=ap-nanjing.cls.tencentcs.com log4j.appender.clsLogAppender.secretId=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX log4j.appender.clsLogAppender.secretKey=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX log4j.appender.clsLogAppender.layout=org.apache.log4j.PatternLayout log4j.appender.clsLogAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n log4j.appender.clsLogAppender.topicId=b9ed1869-156a-4eb4-ae96-b0fa292cf5b5 log4j.appender.clsLogAppender.source=smart-doc log4j.appender.clsLogAppender.timeFormat=yyyy-MM-dd'T'HH:mm:ssZ log4j.appender.clsLogAppender.timeZone=UTC log4j.appender.clsLogAppender.Threshold=INFO #STDOUT log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout log4j.appender.STDOUT.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
In the above code ,clsLogAppender It belongs to what developers need to define appender The name of ,region,endpoint,secretId,secretKey,topicId These information fields need to be obtained from the console , And these fields need to be defined in the custom Appender in , Convenient from log4j.properties Get the custom configuration item from the file .
2 install proto; Installation steps refer to
https://cloud.tencent.com/document/product/614/16873#pb-.E7.BC.96.E8.AF.91.E7.A4.BA.E4.BE.8B
3 Define the log BP Structure
package com.github.cls;
message Log
{
message Content
{
required string key = 1; // For each group of fields key
required string value = 2; // For each group of fields value
}
required int64 time = 1; // Time stamp ,UNIX Time format
repeated Content contents = 2; // Multiple in one log kv Combine
}
message LogTag
{
required string key = 1;
required string value = 2;
}
message LogGroup
{
repeated Log logs = 1; // Log array composed of multiple logs
optional string contextFlow = 2; // It's not available at present
optional string filename = 3; // Log file name
optional string source = 4; // Source of a log , Generally, machines are used IP
repeated LogTag logTags = 5;
}
message LogGroupList
{
repeated LogGroup logGroupList = 1; // Log group list
}4 call protoc --java_out=./ ./cls.proto Generate BP Structure
5 The generated Java File copy to maven In Engineering ( In this example , My Java The structure name is Cls)
6 Custom log upload core logic ( Inherit org.apache.log4j.AppenderSkeleton class )
7 The following abstract methods are implemented
@Override
protected void append(LoggingEvent event) {
// 1 encapsulation BP Structure
Cls.LogGroupList logGroupList = Cls.LogGroupList.newBuilder().addLogGroupList(
Cls.LogGroup.newBuilder().addLogs(
Cls.Log.newBuilder()
.setTime(event.timeStamp)
.addContents(Cls.Log.Content.newBuilder().setKey("exceptionTrace").setValue(String.valueOf(getThrowableStr(event))))
.addContents(Cls.Log.Content.newBuilder().setKey("level").setValue(String.valueOf(event.getLevel())))
.addContents(Cls.Log.Content.newBuilder().setKey("threadName").setValue(event.getThreadName()))
.addContents(Cls.Log.Content.newBuilder().setKey("time").setValue(String.valueOf(event.timeStamp)))
.addContents(Cls.Log.Content.newBuilder().setKey("message").setValue(String.valueOf(event.getMessage())))
)
).build();
// Upload BP Structure
PostStructLogRequest req = client.newPostStructLogRequest(topicId, logGroupList);
String result = client.send(req);
}In the above code client and PostStructLogRequest It is simply implemented by the author , Here is just the general logic , The details of implementation will not be repeated , Please refer to github Code example
8 Write a log test class
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
public class Log4jAppenderExample {
private static final Logger LOGGER = LogManager.getLogger(Log4jAppenderExample.class);
public static void main(String[] args) {
LOGGER.trace("cls log4j trace log");
LOGGER.debug("cls log4j debug log");
LOGGER.info("cls log4j info log");
LOGGER.warn("cls log4j warn log");
LOGGER.error("cls log4j error log", new RuntimeException("Runtime Exception"));
}
}9 Run test class
10 land CLS The console switches to the log retrieval menu , Here's the picture
11 If CLS The console displays the logs you printed in the application , Be accomplished
logback Report logs to the cloud
loback The implementation steps are the same as above , The difference is that developers are required to resources Definition under directory logback.xml In file , And configure Appender Custom properties for , as follows demo To configure
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>
<appender name="TencentCLS" class="com.github.cls.logback.ClsLogbackAppender">
<project>smartdoc</project>
<endpoint>ap-nanjing.cls.tencentcs.com</endpoint>
<region>ap-nanjing</region>
<secretId>AKIDQa1B6dIsc8OSntCuVpujKgHi8HAomOIE</secretId>
<secretKey>vWqtiGadeXCbjcg39yZtwj2EJ7ewZv03</secretKey>
<topicId>b9ed1869-156a-4eb4-ae96-b0fa292cf5b5</topicId>
<source>smart-doc</source>
<timeZone>UTC</timeZone>
<timeFormat>yyyy-MM-dd'T'HH:mmZ</timeFormat>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT"/>
<appender-ref ref="TencentCLS"/>
</root>
</configuration>remarks :
- com.github.cls.logback.ClsLogbackAppender It is self defined by the author logback Appender
- Need to be in appender Configure properties in tags
- logback The log reporting test classes are as follows
package com.github.cls.example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LogbackAppenderExample {
private static final Logger LOGGER = LoggerFactory.getLogger(LogbackAppenderExample.class);
public static void main(String[] args) {
LOGGER.trace("cls logback trace log");
LOGGER.debug("cls logback debug log");
LOGGER.info("cls logback info log");
LOGGER.warn("cls logback warn log");
LOGGER.error("cls logback error log", new RuntimeException("Runtime Exception"));
}
}7、 ... and summary
The above log reporting implementation scheme is relative to LogListener Speaking of , There is a performance gap , Because the above log collection method adopts the synchronous method for log reporting ,LogListener Using a listener , Make the production log party and the consumption log party decouple ; but LogListener Of is limited to CVM Log collection on the machine , Or other Tencent cloud container log collection , If the developer's application is in the self built computer room or other cloud platforms , But log management is in Tencent cloud CLS On , The above implementation scheme can solve this problem , And developers can customize the logic .
边栏推荐
- Vim编辑器的最常用的用法
- Wi-Fi 7 来啦,它到底有多强?
- The penetration of 5g users of operators is far slower than that of 4G. The popularity of 5g still depends on China Radio and television
- CAP:多重注意力机制,有趣的细粒度分类方案 | AAAI 2021
- Low fidelity prototype vs high fidelity prototype, which one is more suitable for your design?
- [parameter configuration tutorial] how should the parameters in the RTMP streaming camera be configured?
- Typescript raw data type
- asciinema 搭配 asciicast2gif 实现高效的命令行终端录制能力
- 熬夜整理出的软件测试【高频】面试题大全(2022最新)
- Unimelb COMP20008 Note 2019 SM1 - Data formats
猜你喜欢

MySQL binlog
![[my advanced OpenGL learning journey] learning notes of OpenGL coordinate system](/img/21/48802245fea2921fd5e4a9a2d9ad18.jpg)
[my advanced OpenGL learning journey] learning notes of OpenGL coordinate system

刚刚阿里面软件测试回来,3+1面任职阿里P7,年薪28*15薪

如何轻松实现在线K歌房,与王心凌合唱《山海》

Mysql之Binlog

Mongodb Getting started Practical Tutoriel: Learning Summary Table des matières
![[C language questions -- leetcode 12 questions] take you off and fly into the garbage](/img/ca/a356a867f3b7ef2814080fb76b9bfb.png)
[C language questions -- leetcode 12 questions] take you off and fly into the garbage

MongoDB入門實戰教程:學習總結目錄

Still worried about missing measurements? Let's use Jacobo to calculate the code coverage

Wi-Fi 7 来啦,它到底有多强?
随机推荐
Istio Troubleshooting: using istio to reserve ports causes pod startup failure
Istio practical skill: hide the automatically added server header
Jenkins的便捷式安装
SF express: please sign for MySQL soul ten
leetcode 139. Word Break 单词拆分(中等)
Three solutions for Jenkins image failing to update plug-in Center
How to generate assembly code using clang in Intel syntax- How to generate assembly code with clang in Intel syntax?
Solution to the problem that FreeRTOS does not execute new tasks
Chaos mesh in Tencent -- Tencent mutual entertainment chaotic engineering practice
[my advanced OpenGL learning journey] learning notes of OpenGL coordinate system
Since the household appliance industry has entered the era of stock competition, why does Suning win the first channel for consecutive times?
FPGA based analog I ² C protocol system design (Part I)
At? Let's blow the air conditioner together!
PHP export data as excel table
Network engineers must know the network essence knowledge!
Design of CAN bus controller based on FPGA (Part 2)
I just came back from the Ali software test. I worked for Alibaba P7 in 3+1, with an annual salary of 28*15
How to implement SQLSERVER database migration in container
Cloud + community [play with Tencent cloud] essay solicitation activity winners announced
FPGA based analog I ² C protocol system design (Part 2)