阿裏雲-日誌服務-Log4j寫入日誌
---------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 =
log4j.appender.loghub.logstore =
log4j.appender.loghub.topic =
log4j.appender.loghub.endpoint = cn-hangzhou.log.aliyuncs.com
log4j.appender.loghub.accessKeyId =
log4j.appender.loghub.accessKey =
#log4j.appender.loghub.stsToken=[your ststoken]
log4j.appender.loghub.packageTimeoutInMS=3000
log4j.appender.loghub.logsCountPerPackage=
log4j.appender.loghub.logsBytesPerPackage =
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));//
}
/*
* 通過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 是日誌打印時間。
* */
}
最後更新:2017-08-23 09:02:17
上一篇:
讀書筆記:關於適當的微服務架構的看法(Perspective on Architectural Fitness of Microservices)
下一篇:
AC自動機
微軟 Office 仍是重要企業應用的10大理由
'System.Data.DataRow.DataRow(System.Data.DataRowBuilder)' is inaccessible due to its protection leve
學習java八大優點,受益匪淺
數組的冒泡排序及拷貝
在CentOS上搭建PHP服務器環境
阿裏巴巴中台架構的業務價值思考——走進《企業IT架構轉型之道》係列1
遠程數據庫的表超過20個索引的影響
LUA 協程 Coroutine
關閉TortoiseSVN的TSVNCache.exe進程方案
C# 指針學習筆記之fixed 語句