Adding color to your tail

Tail is a very useful tool for monitoring error stream. Sometime the output from tail can have too much information and its black and white monotone output can be hard to follow with the eyes.

The basic: Linux terminal has the capability to colorize text.
for example:
$ echo -e “Sample text nicely highlighted”
will produce a simple text saying, well:
Sample text nicely highlighted

But with some terminal color tagging such as:
$ echo -e “Sample \e[36mtext\e[0m nicely highlighted”
The \e above is identical with \033. I would suggest use \033 because it is safer to be used with a programming language such as PHP. PHP will not recognize \e but it will recognize \033, you’ll see below.
$ echo -e “Sample \033[36mtext\033[0m nicely highlighted”
Sample text nicely highlighted

Some basic coloring table can be seen here: http://www.bashguru.com/2010/01/shell-colors-colorizing-shell-scripts.html

Color Foreground Background
Black 30 40
Red 31 41
Green 32 42
Yellow 33 43
Blue 34 44
Magenta 35 45
Cyan 36 46
White 37 47

With the help of perl, which most likely comes with all linux distros, here is a way to colorize your tail output. Lets’ assume that every time you log something you will have a date-time prefixing your log. For example:

[22-Dec-2011 20:28:45] E_DATASOMETHING ……
Too much information …….
Too much information …….
[22-Dec-2011 20:28:46] E_FATAL Something
Too much information …….
Too much information …….
Too much information …….

Lets create a script that can colorize the date portion.
Create an executable linux bash file:


$ touch logwatch.sh
$ chmod 755 logwatch.sh
$ vim logwatch.sh

The copy and paste the following to your empty logwatch.sh

#!/bin/bash
vNow=$(date +"%d-%b-%Y")
tail -f ~username/errorlogfile.txt|perl -pe "s/$vNow/\e[1;30;32m$&\e[0m/g"

Voilà!

[22-Dec-2011 20:28:45] E_DATASOMETHING ……
Too much information …….
Too much information …….
[22-Dec-2011 20:28:46] E_FATAL Something
Too much information …….
Too much information …….
Too much information …….

If you need something more complex, say you want to highlight several words on the file you can use a scripting power of PHP, I’m rusty with my pearl.
Anyhow, save the script below as “colorize.php”, and you can have the words: blah, na, wa, and — highlighted on the fly!

$ tail -f test.txt|php colorize.php blah na wa --


JSON Jumpstart


/**
* In JSON curly braket is a sign of a new object ({ or })
* Any variable inside the object, also known as object's attribute, is accessible using a period sign (.) object.attribute
* or you can also acces it using an "associative array" such as object['attribute']
* You'll need a Firefox with Firebug extension to see the result from console.log or IE9 with its built-in Developer Tools
*
*/

// =========================
// JSON Object
// =========================
var var1 = {
'var1_1':'value1_1',
'var1_2':'value1_2'
};
console.log(var1);
// The line above will output> Object { var1_1="value1_1", var1_2="value1_2"}
console.log(var1.var1_1);
// The line above will output> value1_1
console.log(var1['var1_1']);
// The line above will output> value1_1

// =========================
// JSON Array
// =========================
// In JSON square braket is a sign of an array ([ or ])
// Any array element is accessible by using a square bracket and followed by an index (0,1,2,...)
var var2 = ['value2_1', 'value2_2'];
console.log(var2);
// The line above will output> ["value2_1", "value2_2"]
console.log(var2[0]);
// The line above will output> value2_1

var var3 = [{
'var3_1':'value3_1',
'var3_2':'value3_2'
},{
'var3_3':'value3_3',
'var3_4':'value3_4'
}];
console.log(var3);
// The line above will output> [Object { var3_1="value3_1", var3_2="value3_2"}, Object { var3_3="value3_3", var3_4="value3_4"}]
console.log(var3[0].var3_1);
// The line above will output> value3_1

Vodafone Group, AT&T, and Verizon are The Biggest Yielding Companies In November 2011

Vodafone Group, the telecom giant known for its land line and wireless services around the U.S and U.K, together with two other U.S based telecom giants AT&T and Verizon are one of the best yielding companies in Nov 2011. As per today’s data, 2011-11-02, Vodafone is yielding 6.8% yearly, AT&T 5.9% and Verizon is 5.3%. Is this a good time to invest in these telecom companies? You might also want to see the list of stocks with the highest yield displayed in www.greenandredmarket.com‘s highest yield page.

Find Files in Linux by Name

To find files in linux with a certain name portion or pattern we can issue a locate locate command.
If locate is unable to find a matching file, then you might need to update your file index database.

# locate journal-2011-07
# updatedb

In the example above we are trying to find a file that has journal-2011-07 in its name which can be journal-2011-07-01, journal-2011-07-02, my-journal-2011-07-09, etc.

You can also use the find command. The find command does not use file indexing algorithm which might be slower than locate.
The example below will look for all files with xml extension inside /home/ronald.
The find command by default is recursive or it will look into subfolders.

find /home/ronald/ -name *.xml

Check For Loaded PHP Extensions

Sometimes you want to make sure a certain php extension is loaded or not. These extension can be curl, xmlrpc, or ldap. After all when you move your application from one server to another the configuration not necessarily the same. So here how you do check for loaded or existing php extensions using get_loaded_extensions().


$extNeeded = array('ldap', 'mysql', 'xmlrpc','ron_ext');
$loadedExtension = get_loaded_extensions();
foreach($extNeeded as $ext){ 
 if(!in_array($ext, $loadedExtension)){
  echo 'The application cannot start properly, '.$ext.' extension is missing. 
  The following extensions are needed: '.implode(', ', $extNeeded).'. 
  Terminating application ...';
  die;
 }
}

Extending an Existing Javascript Function

Extending an existing javascript function

This is how you extend an existing javascript function.
Assuming that you want to preserve the existing function and the “added algorithm” is somewhat only needed to be executed based on a certain environment only.


function abc(){
  alert('abc is called');       
}      
var abcOrig = abc;
abc = function(){
  abcOrig();
  alert('abc has been extended!');
}
alert('test 1');
abc();
alert('test 2');
abc();      

Map Network Drive using DOS command

Create a batch file named “mapdrive.bat”
Write the following text into its content


REM Map network Drive
net use j: \\server1\john_music
net use p: \\server2\pdfs
pause

Explaination:
Line 1 is to create a comment
Line 2 (similar with line 3) is to map a network folder \\server1\john_music to drive J:>
Line 4 is to pause the DOS window so it won’t close automatically

Super Powerful Debugging Function in Javascript

Super powerful debugging function in javascript. This function will output your variable in question to firebug console or html page.
With this function you can debug anything from Object, array, string, integer, etc. It is also recursive safe.


/**
 * @author: Ronald Pringadi, 2011
 * @desc: to debug a JavaScript Object
 * @usage: debug(yourObject);  
 * or debug(yourString); 
 * or debug(yourArray);  
 * @param object vTraceObject
 * @param string vPadding
 * @param object vPadding
 * 
 */

function debug(vTraceObject, vPadding, vPreviousTraceObject) {
	'use strict';

	function getObjectClass(vObj) {

		if (vObj && vObj.constructor && vObj.constructor.toString) {
			var arr = vObj.constructor.toString().match(/function\s*(\w+)/);
			if (arr && arr.length === 2) {
				return arr[1];
			}
		}
		return undefined;
	}
	function typeOf(vObj) {

		var vResult = typeof vObj;
		if (vResult === 'object') {
			try {
				if (vObj.length !== undefined) {
					vResult = 'array';
				}
			} catch (e) {
			}
		}

		return vResult;
	}	
	function print(vPadding, vType, vValue) {

		if (vPadding === undefined) {
			vPadding = '';
		}
		if (vType === undefined) {
			vType = '';
		}
		if (vValue === undefined) {
			vValue = '';
		}
		try{
			console.log(vPadding + ' (' + vType + '): ' + vValue + "\n");
		}catch(e){
			document.write(vPadding + ' (' + vType + '): ' + vValue + "\n");
		}
		
	}	

	if (vPadding === '' || vPadding === undefined) {
		vPadding = '';
	}
	if (typeOf(vTraceObject) === 'object'
			&& typeOf(vPreviousTraceObject) === 'object') {
		if (getObjectClass(vPreviousTraceObject) === getObjectClass(vTraceObject)) {
			print(vPadding, 'ERROR', 'RECURSION EXISTING ...');
			return null;
		}
	}

	var vType = typeOf(vTraceObject);
	if (vType === 'object' || vType === 'array') {
		if (vPadding === '') {
			print(vPadding, vType, '...');
			vPadding = '----';

		}
		try {
			var vTempType = '';
			var variable = '';
			for (variable in vTraceObject) {
				vTempType = typeOf(vTraceObject[variable]);
				if (vTempType === 'object' || vTempType === 'array') {
					print(vPadding, vTempType, variable);
					debug(vTraceObject[variable], vPadding + '----',
							vTraceObject);
				} else {
					print(vPadding, vTempType, variable + ' : '
							+ vTraceObject[variable]);
				}
			}
		} catch (e) {
			print(vPadding, 'ERR', 'EXCEPTION');
		}
	} else {
		print(vPadding, vType, vTraceObject);

	}


}

function objA() {
	this.varA1 = 'a1';
	this.varA2 = 'a2';
	this.myCars = new Array();
	this.myCars[0] = "Saab";
	this.myCars[1] = "Volvo";
	this.myCars[2] = "BMW";
}

function objB() {
	this.varB1 = 'b1';
	this.varB2 = 'b2';
	this.varB3 = new objA;

}
instanceB = new objB;

Output

(object): ...
---- (string): varB1 : b1
---- (string): varB2 : b2
---- (object): varB3
-------- (string): varA1 : a1
-------- (string): varA2 : a2
-------- (array): myCars
------------ (string): 0 : Saab
------------ (string): 1 : Volvo
------------ (string): 2 : BMW