Log::CSVLogger - Log::CSVLogger documentation


Log-CSVLogger documentation Contained in the Log-CSVLogger distribution.

Index


Code Index:

NAME

Top

Log::CSVLogger

SYNOPSIS

Top

    $csvlogger = Log::CSVLogger->new("path/to/log.csv");

    $csvlogger->debug("Debug Message1","Debug Message2");

    $csvlogger->error("Error Message1","Error Message2");




DESCRIPTION

Top

Log to a file in CSV format.

METHODS

Top

new

It accepts csv file path as a arguement.

    $csvlogger = Log::CSVLogger->new("path/to/log.csv");

If the file does not exist it will create it for you.

debug

This method will print a debug message.

    $csvlogger->debug("Debug Message1","Debug Message2");

    timestamp,debug,Debug Message1,Debug Message2,...,Debug MessageN

info

This method will print an info message.

    $csvlogger->info("Info Message1","Info Message2");

warn

This method will print a warn message.

    $csvlogger->warn("Warn Message1","Warn Message2");

error

This method will print an error message.

    $csvlogger->error("Error Message1","Error Message2");

log

In case you want to access log function, this method will print a log message with timestamp and passed arguements.

    $csvlogger->log("Log Message1","Log Message2");

AUTHOR

Top

Gaurav Khambhala ( gaurav at deeproot dot co dot in )

CREDITS

Top

Terence Monteiro

LICENSE

Top

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License version 3 or any later version. See http://www.fsf.org/licensing/licenses/gpl.html


Log-CSVLogger documentation Contained in the Log-CSVLogger distribution.
package Log::CSVLogger;
use POSIX qw(strftime);
use Text::CSV_XS;
use Path::Class;
our $VERSION = 0.1;

sub new {
    my $class = shift;
    my $args = shift;
    
    my $csv = Text::CSV_XS->new;
    my $csvpath = file($args->{'csvfile'}); 
        
    my $self = {
        csv => $csv,
        csvpath => $csvpath,
    };

    bless($self,$class);
    return $self;
}


sub debug {
    my $self = shift;
    $self->log('debug', @_);
}


sub info {
    my $self = shift;
    $self->log('info', @_);
}

sub warn {
    my $self = shift;
    $self->log('warn', @_);
}

sub error {
    my $self = shift;
    $self->log('error', @_);
}

sub log{
    my $self = shift;
    if (-f $self->{csvpath} && !-w $self->{csvpath} ){
        print STDERR "File " . $self->{csvpath} . " exists but don't have write permission on it.\n";
        return 0;
    }
    if ( !-f $self->{csvpath} && !-w $self->{csvpath}->parent ){
        print STDERR "File " . $self->{csvpath} . "does not exist and also don't have write permission on it.\n";
        return 0;
    }
    unless (open FILE, ">>", $self->{csvpath}) { 
        print STDERR "Unable to open file " . $@;  
    } 
    my $timestamp = POSIX::strftime( "%a %b %e %H:%M:%S %Y", localtime);
    $self->{csv}->combine ($timestamp,@_);
    my $string = $self->{csv}->string;
    print FILE "$string\n";
    close FILE;
    return 1;
    
}

1;