日志规范化落地方案
  • 时间:2020-01-14

这些规划都是为了后边便利日志查找和监控报警。

别的咱们发现许多搭档打日志惧怕反常信息和仓库丢掉,喜爱把反常音讯,仓库和自定义音讯拼接到一同,例如:

logger.error;

形成反常信息,仓库丢掉的原因是因为原先的日志里没有主动把反常信息,仓库,包含到格局里。这也是咱们顺带需求处理的一个问题。

咱们团队一致运用的是slf4j + log4j2来打日志,log4j2自身供给了十分友爱的插件扩展, 这样咱们就可以扩展一个自己的Layout出来,将日志格局标准内化到Layout里,这样运用者 只需求运用咱们的Layout,无需关怀日志格局,打出来便是标准的日志格局,这样推行就会 简略许多,并且还可以便利的扩展字段,趁便处理掉反常音讯和仓库丢掉的问题。

咱们扩展的Layout完成类名为: Autolog4jCsvLayout, 以下装备就可以完成日志标准化。

 RollingFile name= ProgramError ignoreExceptions= false 
 fileName= ${sys:log.path}/project_error.log 
 filePattern= ${sys:log.path}/project_error.log_%d{yyyy-MM-dd} 
 Autolog4jCsvLayout charset= UTF-8 department= ${sys:department} team= ${sys:team} project= ${sys:project} / 
 Policies 
 TimeBasedTriggeringPolicy interval= 1 modulate= true / 
 /Policies 
 EnumFilter allowLevels= WARN,ERROR,FATAL / 
 /RollingFile 
 RollingFile name= ProgramRun ignoreExceptions= false 
 fileName= ${sys:log.path}/project_run.log 
 filePattern= ${sys:log.path}/project_run.log_%d{yyyy-MM-dd} 
 Autolog4jCsvLayout charset= UTF-8 department= ${sys:department} team= ${sys:team} project= ${sys:project} / 
 Policies 
 TimeBasedTriggeringPolicy interval= 1 modulate= true / 
 /Policies 
 EnumFilter allowLevels= TRACE,DEBUG,INFO / 
 /RollingFile 

这儿将过错日志和INFO日志分隔写入。别的咱们发现log4j2的日志等级过滤器很难了解,所以完成了一种直接枚举日志等级的过滤器,简略好懂。 处理了反常信息仓库丢掉的问题,咱们就可以高兴的运用如下方法打日志:

logger.error;

打出的文本日志举例:

 2018-04-20T15:18:59.773+08:00 - dealer dealer.arch projectname - 10.1.1.1 - - - - - ERROR org.springframework.test.context.TestContextManager prepareTestInstance - 234 org.springframework.test.context.TestContextManager unknown java.lang.IllegalStateException Failed to load ApplicationContext Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@17a756db] to prepare test instance [com.autohome.daimon.job.service.integration.HawkeyeServiceTest@2d10160a] java.lang.IllegalStateException: Failed to load ApplicationContext
 at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext
 at com.intellij.rt.execution.junit.JUnitStarter.main
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hawkeyeService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'redisDao' is expected to be of type 'RedisDao' but was actually of type 'RedisDao$$EnhancerBySpringCGLIB$$17f5ad58'
 at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean
 at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal
 at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext
 ... 24 more
Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'redisDao' is expected to be of type 'IRedisDao' but was actually of type 'RedisDao$$EnhancerBySpringCGLIB$$17f5ad58'
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean
 ... 40 more
 b83539ac4a40de0d 

别的日志标准仅仅日志建造的第一步,咱们还开源了根据Elasticsearch数据的开源日志监控体系, 欢迎运用。 github地址: frostmourne


客服QQ: 点击这里
地址: 客服QQ:
Copyright © 2018 凯发娱乐手机客户端凯发娱乐手机客户端-凯发娱乐手机端 All Rights Reserved

服务时间:7X10小时