阿里云-日志服务-Log4j写入日志

    xiaoxiao2022-06-23  213

    ---------Maven依赖--------- <dependency> <groupId>com.aliyun.openservices</groupId> <artifactId>aliyun-log</artifactId> <version>0.6.6</version> </dependency> <dependency> <groupId>com.aliyun.openservices</groupId> <artifactId>log-loghub-log4j-appender</artifactId> <version>0.1.3</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> ---------web.xml配置--------- <!--log4j配置文件加载--> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <!--启动一个watchdog线程每1800秒扫描一下log4j配置文件的变化--> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>1800000</param-value> </context-param> <!--spring log4j监听器--> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> ---------log4j.properties配置 --------- log4j.rootLogger=INFO,loghub log4j.appender.loghub = com.aliyun.openservices.log.log4j.LoghubAppender log4j.appender.loghub.projectName = 你的日志服务projectName log4j.appender.loghub.logstore = 你的日志服务logstoreName log4j.appender.loghub.topic = 你的日志服务topic log4j.appender.loghub.endpoint = cn-hangzhou.log.aliyuncs.com log4j.appender.loghub.accessKeyId = 你的阿里云accessKeyId log4j.appender.loghub.accessKey = 你的阿里云accessKey #log4j.appender.loghub.stsToken=[your ststoken] log4j.appender.loghub.packageTimeoutInMS=3000 log4j.appender.loghub.logsCountPerPackage= 4096 log4j.appender.loghub.logsBytesPerPackage = 5242880 log4j.appender.loghub.memPoolSizeInByte=1048576000 log4j.appender.loghub.ioThreadsCount=1 log4j.appender.loghub.timeFormat=yyyy-MM-dd'T'HH:mmZ log4j.appender.loghub.timeZone=UTC ---------JAVA测试代码 --------- import org.apache.log4j.LogManager; import org.apache.log4j.Logger; public class TestLog4j {     private static Logger logger = LogManager.getLogger(TestLog4j.class); public static void main(String args[]) { long startTime = System.currentTimeMillis(); System.out.println("写日志完成-开始:"+startTime); for(int i=0;i<30000;i++) { logger.info(i+"," +System.currentTimeMillis()+ ",备注字段内容2W-from-log4j-"+i); } long endTime = System.currentTimeMillis(); System.out.println("写日志完成-结束:"+endTime); System.out.println("写日志完成-用时:"+(endTime-startTime));// 2个shard,3万条日志写入用时约1.58秒 } /*  * 通过Loghub Log4j Appender,您可以控制日志的输出目的地为阿里云日志服务。需要注意的是,Loghub Log4j Appender不支持设置日志的输出格式,写到日志服务中的日志的样式如下:  * level:ERROR  * location:test.TestLog4jAppender.main(TestLog4jAppender.java:18)  * message:test log4j appender  * thread:main  * time:2016-05-27T03:15+0000  *其中:  * level 是日志级别。  * location 是日志打印语句的代码位置。  * message 是日志内容。  * thread 是线程名称。  * time 是日志打印时间。  * */ }

    最新回复(0)