閱讀945 返回首頁    go 阿裏雲 go 技術社區[雲棲]


阿裏雲-日誌服務-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

  上一篇:go  讀書筆記:關於適當的微服務架構的看法(Perspective on Architectural Fitness of Microservices)
  下一篇:go  AC自動機