Logback is not writing specific log file Solution


Logback is not writing specific log file Solution
Logback Configuration:
<property name="DEV_HOME" value="/root/apps/logs" />
<appender name="FILE-AUDIT"     class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${DEV_HOME}/myapp.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- rollover daily -->
        <fileNamePattern>${DEV_HOME}/archived/myapp.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <!-- or whenever the file size reaches the max -->
            <maxFileSize>${rolling.file.max.size}</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
        <maxHistory>${rolling.file.max.history}</maxHistory>
    </rollingPolicy>
    <encoder>
        <pattern>${rolling.file.encoder.pattern}</pattern>
    </encoder>
</appender>

Issue is related to conflict between log4j and Logback, when we migrate from old log4j to Logback. You need to exclude lo4j dependencies from existing dependencies by running this command:

$ mvn dependency:tree


= >Add these jars only for logging using SLF4j and Logback

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${version.slf4j}</version>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>${version.logback}</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>log4j-over-slf4j</artifactId>
        <version>${version.slf4j}</version>
        <scope>runtime</scope>
    </dependency>

Posted detail issue on StackOverflow:

http://stackoverflow.com/questions/35813027/logback-is-not-writing-specific-log-file-on-the-linux-server 

Advertisements

Apache Common Pool – Object pool example


Advantage of using object pool is to improve performance of the application by saving time to create new object from other source for the same purpose. Object pool creates once and reuse for other subsequent operations on the same object. You can customize other configuration settings. Please visit this URL for more info: http://commons.apache.org/proper/commons-pool/api-1.6/index.html

Step:1 Add this Maven jar dependency in your POM.xml

<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>

Step:2 Create Factory  Class

import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.pool.BasePoolableObjectFactory;
import org.springframework.stereotype.Component;

/**
 * @author Rajiv Srivastava
 * Object pool factory
 */
//Extend BasePoolableObjectFactory class.
@Component
public class ObjectPoolFactory extends BasePoolableObjectFactory<ConnClient> {

 // for makeObject we'll simply return a new object e.g: connection object
 @Override
 public ConnClient makeObject() throws Exception {

 InputStream stream = null;
 // Create object code here for the first time. e.g: Create connection object from other integrated system component
 ConnClient connClient= new connClient();
 return connClient;
 }
}

Step:3 Create Object Pool class and usage

import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.springframework.stereotype.Component;

/**
 * @author Rajiv Srivastava
 */

@Component
public class ObjectPoolConnection {

    private ObjectPool<ConnClient> pool;

	ConnClient obj = null;

	public ConnClient getConnClient(){

		PoolableObjectFactory<ConnClient> factory = new ObjectPoolFactory();
	    pool = new GenericObjectPool<ConnClient>(factory);

	        try {
	                try {
	                	if(obj==null){
	                		obj = (ConnClient)pool.borrowObject();
	                	}
	                } catch (Exception e) {
	                	obj=null;
	                	logger.error("failed to borrow object from Connection pool"+e.getMessage());
	                } finally {
	                    if (obj != null) {
	                        try {
								pool.returnObject(obj);
							} catch (Exception e) {
			                	logger.error("failed to return object from Connection"+e.getMessage());
							}
	                    }
	                }
	        } finally {
	            try {
					pool.close();
				} catch (Exception e) {
                	logger.error("failed to return pool"+e.getMessage());
				}
	        }
	    	return obj;
	}
}

Cassandra DataStax – Developer Guide with Spring Data Cassandra


We will discus Cassandra implementation :

Important Points:

Download and Installation:

  1. Tarball Installation

  1. DataStax DB
    • You need to register yourself with DataStax for download.
    • DataStax Enterprise – http://www.datastax.com/download#dl-enterprise.
    • Create These folders and changed permission:
    • sudo chmod 777 /var/log/cassandra
      sudo mkdir -p /var/lib/cassandra/data
      sudo chmod 777 /var/lib/cassandra/data
      sudo mkdir -p /var/lib/cassandra/commitlog
      sudo chmod 777 /var/lib/cassandra/commitlog
      sudo mkdir -p /var/lib/cassandra/saved_caches
      sudo chmod 777 /var/lib/cassandra/saved_caches
    • How to run Cassandra: Go to DataStax Cassndra installed folder on Mac/Linux/Unix env:                                                                                          
                                                                                                                                                                        cd /Users/<userName>/dse-4.5.2/bin</pre>
      <pre>sudo ./dse cassandra -f
       
      //This above command Cassandra DB on your local system. Hit enter to quit from ruining server in background and start CQL query console.
      sudo ./cqlsh
      
  • Create Schema:
    CREATE SCHEMA event_owner WITH replication = {‘class’: ‘SimpleStrategy’, ‘replication_factor’ : 1 };
    1. Schema(Keyspace) name: event_owner
    2. Table name: event_audit
    Table creation syntax:Please find the revised data model and details below.Note: I have used expanded names for easier understanding which can be shortened later on.
    CREATE TABLE  event_owner.event_audit (
    ctg    text,
    month       timestamp,
    ctgid      text,
    ver     timeuuid,
    userid      text,
    action      text,
    content     text,
    PRIMARY KEY ((category,month),cat_id,version)
    ) WITH CLUSTERING ORDER BY (cat_id ASC, version DESC);
    Sample Data
  • category | month                    | cat_id | version                              | action | content      | userid
    ———-+————————–+——–+————————————–+——–+————–+———–
    CC | 2014-01-01 05:30:00+0530 |   8000 | b3fc48e0-5608-11e4-aacd-e700f669bcfc |  DRAFT | json content | 155045940
    CC | 2014-01-01 05:30:00+0530 |   9000 | a4747460-5608-11e4-aacd-e700f669bcfc |  DRAFT | json content | 155045940
    Description

    • category
      • Commitcode / Part Association/ EventTag [CC / PA / ET]
    • month
      • 12 AM timestamp of first day of the month the change is made
    • cat_id 
      • Unique id for a particular category [Say inc axe of commit code it is cc_id say 9000]
    • version 
      • it is the unique id that indicates the version number. 
      • You can populate it using now() function. 
      • It has an embedded timestamp that can be used to know the timestamp. Use dateOf() function to get the timestamp value.
    • userid
      • id of the user who made the change
    • action 
      • SAVEDRAFT/ PUBLISH / DELETE etc.
    • content 
      • actual json content after the change [full json]

  • Sample Query to access the data
    cqlsh:cdb> select * from audit where category=’CC’ and month=’2014-01-01 05:30:00+0530′ and cat_id=’9000′;
    category | month                    | cat_id | version                              | action | content      | userid
    ———-+————————–+——–+————————————–+——–+————–+———–
    CC | 2014-01-01 05:30:00+0530 |   9000 | a4747460-5608-11e4-aacd-e700f669bcfc |  DRAFT | json content | 155045940
  • SQL Like Commands: You can use same standard SQL DDL/DML commands/syntax for Cassandra query, like-
    • Drop table 
    • Update
    • Delete
    • Truncate
    • Select query

    2. DataStax OpsCenter- 

http://www.datastax.com/what-we-offer/products-services/datastax-opscenter

    3. DataStax DevCenter

        Installation: DataStax DevCenter is a visual CQL query tool for Cassandra and DataStax Enterprise.

        How to start OpsCenter GUI: http://www.datastax.com/documentation/getting_started/doc/getting_started/gettingStartedInstallOpscTar_t.html

2. Package Installation:

DataStax All-in-One Installer

How to run DataStax?

  • Cassandra:
    • Go to your /Users//dse via Terminal and execute the following command:
      • sudo ./bin/dse cassandra -f (This will start the cassandra)
  • Opscenter and DataStax-Agent:
    • Go to /Users//dse in new Tab and execute command : 
      • sudo ./opscenter/bin/opscenter -f (This will start the opscenter)
    • Go to /Users//dse in new Tab and execute command :
      • sudo ./datastax-agent/bin/datastax-agent -f (This will start the datastax-agent)

Using DataStax:

  • Now you can see the Opscenter in Browser in following address:
    • localhost:8888/
  • Here You can able to see the your Cassandra cluster visually, (Kind of monitoring tool for cassandra)

 

Integrate Cassandra with Spring Data Cassandra :

  1. /**
     * Created by: Rajiv Srivastava
     */
    @Configuration
    @ComponentScan(basePackages = {com.cassandraproject.dao,com.cassandraproject.utils})
    @EnableCassandraRepositories(basePackages ={com.cassandraproject.repository})
    public class AuditCoreContextConfig extends AbstractCassandraConfiguration {
    
     @Override
     protected String getKeyspaceName() {
     return event_owner; //Schema or Keyspace name
     }
    
     @Override
     protected String getContactPoints() {
     return localhost;//IP address of server/local machine. Host of a clusters can be separated with comma (,) like host1,host2. Also minimum two host should be added, so that second Cassandra server will be connected if first is down.
     }
    
     @Override
     protected int getPort() {
     return 9042; //Cassandra DB port
     }
    }
    

2. Data Modelling

 a. Primary/Clustered/Partioned key


/**
 * Created by: Rajiv Srivastava
 */

/* Keyspac/Schema- event_owner
 * CREATE TABLE event_owner.event_audit (
 ctg text,
 month timestamp,
 ctgid text,
 ver timeuuid,
 userid text,
 action text,
 content text,
 PRIMARY KEY ((ctg,month),ctgid,ver)
 )WITH CLUSTERING ORDER BY (ctgid ASC, ver DESC);

*/

@PrimaryKeyClass
public class EventAuditKey implements Serializable {

private static final long serialVersionUID = 1L;

@PrimaryKeyColumn(name = ctg, ordinal = 0, type = PrimaryKeyType.PARTITIONED)
 private String category;

 @PrimaryKeyColumn(name = month, ordinal = 1, type = PrimaryKeyType.PARTITIONED)
 private Date month;

 @PrimaryKeyColumn(name = ctgid, ordinal = 2, type = PrimaryKeyType.CLUSTERED, ordering =Ordering.ASCENDING)
 private String categoryId;

 @PrimaryKeyColumn(name = ver, ordinal = 3, type = PrimaryKeyType.CLUSTERED, ordering = Ordering.DESCENDING)
 private UUID version;

 3. Repository


/**
 * Created by: Rajiv Srivastava*/
@Repository
public interface AuditRepository extends CrudRepository <EventAudit, EventAuditKey> {

	@Query(select * from event_owner.event_audit)
	public List<EventAudit&amp> eventAudit();
}

4. CRUD Operation using JPA/CrudRepository – DAO Layer


/**
 * Created by: Rajiv Srivastava*/
@Component
public class EventDaoImpl implements EventAuditDao {

 @Autowired
 public AuditRepository auditRepository;

 @Override
 public void save(EventAudit entity) {
 auditRepository.save(entity);

 }

 @Override
 public void save(Collection<EventAudit> entities) {
 auditRepository.save(entities);
 }

 @Override
 public EventAudit find(EventAuditKey eventAuditKey) {
 if(null==eventAuditKey){
 throw new IllegalArgumentException(It doesn't has all required instance variable set);
 }
 return auditRepository.findOne(eventAuditKey);
 }

 @Override
 public List<EventAudit> getAll() {

 Iterable<EventAudit> iterable=auditRepository.findAll();
 if(null != iterable.iterator()){
 return Lists.newArrayList(iterable.iterator());
 }
 return new ArrayList<>();
 }

 @Override
 public List<EventAudit> getListEventAuditMonthCategoryWise(Date date, String Category) {
 // TODO Auto-generated method stub
 return null;
 }
}

Apache Cassandra: 

  * Getting started: http://wiki.apache.org/cassandra/GettingStarted

  * Join us in #cassandra on irc.freenode.net and ask questions

  * Subscribe to the Users mailing list by sending a mail to

    user-subscribe@cassandra.apache.org

  * Planet Cassandra aggregates Cassandra articles and news:

    http://planetcassandra.org/

For more on what commands are supported by CQL, see

https://github.com/apache/cassandra/blob/trunk/doc/cql3/CQL.textile.  A

reasonable way to think of it is as, “SQL minus joins and subqueries.”

MongoDB + Java + Maven Sample Application


Prerequisite: Download and Install MongoDB (http://docs.mongodb.org/manual/installation/) on your local machine and run two instances in two separate terminal/command prompt. This sample app is developed on Mac OS/Linux env.

1. Start MongoDB server using : ./mongod

2. Start MongoDB client : ./mongo

Add MongoDB Jar dependency in your project:

 <dependency>
	<groupId>org.mongodb</groupId>
	<artifactId>mongo-java-driver</artifactId>
	<version>2.10.1</version>
 </dependency>

 

Sample MONGODB CRUD code:

package com.db;
import java.util.List;
import java.util.Set;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;

public class MongoConnectionManager {
	static DBCursor cursor;

	public static void main(String[] args) {

	try{
		System.out.println("Connecting with MongoDB......");
		//DB connection after ver 2.10.0
		MongoClient mongoconn = new MongoClient("localhost",27017 );
		//Get database
		DB db = mongoconn.getDB( "test" );
		System.out.println("Database Name: "+db.getName());

		//Display all databases
		List<String> dbs = mongoconn.getDatabaseNames();
		for(String mydb : dbs){
			System.out.println(mydb);
		}

		//If MongoDB is installed on Secure mode
		//boolean auth = db.authenticate("username", "password".toCharArray());

		//Get Collection/tables names
		Set<String> colls = db.getCollectionNames();

		for (String s : colls) {
		    System.out.println(s);
		}

		//Get Collection

		DBCollection coll = db.getCollection("mynames");
		coll.createIndex(new BasicDBObject("email", 1));  // create index on "i", ascending

		System.out.printf("Collection Names:"+db.getCollectionNames());

		//Drop collections
		coll.drop(); 

		//Insert new records/documents
		BasicDBObject doc1 = new BasicDBObject();

		//Insert values into document/record
        doc1.put("name", "Rajiv");
        doc1.put("email", "rajivmca2004@yahoo.com");
        doc1.put("count",1);

     // Insert values in Collection
        coll.insert(doc1);

        BasicDBObject doc2 = new BasicDBObject();

		//Insert values into document/record
        doc2.put("name", "Mohit");
        doc2.put("email", "mohit@yahoo.com");
        doc2.put("count",2);

       // Insert values in Collection
        coll.insert(doc2);

        //Fecth only first doc

        DBObject mydoc= coll.findOne();
        System.out.println("Only First Doc :"+mydoc);

        //Insert and fetch Multiple doc

        for (int i = 0; i < 10; i++) {
			coll.insert(new BasicDBObject().append("count", i));
		}

        //Total documents
       cursor= coll.find();

        while(cursor.hasNext()){
        	System.out.println(cursor.next());
        }

        //Getting A Single Document with A Query
        BasicDBObject query= new BasicDBObject();
        query.put("name","Rajiv");
        cursor= coll.find(query);
        System.out.print("Getting A Single Document with A Query: \n");

        //Iterate over database results
        while(cursor.hasNext()){
        	System.out.println(cursor.next());
        }

        //Conditional Queries, documents where i > 50. We could also get a range, say 20 < i <= 30 :
        query = new BasicDBObject();
        query.put("count", new BasicDBObject("$gt", 1).append("$lte", 30));  // i.e.   20 < i <= 30

        cursor = coll.find(query);

        while(cursor.hasNext()) {
            System.out.println("Comparison =>"+cursor.next());
        }
        System.out.println("Total documents: "+coll.getCount());

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

Implement Google Signer to implement Google Map Search


 
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.springframework.stereotype.Service;
import com.test.gcrmws.core.context.GCRMWSContext;

@Service

public class GoogleUrlSigner {

public String getLicnsedGeoCoderURL(String address,String inputUrl, String inputKey, String clientId)
throws InvalidKeyException, NoSuchAlgorithmException,
URISyntaxException {
 String request = null;
 URL url = null;

try {

inputUrl=inputUrl+"?sensor=false"+"&address="+address+"&client="+clientId;

byte[] key = convertBase64toBinary(inputKey);
url = new URL(inputUrl);

request = signRequest(url.getPath(), url.getQuery(),key);

} catch (MalformedURLException e) {
GCRMWSContext.getLogger().logMessage(e.getMessage());
} catch (UnsupportedEncodingException e) {
GCRMWSContext.getLogger().logMessage(e.getMessage());
}
return request;
}

private byte[] convertBase64toBinary(String keyString) {

// Convert the key from 'web safe' base 64 to binary

keyString = keyString.replace('-', '+');
keyString = keyString.replace('_', '/');
byte[] key = Base64.decode(keyString);
return key;
}

private String signRequest(String path, String query,byte[] key)
throws NoSuchAlgorithmException, InvalidKeyException,

UnsupportedEncodingException, URISyntaxException {
// Retrieve the proper URL components to sign
String resource = path + '?' + query;
// Get an HMAC-SHA1 signing key from the raw key bytes
SecretKeySpec sha1Key = new SecretKeySpec(key, "HmacSHA1");
// Get an HMAC-SHA1 Mac instance and initialize it with the HMAC-SHA1
// key
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(sha1Key);

// compute the binary signature for the request
byte[] sigBytes = mac.doFinal(resource.getBytes());
// base 64 encode the binary signature
// String signature = Base64.encodeBytes(sigBytes);
String signature = Base64.encodeToString(sigBytes, true);
// convert the signature to 'web safe' base 64
signature = signature.replace('+', '-');
signature = signature.replace('/', '_');
return signature;
}
}

Main class – How to call this above method:

 


public it.units.GoogleGeocoding.GeocodeResponse getLocation(
StringBuilder geocodingIP, GoogleUrlSigner googleUrlSigner,

@GWSSuppressLogging Map<String, String> grlSyncLogMap,
RestConnector googleMapRestConnector, String addr,String... addressElements) {

Map<String, String> queryParams = new LinkedHashMap<String, String>();

GeocodeResponse geoResponse = null;

   String signature = null;
   StringBuilder address = new StringBuilder();

for (String addrelem : addressElements) {
  if (address.length() > 0) {
    address.append('+');
  }

try {

address.append(URLEncoder.encode(addrelem, "UTF-8").replace("+", "%20"));
} catch (UnsupportedEncodingException e) {

GCRMWSContext.getLogger().logMessage(e.getMessage());
}
}

Map<Param, String> map = googleMapRestConnector.getConfig();
String endpoint = java.text.MessageFormat.format(
map.get(Param.ENDPOINT_URL), geocodingIP);
map.put(Param.ENDPOINT_URL, endpoint);

try {

signature = googleUrlSigner.getLicnsedGeoCoderURL(
address.toString(), endpoint,
grlSyncLogMap.get("google.geocoder.key"),
grlSyncLogMap.get("google.geocoder.client"));
} catch (InvalidKeyException e) {

GCRMWSContext.getLogger().logMessage(e.getMessage());
} catch (NoSuchAlgorithmException e) {
GCRMWSContext.getLogger().logMessage(e.getMessage());
} catch (URISyntaxException e) {
GCRMWSContext.getLogger().logMessage(e.getMessage());
}

queryParams.put("sensor", "false");
queryParams.put("address", addr);
queryParams.put("client", grlSyncLogMap.get("google.geocoder.client"));
queryParams.put("signature", signature);

try {
geoResponse = (it.units.GoogleGeocoding.GeocodeResponse) googleMapRestConnector.invoke(HttpMethod.GET, null, null, queryParams);

/*

* System.out.println("LocationType: "+

* geoResponse.getResult().getGeometry().getLocationType());

* System.out.println("FormattedAddress: "+

* geoResponse.getResult().getFormattedAddress());

* System.out.println("AddressTypes: "+

* geoResponse.getResult().getAddressTypes());

* System.out.println("Lat: "+

* geoResponse.getResult().getGeometry().getLocation().getLat());

* System.out.println("Lan: "+

* geoResponse.getResult().getGeometry().getLocation().getLng());

*/

} catch (NullPointerException ne) {
GCRMWSContext.getLogger().logMessage(ne.getMessage());
} catch (RestException re) {
GCRMWSContext.getLogger().logMessage(re.getMessage());
} catch (Exception e) {
GCRMWSContext.getLogger().logMessage(e.getMessage());
}
return geoResponse;
}

 

Git vs SVN – Advantages of GIT


  • Integration– Git has very flexible integration with Stash(GIt UI), Bamboo (Build tool) , JIRA (Agile). We can associate feature (a separate branch of code for a given story) branch with any JIRA story ( part of a sprint) by creating branch from Stash. So that all code changes/commits can be easily audit/trackable. Bamboo create build and show status of build (pass or fail) to Stash/Git and JIRA board.
  • Cloning – GIT creates a separate mirror branch for a minor change.
  • Easy switching between different branches- feature, develop, release, master, tag with the same set of local code or same folder. No need to replicate like SVN. Code merge and roll back is also easy and quicker.
  • Forking – A new project can be created outside of your project space by external team. Other team can work in parallel and merge their code like XYZ team has their own developers and development process, however they can work on same set of code and later on merge and release using same original remote repository.
  • Easy Code Review– We can do peer review by using Git/Stash tool by creating pull request. There we have set rules like- at least 2 approvals,1 successful build etc.
  • Easy to manage various branches and code merge is easy by its own merge tool/console commands.
  • Light wight and faster than SVN.