<?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>Bytearray - Programming, Computers and Technology &#187; Perl</title>
	<atom:link href="http://bytearray.brixtonjunkies.com/category/code/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://bytearray.brixtonjunkies.com</link>
	<description></description>
	<lastBuildDate>Tue, 03 Nov 2009 11:52:07 +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>Perl: Unbuffered File Logging Module</title>
		<link>http://bytearray.brixtonjunkies.com/2009/08/21/perl-unbuffered-file-logging-module/</link>
		<comments>http://bytearray.brixtonjunkies.com/2009/08/21/perl-unbuffered-file-logging-module/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 17:54:38 +0000</pubDate>
		<dc:creator>alchemist</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[file logger]]></category>
		<category><![CDATA[logger]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[logging module]]></category>

		<guid isPermaLink="false">http://bytearray.brixtonjunkies.com/?p=217</guid>
		<description><![CDATA[This an old Perl module I dug up which is quite a handy, there is no file write buffering so if you do a tail -f on the log file while it&#8217;s writing you can see real time what is going into the log file. Logger Module Logger Module Usage]]></description>
			<content:encoded><![CDATA[<p>This an old Perl module I dug up which is quite a handy, there is no file <span style="text-decoration: underline;">write buffering</span> so if you do a <strong>tail -f </strong>on the log file while it&#8217;s writing you can see real time what is going into the log file.</p>
<p><strong><br />
Logger Module</strong></p>
<pre class="brush: perl; title: ; notranslate">
package Logger;
# Logger.pm
#---------------------------------------------------
#
# Basic module to log to file
#
#---------------------------------------------------
# Constructor for logger module

sub new{
    my $class = shift;
    my $args = shift;

    # Default is to create a new logfile, NOT to append to it
    my $append = 0;
    if (defined($args-&gt;{append})){
		$append = $args-&gt;{append};
    }

    my $disable = 0;
    if (defined($args-&gt;{disable})){
		$disable = $args-&gt;{disable};
    }

    my $self = { filename =&gt; $args-&gt;{filename},
		 		 append =&gt; $append,
		 		 line_no =&gt; 0,
		 		 disable =&gt; $disable
	     };

    bless($self,$class);
    $self-&gt;_init();
}

#---------------------------------------------------
# Initialise the log file

sub _init{
    my $self = shift;
    if (!$self-&gt;{disable}){
		$filename = $self-&gt;{filename};

		if ($self-&gt;{append}){
	    	# Open a file for appending in write mode
	    	open(LOG,&quot;&gt;&gt; $filename&quot;);
	    	select LOG;
	    	$| = 1;
	    	select STDOUT;
	    	$self-&gt;{log_handle} = *LOG;
		}
		else{
	    	# Create a new file in write mode
	    	open(LOG,&quot;&gt; $filename&quot;);
	    	select LOG;
	    	$| = 1;
	    	select STDOUT;
	    	$self-&gt;{log_handle} = *LOG;
		}
    }

    return $self;
}

#---------------------------------------------------
# Add to the log file

sub add{
    my $self = shift;
    if (!$self-&gt;{disable}){
		my $str = shift;

		my $handle = $self-&gt;{log_handle};
		my $timestamp = localtime(time);
		my $line_no = ++$self-&gt;{line_no};
		print $handle &quot;[$timestamp#$line_no]: &quot; . $str . &quot;\n&quot;;
    }
}

#---------------------------------------------------
# Close log file

sub close{
    my $self = shift;
    if (!$self-&gt;{disable}){
		my $handle = $self-&gt;{log_handle};
		close($handle);
    }
}

#---------------------------------------------------
1;
</pre>
<p><strong><br />
Logger Module Usage</strong></p>
<pre class="brush: perl; title: ; notranslate">
#!/usr/bin/perl -w

use strict;
use lib 'MyPerlLibDir/';
use Logger;

my $myLogString = 'This text will appear in the log file';

my $loggerObj = new Logger({ append =&gt; 0, disable =&gt; 0, filename =&gt; 'MyLogFile.txt'  });
$loggerObj-&gt;add($myLogString);
$loggerObj-&gt;close();
</pre>
]]></content:encoded>
			<wfw:commentRss>http://bytearray.brixtonjunkies.com/2009/08/21/perl-unbuffered-file-logging-module/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

