Spring Boot and RESTful API(15)Logging and Custom SOLR Template

Spring Boot and RESTful API(15)Logging and Custom SOLR Template

Recently, one of our Spring Boot Project lost the logging in the CloudWatch. I was thinking it is jar conflicts because we bring in a new jar. That mades us dive into the logging for that Project for a while.

First of all, here is the MAVEN command to display the while dependencies which I really think it is useful.
>mvn dependency:tree

I used to use similar command in grails, but this is first time for maven. That is great beginning.

Here is the results how we fix that.
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

That is complex because I used to directly use spring boot starter logging, right now It is kind of disable that and enable log4j2 in spring boot. But any way, since it is a quick fix. Make it working first.

In the class path, put log4j.xml there, since we streaming the logging to cloud watch, so we really do not need a File Pending or any other settings, just pending the to console

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="ERROR" shutdownHook="disable">
<Appenders>
   <Console name="CONSOLE" target="SYSTEM_OUT">
     <PatternLayout pattern="(%d{yyyy-MM-dd'T'HH:mm:ss.SXXX}) %-7p - %C %M - %m\r\n%throwable{full}"/>
   </Console>
   <!--
   <Console name="COLOR_CONSOLE" target="SYSTEM_OUT">
     <PatternLayout pattern="%highlight{(%d{yyyy-MM-dd'T'HH:mm:ss.SXXX}) %-7p - %C %M - %m}{FATAL=red bright, ERROR=red bright, WARN=red bright, INFO=green bright, DEBUG=green bright, TRACE=blue bright}\r\n%throwable{full}"/>
   </Console>
   -->
   <!--
   <File name="FILE" fileName="${sys:java.io.tmpdir}/internal-api.log" append="true">
     <PatternLayout>
       <pattern>(%d{yyyy-MM-dd'T'HH:mm:ss.SXXX}) %-7p - %C %M - %m\r\n%throwable{full}</pattern>
     </PatternLayout>
   </File>
   -->
   <!--
   <Async name="ASYNC_FILE" includeLocation="true">
     <AppenderRef ref="FILE"/>
   </Async>
   -->
</Appenders>
<Loggers>
   <Logger name="com.google.inject.internal" level="WARN"/>
   <Logger name="com.optimaize" level="WARN"/>
   <Logger name="net.sourceforge.cobertura" level="WARN"/>
   <Logger name="org.hibernate" level="WARN"/>
   <Logger name="com.sillycat" level="INFO" />
   <Root level="ALL">
     <AppenderRef ref="CONSOLE" level="INFO"/>
     <!-- <AppenderRef ref="COLOR_CONSOLE" level="WARN"/> -->
     <!-- <AppenderRef ref="FILE" level="ALL"/> -->
     <!-- <AppenderRef ref="ASYNC_FILE" level="ALL"/> -->
   </Root>
</Loggers>
</Configuration>

Another thing, we do a fix to disable the auto commit on Spring-data-solr, otherwise, our SOLR will have issue to handle 200G Data.

here is the implementation of that idea
package com.sillycat.jobsmonitorapi.repository;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.data.solr.core.query.SolrDataQuery;
import org.springframework.stereotype.Repository;

@Repository
public class JobCustomRepositorySolrImpl implements JobCustomRepositorySolr
{

  private final Logger logger = LoggerFactory.getLogger( this.getClass() );

  @Resource
  private SolrTemplate solrTemplate;

  @Override
  public void deleteByCampaignID( Long campaignID, Boolean commit )
  {
    SolrDataQuery query = new SimpleQuery( new Criteria( "campaign_id" ).is( campaignID ) );
    solrTemplate.delete( "job", query );
    if ( commit )
    {
      logger.warn( "Client side should never commit itself." );
      solrTemplate.commit( "job" );
    }
  }

}



References:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html


猜你喜欢

转载自sillycat.iteye.com/blog/2408201