当前位置:网站首页>Uploading logs using loghub log4j appender

Uploading logs using loghub log4j appender

2022-06-23 03:09:00 No comment

Look at the effect :

One 、 Project introduction and configuration

1、Maven To configure

<dependency>
    <groupId>com.tencentcloudapi.cls</groupId>
    <artifactId>tencentcloud-cls-log4j-appender</artifactId>
    <version>1.0.2</version>
</dependency>

2、log4j.properties To configure

log4j.rootLogger=DEBUG,loghubAppender
#loghubAppender
log4j.appender.loghubAppender=com.tencentcloudapi.cls.LoghubAppender
# Logging service http Address , Required parameters 
log4j.appender.loghubAppender.endpoint=ap-chengdu.cls.tencentcs.com
# User identity , Required parameters 
log4j.appender.loghubAppender.accessKeyId= Own SecretId
log4j.appender.loghubAppender.accessKeySecret= Own SecretKey
# Set up log The format of the field , Required parameters 
log4j.appender.loghubAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.loghubAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
# Specify the log subject , Optional parameters 
log4j.appender.loghubAppender.topicID = My own theme ID
# Specify the log source , Optional parameters 
log4j.appender.loghubAppender.source =
# Single  producer  The maximum log size that an instance can cache , The default is  100MB.
log4j.appender.loghubAppender.totalSizeInBytes=104857600
# If  producer  Insufficient free space , The caller is  send  Maximum blocking time on method , The default is  60  second . In order not to block the thread that prints the log , It is strongly recommended that this value be set to  0.
log4j.appender.loghubAppender.maxBlockMs=0
# The size of the thread pool that executes the log sending task , The default is the number of available processors .
log4j.appender.loghubAppender.sendThreadCount=8
# When one  ProducerBatch  The log size cached in is greater than or equal to  batchSizeThresholdInBytes  when , The  batch  Will be sent , The default is  512 KB, The maximum can be set to  5MB.
log4j.appender.loghubAppender.batchSizeThresholdInBytes=524288
# When one  ProducerBatch  The number of cached log entries in is greater than or equal to  batchCountThreshold  when , The  batch  Will be sent , The default is  4096, The maximum can be set to  40960.
log4j.appender.loghubAppender.batchCountThreshold=4096
# One  ProducerBatch  Stay time from creation to sendable , The default is  2  second , The minimum can be set to  100  millisecond .
log4j.appender.loghubAppender.lingerMs=2000
# If a  ProducerBatch  First send failed , The number of times it can be retried , The default is  10  Time .
# If  retries  Less than or equal to  0, The  ProducerBatch  After the first sending failure, it will directly enter the failure queue .
log4j.appender.loghubAppender.retries=10
# The larger the parameter, the more information you can trace , But it also consumes more memory .
log4j.appender.loghubAppender.maxReservedAttempts=11
# The backoff time of the first retry , The default is  100  millisecond .
#Producer  Sampling exponential backoff algorithm , The first  N  The scheduled wait time for retries is  baseRetryBackoffMs * 2^(N-1).
log4j.appender.loghubAppender.baseRetryBackoffMs=100
# Maximum backoff time for retry , The default is  50  second .
log4j.appender.loghubAppender.maxRetryBackoffMs=50000

# Set time format , Optional parameters 
log4j.appender.loghubAppender.timeFormat=yyyy-MM-dd'T'HH:mm:ssZ
# Set the time zone to East 8 , Optional parameters 
log4j.appender.loghubAppender.timeZone=Asia/Shanghai
# Output INFO Level and above 
log4j.appender.loghubAppender.Threshold=DEBUG

Two 、 Create a new test class and run

import org.apache.log4j.Logger;

class Test_Loghub_log4j_Appende {

    private  static  final  org.apache.log4j.Logger  Logger= org.apache.log4j.Logger.getLogger(org.apache.log4j.Logger.class);

    public static void main(String[] args) {

        Logger.info(" test ");

    }
}

3、 ... and 、CLS Console configuration index

All right

Four 、JDK Environmental Science

原网站

版权声明
本文为[No comment]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/01/202201222213395717.html