<?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; logging module</title>
	<atom:link href="http://bytearray.brixtonjunkies.com/tag/logging-module/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>Simple Java File Logging</title>
		<link>http://bytearray.brixtonjunkies.com/2009/09/30/simple-java-file-logging/</link>
		<comments>http://bytearray.brixtonjunkies.com/2009/09/30/simple-java-file-logging/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 10:21:19 +0000</pubDate>
		<dc:creator>alchemist</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Java]]></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=513</guid>
		<description><![CDATA[Not much to say about this one, a small one off independent class to simplify the process of logging stuff to file. Something like this is often handy when writing backgrounded daemon processes. FileLogger class Usage:]]></description>
			<content:encoded><![CDATA[<p>Not much to say about this one, a small one off independent class to simplify the process of logging stuff to file. Something like this is often handy when writing backgrounded daemon processes.</p>
<p><em><strong>FileLogger class</strong></em></p>
<pre class="brush: java; title: ; notranslate">
package Utils;

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FileLogger{

	private File logFile;

	private long logEntryNo = 0;

	private static FileLogger instance = null;

	public static final String DEFAULT_LOG_FILEPATH = &quot;bin/Log/Daemon.log&quot;;

	public FileLogger(String logFilePath){
		logFile = new File(logFilePath);
		try{
			logFile.createNewFile();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		calcNumLines();
		instance = this;
	}

	public FileLogger(File logFile){
		this.logFile = logFile;
		calcNumLines();
		instance = this;
	}

	public static FileLogger getInstance(){
		return instance;
	}

	/**
	 * Adds and entry in the log file but also echos the log to standard out
	 *
	 * @param data
	 * @param printToStdOut
	 */

	public void add(String data,boolean printToStdOut){
		if (printToStdOut){
			System.out.print(&quot;\n&quot; + data + &quot;\n&quot;);
		}
		add(data);
	}

	/**
	 * Add a log file entry
	 *
	 * @param data
	 */

	public synchronized void add(String data){
		DateFormat dateFormat = new SimpleDateFormat(&quot;dd.MM.yyyy HH:mm:ss&quot;);
		Date date = new Date();
		String dateTime = dateFormat.format(date);

		try{
			if (logFile.createNewFile()){
				FileWriter fileWriter = new FileWriter(logFile);
				BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
				bufferedWriter.write(&quot;[&quot; + dateTime +  &quot;]#&quot; + ++logEntryNo + &quot;: &quot; + data);
				bufferedWriter.newLine();
				bufferedWriter.close();
			}
			else{
				FileWriter fileWriter = new FileWriter(logFile,true);
				BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
				bufferedWriter.write(&quot;[&quot; + dateTime +  &quot;]#&quot; + ++logEntryNo + &quot;: &quot; + data);
				bufferedWriter.newLine();
				bufferedWriter.close();
			}
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void calcNumEntries(){
		try {
			FileReader fileReader = new FileReader(logFile);
			BufferedReader bufferedFileReader = new BufferedReader(fileReader);

			try{
				String line;
				while((line = bufferedFileReader.readLine()) != null){
					Pattern p = Pattern.compile(&quot;^\\[\\d+\\.\\d+\\.\\d+\\s+\\d+:\\d+:\\d+]#\\d+&quot;);
					Matcher m = p.matcher(line);

					if (m.find()){
						logEntryNo++;
					}
				}
				bufferedFileReader.close();
			}
			catch (IOException e){
				e.printStackTrace();
			}
		}
		catch (FileNotFoundException e){
			// The log file hasn't been created yet so gracefully ignore that fact
		}
	}

}
</pre>
<p><strong>Usage:</strong></em></p>
<pre class="brush: java; title: ; notranslate">
FileLogger myFileLogger = new FileLogger(FileLogger.DEFAULT_LOG_FILEPATH);
myFileLogger.add(&quot;my log entry&quot;);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://bytearray.brixtonjunkies.com/2009/09/30/simple-java-file-logging/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>

