Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 11-15 of 15 total

Watch a log and/or search for a certain term

If you're developing on a server that has all PHP errors turned off, you can still watch the error log (if you have access to this file). For example on a dreamhost account you could try this:

tail -f /home/account/logs/website.com/http/error.log

As the errors pour in, you will see them come by. Change 'account' to the username you have on dreamhost and website.com to the domainname you host there. If the errors come in large amounts you can filter them by using grep:

tail -f /home/account/logs/website.com/http/error.log | grep -i "search_term"


Now only errors containing 'search_term' will be shown.

Apache log anonymizer

/**
* Takes Apache log file at $log_path, hashes the client address, and 
* writes the file to $output_path. Useful for sharing log files without 
* compromising user privacy. Reads input line-by-line to handle very 
* large logs.
*
* @version 1.0
* @author Scott Reynen
* @copyright Scott Reynen 2006
* @link http://httpd.apache.org/docs/1.3/logs.html#accesslog
*/

$log_path = '';
$output_path = '';

$file = fopen( $log_path , 'r' );
$output = fopen( $output_path , 'a' );

while ( ! feof( $file ) ) 
{

	$line = fgets( $file , 8192 );
	$space = strpos( $line , ' ' );
	$out_line = md5( substr( $line , 0 , $space ) );
	$out_line.= substr( $line , $space );
	fwrite( $output , $out_line );

} // while

fclose( $file );
fclose( $output );

Textpattern - display log by hostname


<html>
<head>
<link href="/textpattern/textpattern.css" rel="Stylesheet" type="text/css" />
</head>
<body>
<?php
require_once("textpattern/config.php");
mysql_connect($txpcfg['host'], $txpcfg['user'], $txpcfg['pass']) or die(mysql_error());
mysql_select_db($txpcfg['db']) or die(mysql_error());

// Quote variable to make safe
function quote_smart($value)
{
   // Stripslashes
   if (get_magic_quotes_gpc()) {
       $value = stripslashes($value);
   }
   // Quote if not a number or a numeric string
   if (!is_numeric($value)) {
       $value = mysql_real_escape_string($value);
   }
   return $value;
}
$sql = "SELECT * FROM " .$txpcfg['table_prefix'] ."txp_log WHERE host LIKE '%" .quote_smart($_GET['host']) ."%' ORDER BY 'time' DESC LIMIT 0, 30 ";
$result = mysql_query($sql) or die(mysql_error());
echo "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" id=\"list\" align=\"center\">";
echo "<tr><td><b>Time</b></td><td><b>host</b></td><td><b>page</b></td></tr>";
while($row = mysql_fetch_array($result))
{
 echo "<tr><td>".$row['time']."</td><td>".$row['host']."</td><td>".$row['page']."</td></tr>";
}
echo "</table>";
?>
</body></html>


How to rotate your Rails logs by size, so you can allocate a certain amount of space for it.

Add this to RAILS_ROOT/config/environment.rb to keep 50 logfiles of 1MB each.
config.logger = Logger.new("#{RAILS_ROOT}/log/#{ENV['RAILS_ENV']}.log", 50, 1048576)

Class rename friendly log instance

Use the following to automatically use the current class name for the log category (example is for commons-logging, works with log4j, too):

class Foo {
   private final Log log = LogFactory.getLog(getClass());
}


Compare to the following where the class name is refered to explicitely. If the class is renamed or parts of the codes are copied ("reused") the logging will by misleading.

class Foo {
   private static final Log log = LogFactory.getLog(Foo.class);
}



« Newer Snippets
Older Snippets »
Showing 11-15 of 15 total