<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>(function() { &#187; snippets</title>
	<atom:link href="http://maxb.net/blog/category/snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://maxb.net/blog</link>
	<description>// MAXB.NET LABS</description>
	<lastBuildDate>Mon, 16 May 2011 19:12:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>MyPearLogComposite</title>
		<link>http://maxb.net/blog/2009/04/24/mypearlogcomposite/</link>
		<comments>http://maxb.net/blog/2009/04/24/mypearlogcomposite/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 10:53:32 +0000</pubDate>
		<dc:creator>Massimiliano Balestrieri</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[log]]></category>
		<category><![CDATA[pear]]></category>

		<guid isPermaLink="false">http://maxb.net/blog/?p=324</guid>
		<description><![CDATA[Pubblico una classe per effettuare il log composito con PEAR::Log. In sviluppo il log potrà essere usato in maniera abbondante (entrata/uscita dei metodi ad esempio). Questo causerà frequenti accessi al file di log. In produzione la maschera impostata nello switch &#8230; <a href="http://maxb.net/blog/2009/04/24/mypearlogcomposite/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Pubblico una classe per effettuare il log composito con PEAR::Log.<br />
In sviluppo il log potrà essere usato in maniera abbondante (entrata/uscita dei metodi ad esempio). Questo causerà frequenti accessi al file di log.<br />
In produzione la maschera impostata nello switch eviterà l&#8217;accesso al file per i log di tipo  PEAR_LOG_DEBUG (valore di default) ma manterrà i log di tipo ERROR e i log di tipo EMERG.<br />
In più per il log di tipo EMERG verrà inviata una mail. Il metodo _rotate della classe garantirà una rotazione del file al raggiungimento di N bytes (vedi costante LOG_SIZE_ROTATION).</p>
<pre class="brush: php; title: ; notranslate">
set_include_path(dirname(__FILE__) .'/lib/PEAR-1.7.2/:'.get_include_path());

require_once(dirname(__FILE__) . '/lib/PEAR-1.7.2/PEAR.php');
require_once(dirname(__FILE__) . '/lib/Log-1.11.3/Log.php');
require_once(dirname(__FILE__) . '/lib/Mail-1.2.0b1/Mail.php');

$env = 1;
switch($env){
	case 0://development
		define('MASK_ERROR', PEAR_LOG_DEBUG);
	break;
	case 1://production
		define('MASK_ERROR', PEAR_LOG_ERR);
	break;
}
define('APPLICATION_NAME', 'mylog');
define('MAIL_MASK_ERROR', PEAR_LOG_EMERG);
define('MAIL_WEBMASTER', 'MAILTO');
define('MAIL_FROM', 'MAILFROM');
define('LOG_FILE', dirname(__FILE__) . '/logs/'.APPLICATION_NAME.'.txt');
define('LOG_SIZE_ROTATION',1000000);

class MyPearLogComposite{

	public $logger;

	public function __construct(){
		$this-&gt;_check_rotate();
		$conf = array('mode' =&gt; 0600, 'timeFormat' =&gt; '%X %x');
		$file = &amp;Log::singleton('file', LOG_FILE, APPLICATION_NAME , $conf);
		$mask = Log::MAX(MASK_ERROR);
		$file-&gt;setMask($mask);
		$conf = array('from' =&gt; MAIL_FROM,'subject' =&gt; APPLICATION_NAME . ' Important log events');
		$mail = &amp;Log::singleton('mail', MAIL_WEBMASTER, APPLICATION_NAME, $conf);
		$mask = Log::MAX(MAIL_MASK_ERROR);
		$mail-&gt;setMask($mask);
		$this-&gt;logger = &amp;Log::singleton('composite');
		$this-&gt;logger-&gt;addChild($file);
		$this-&gt;logger-&gt;addChild($mail);
	}
	private function _check_rotate(){
		$size = @filesize(LOG_FILE);
		if($size &gt; LOG_SIZE_ROTATION)
        	$this-&gt;_rotate();

	}
	private function _rotate(){
		$file = LOG_FILE . '.' . date('Y-m-d');
        rename(LOG_FILE, $file);
	}
}

$log = new MyPearLogComposite();

$log-&gt;logger-&gt;log('log something');
$log-&gt;logger-&gt;log('log log');
$log-&gt;logger-&gt;log('log error', PEAR_LOG_ERR);
$log-&gt;logger-&gt;log('emergency', PEAR_LOG_EMERG);

//in development
//log file:
/*
12:32:30 24/04/2009 mylog [info] log something
12:32:30 24/04/2009 mylog [info] log log
12:32:30 24/04/2009 mylog [error] log error
12:32:30 24/04/2009 mylog [emergency] emergency
*/
//mail:
/*
To: MAILTO
Subject: mylog Important log events
From: MAILFROM
User-Agent: PEAR Log Package
Apr 24 12:40:25 mylog [emergency] emergency
*/

//in production
//log file:
/*
12:42:25 04/24/09 mylog [error] log error
12:42:25 04/24/09 mylog [emergency] emergency
*/
//mail:
/*
To: MAILTO
Subject: mylog Important log events
From: MAILFROM
User-Agent: PEAR Log Package
Apr 24 12:42:25 mylog [emergency] emergency
*/
</pre>
]]></content:encoded>
			<wfw:commentRss>http://maxb.net/blog/2009/04/24/mypearlogcomposite/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

