<?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>Untitled &#187; logfiles</title>
	<atom:link href="http://ammonlauritzen.com/blog/tag/logfiles/feed/" rel="self" type="application/rss+xml" />
	<link>http://ammonlauritzen.com/blog</link>
	<description>and still for good reason.</description>
	<lastBuildDate>Tue, 13 Dec 2011 21:29:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>dead simple traditional style rotation</title>
		<link>http://ammonlauritzen.com/blog/2010/02/05/dead-simple-traditional-style-rotation/</link>
		<comments>http://ammonlauritzen.com/blog/2010/02/05/dead-simple-traditional-style-rotation/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 17:51:26 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[logfiles]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/?p=1105</guid>
		<description><![CDATA[In response to my two-step rotation post earlier this week, I figure I may as well share the logic I use for a more traditional logfile rotation scheme. I think this is as simple as I can possibly make it: &#38;lt;? define&#40; 'MAX_COPIES', 3 &#41;; $back_fname = &#38;quot;/path/to/log/file/abc.log&#38;quot;; function trace&#40; $msg &#41; &#123; &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>In response to <a href='http://ammonlauritzen.com/blog/2010/02/03/simple-two-step-logfile-rotation/'>my two-step rotation post</a> earlier this week, I figure I may as well share the logic I use for a more traditional logfile rotation scheme.</p>
<p>I think this is as simple as I can possibly make it:</p>
<div class="syntax_hilite">
<div id="php-3">
<div class="php">&amp;lt;?<br />
<a href="http://www.php.net/define"><span style="color:#000066;">define</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#FF0000;">'MAX_COPIES'</span>, <span style="color:#CC66CC;">3</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#0000FF;">$back_fname</span> = &amp;quot;/path/to/<a href="http://www.php.net/log"><span style="color:#000066;">log</span></a>/<a href="http://www.php.net/file"><span style="color:#000066;">file</span></a>/abc.log&amp;quot;;</p>
<p><span style="color:#000000; font-weight:bold;">function</span> trace<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$msg</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> &amp;quot;- <span style="color:#0000FF;">$msg</span>\n&amp;quot;;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p><a href="http://www.php.net/exec"><span style="color:#000066;">exec</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;ls -r $<span style="color:#006600; font-weight:bold;">&#123;</span>back_fname<span style="color:#006600; font-weight:bold;">&#125;</span>*&amp;quot;, <span style="color:#0000FF;">$copies</span>, <span style="color:#0000FF;">$succ</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/count"><span style="color:#000066;">count</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$copies</span><span style="color:#006600; font-weight:bold;">&#41;</span>&amp;gt;= MAX_COPIES <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#0000FF;">$fname</span> = <a href="http://www.php.net/array_shift"><span style="color:#000066;">array_shift</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$copies</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;deleting &amp;quot;.<span style="color:#0000FF;">$fname</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#0000FF;">$next</span> = <a href="http://www.php.net/count"><span style="color:#000066;">count</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$copies</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$fname</span> = <a href="http://www.php.net/array_shift"><span style="color:#000066;">array_shift</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$copies</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; --<span style="color:#0000FF;">$next</span>;<br />
&nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;rotating <span style="color:#0000FF;">$fname</span> -&amp;gt; <span style="color:#0000FF;">$next</span>&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <a href="http://www.php.net/rename"><span style="color:#000066;">rename</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$fname</span>, &amp;quot;<span style="color:#0000FF;">$back_fname</span>.<span style="color:#0000FF;">$next</span>&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p>trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;creating <span style="color:#0000FF;">$back_fname</span>&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<a href="http://www.php.net/touch"><span style="color:#000066;">touch</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$back_fname</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
?&amp;gt;</div>
</div>
</div>
<p></p>
<p>A sample series of executions might look like this:</p>
<div class="syntax_hilite">
<div id="code-4">
<div class="code">ammon@wernstrom:/path/to/log/file$ touch abc.<span style="">log</span><br />
ammon@wernstrom:/path/to/log/file$ php rotate.<span style="">php</span> <br />
- rotating /path/to/log/file/abc.<span style="">log</span> -&amp;gt; <span style="color:#800000;">0</span><br />
- creating /path/to/log/file/abc.<span style="">log</span><br />
ammon@wernstrom:/path/to/log/file$ php rotate.<span style="">php</span> <br />
- rotating /path/to/log/file/abc.<span style="">log</span>.<span style="color:#800000;">0</span> -&amp;gt; <span style="color:#800000;">1</span><br />
- rotating /path/to/log/file/abc.<span style="">log</span> -&amp;gt; <span style="color:#800000;">0</span><br />
- creating /path/to/log/file/abc.<span style="">log</span><br />
ammon@wernstrom:/path/to/log/file$ php rotate.<span style="">php</span> <br />
- rotating /path/to/log/file/abc.<span style="">log</span>.<span style="color:#800000;">1</span> -&amp;gt; <span style="color:#800000;">2</span><br />
- rotating /path/to/log/file/abc.<span style="">log</span>.<span style="color:#800000;">0</span> -&amp;gt; <span style="color:#800000;">1</span><br />
- rotating /path/to/log/file/abc.<span style="">log</span> -&amp;gt; <span style="color:#800000;">0</span><br />
- creating /path/to/log/file/abc.<span style="">log</span><br />
ammon@wernstrom:/path/to/log/file$ php rotate.<span style="">php</span> <br />
- deleting /path/to/log/file/abc.<span style="">log</span>.<span style="color:#800000;">2</span><br />
- rotating /path/to/log/file/abc.<span style="">log</span>.<span style="color:#800000;">1</span> -&amp;gt; <span style="color:#800000;">2</span><br />
- rotating /path/to/log/file/abc.<span style="">log</span>.<span style="color:#800000;">0</span> -&amp;gt; <span style="color:#800000;">1</span><br />
- rotating /path/to/log/file/abc.<span style="">log</span> -&amp;gt; <span style="color:#800000;">0</span><br />
- creating /path/to/log/file/abc.<span style="">log</span></div>
</div>
</div>
<p></p>
<p>This doesn't have any failsafes, doesn't compress anything, depends on an external call to 'ls', and it actually deletes old files in stead of overwriting them... but it is the shortest, simplest method I've come up with to get the job done.</p>
<p>If I feel like making this a full-fledged series, I might actually post a more thorough implementation later <img src='http://ammonlauritzen.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2010/02/05/dead-simple-traditional-style-rotation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>simple two-step logfile rotation</title>
		<link>http://ammonlauritzen.com/blog/2010/02/03/simple-two-step-logfile-rotation/</link>
		<comments>http://ammonlauritzen.com/blog/2010/02/03/simple-two-step-logfile-rotation/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 19:23:08 +0000</pubDate>
		<dc:creator>Ammon</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[logfiles]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://ammonlauritzen.com/blog/?p=1103</guid>
		<description><![CDATA[This is the result of 10 minutes of pounding on the keyboard after yet another disappointing experience with trying to get logrotate to do something vaguely more flexible. This simple script scans all normal files in a log directory, and if they are older than a certain cutoff, moves them into a holding directory for [...]]]></description>
			<content:encoded><![CDATA[<p>This is the result of 10 minutes of pounding on the keyboard after yet another disappointing experience with trying to get logrotate to do something vaguely more flexible.</p>
<p>This simple script scans all normal files in a log directory, and if they are older than a certain cutoff, moves them into a holding directory for old logs. Future passes will check files in the old directory for another age setting and will delete them. That's all there is to it.</p>
<p>Configure your cutoffs, directories of interest, and optionally plug in a better logging mechanism and you're set. (Oh, and change the #! if necessary, of course).</p>
<div class="syntax_hilite">
<div id="php-6">
<div class="php"><span style="color:#008000; font-style:italic;">#!/usr/bin/php</span><br />
&amp;lt;?<br />
<span style="color:#0000FF;">$cutoff_rotate</span> = &amp;quot;<span style="color:#CC66CC;">3</span> days&amp;quot;;<br />
<span style="color:#0000FF;">$cutoff_delete</span> = &amp;quot;<span style="color:#CC66CC;">7</span> days&amp;quot;;<br />
<span style="color:#0000FF;">$dir_log</span> = &amp;quot;/logs&amp;quot;;<br />
<span style="color:#0000FF;">$dir_old</span> = &amp;quot;/logs.old&amp;quot;;</p>
<p><span style="color:#000000; font-weight:bold;">function</span> trace<span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$msg</span>, <span style="color:#0000FF;">$debug</span> = <span style="color:#000000; font-weight:bold;">FALSE</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// appropriate logging mechanism can be plugged in here</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/echo"><span style="color:#000066;">echo</span></a> &amp;quot;<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#0000FF;">$msg</span>\n&amp;quot;;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p><span style="color:#FF9933; font-style:italic;">// scan old files for deletion</span><br />
<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/is_dir"><span style="color:#000066;">is_dir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$dir_old</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#0000FF;">$dh</span> = <a href="http://www.php.net/opendir"><span style="color:#000066;">opendir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$dir_old</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$dh</span> !== <span style="color:#000000; font-weight:bold;">FALSE</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/chdir"><span style="color:#000066;">chdir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$dir_old</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;scanning <span style="color:#0000FF;">$dir_old</span> <span style="color:#616100;">for</span> logs more than <span style="color:#0000FF;">$cutoff_delete</span> old&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$cutoff</span> = <a href="http://www.php.net/strtotime"><span style="color:#000066;">strtotime</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;-<span style="color:#0000FF;">$cutoff_delete</span>&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;cutoff is &amp;quot;.<a href="http://www.php.net/date"><span style="color:#000066;">date</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'r'</span>,<span style="color:#0000FF;">$cutoff</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$file</span> = <a href="http://www.php.net/readdir"><span style="color:#000066;">readdir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$dh</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> !== <span style="color:#000000; font-weight:bold;">FALSE</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/is_dir"><span style="color:#000066;">is_dir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$file</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;skipping <span style="color:#0000FF;">$file</span>&amp;quot;, <span style="color:#000000; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">continue</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$ts</span> = <a href="http://www.php.net/filemtime"><span style="color:#000066;">filemtime</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$file</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$ts</span> &amp;lt;<span style="color:#0000FF;">$cutoff</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;deleting <span style="color:#0000FF;">$file</span>, &amp;quot;.<a href="http://www.php.net/date"><span style="color:#000066;">date</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'r'</span>,<span style="color:#0000FF;">$ts</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$succ</span> = @<a href="http://www.php.net/unlink"><span style="color:#000066;">unlink</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$file</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$succ</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;failed to <a href="http://www.php.net/unlink"><span style="color:#000066;">unlink</span></a> <span style="color:#0000FF;">$file</span>!&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;ignoring <span style="color:#0000FF;">$file</span>, &amp;quot;.<a href="http://www.php.net/date"><span style="color:#000066;">date</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'r'</span>,<span style="color:#0000FF;">$ts</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#000000; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/closedir"><span style="color:#000066;">closedir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$dh</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;no old <a href="http://www.php.net/log"><span style="color:#000066;">log</span></a> <a href="http://www.php.net/dir"><span style="color:#000066;">dir</span></a> <span style="color:#0000FF;">$dir_old</span> to scan yet&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p><span style="color:#FF9933; font-style:italic;">// scan current files for rotation</span><br />
<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/file_exists"><span style="color:#000066;">file_exists</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$dir_old</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;creating <span style="color:#0000FF;">$dir_old</span>&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#0000FF;">$succ</span> = @<a href="http://www.php.net/mkdir"><span style="color:#000066;">mkdir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$dir_old</span>, <span style="color:#CC66CC;">0775</span>, <span style="color:#000000; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$succ</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;mkdir failed, aborting rotation&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/exit"><span style="color:#000066;">exit</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC66CC;">1</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/is_dir"><span style="color:#000066;">is_dir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$dir_log</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#0000FF;">$dh</span> = <a href="http://www.php.net/opendir"><span style="color:#000066;">opendir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$dir_log</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$dh</span> !== <span style="color:#000000; font-weight:bold;">FALSE</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/chdir"><span style="color:#000066;">chdir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$dir_log</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;scanning <span style="color:#0000FF;">$dir_log</span> <span style="color:#616100;">for</span> logs more than <span style="color:#0000FF;">$cutoff_rotate</span> old&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$cutoff</span> = <a href="http://www.php.net/strtotime"><span style="color:#000066;">strtotime</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;-<span style="color:#0000FF;">$cutoff_delete</span>&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;cutoff is &amp;quot;.<a href="http://www.php.net/date"><span style="color:#000066;">date</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'r'</span>,<span style="color:#0000FF;">$cutoff</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$file</span> = <a href="http://www.php.net/readdir"><span style="color:#000066;">readdir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$dh</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> !== <span style="color:#000000; font-weight:bold;">FALSE</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <a href="http://www.php.net/is_dir"><span style="color:#000066;">is_dir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$file</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;skipping <span style="color:#0000FF;">$file</span>&amp;quot;, <span style="color:#000000; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">continue</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$ts</span> = <a href="http://www.php.net/filemtime"><span style="color:#000066;">filemtime</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$file</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$ts</span> &amp;lt;<span style="color:#0000FF;">$cutoff</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;rotating <span style="color:#0000FF;">$file</span>, &amp;quot;.<a href="http://www.php.net/date"><span style="color:#000066;">date</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'r'</span>,<span style="color:#0000FF;">$ts</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#000000; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$succ</span> = @<a href="http://www.php.net/rename"><span style="color:#000066;">rename</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$file</span>, <span style="color:#0000FF;">$dir_old</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#616100;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> !<span style="color:#0000FF;">$succ</span> <span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;failed to rotate <span style="color:#0000FF;">$file</span>!&amp;quot; <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#616100;">else</span> <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trace<span style="color:#006600; font-weight:bold;">&#40;</span> &amp;quot;ignoring <span style="color:#0000FF;">$file</span>, &amp;quot;.<a href="http://www.php.net/date"><span style="color:#000066;">date</span></a><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'r'</span>,<span style="color:#0000FF;">$ts</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#000000; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; <a href="http://www.php.net/closedir"><span style="color:#000066;">closedir</span></a><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0000FF;">$dh</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
<span style="color:#006600; font-weight:bold;">&#125;</span><br />
?&amp;gt;</div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://ammonlauritzen.com/blog/2010/02/03/simple-two-step-logfile-rotation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

