Solstice::Logger::File - Dispatches a log message to a file.


Solstice documentation Contained in the Solstice distribution.

Index


Code Index:

NAME

Top

Solstice::Logger::File - Dispatches a log message to a file.

SYNOPSIS

Top

DESCRIPTION

Top

Export

No symbols exported.

Methods

writeLog($message)

Private Methods

_getFilePath($message)
_writeToFile($file, $message)

Modules Used

Solstice::Logger.

AUTHOR

Top

Catalyst Group, <catalyst@u.washington.edu>

VERSION

Top

Version $Revision: 3177 $

COPYRIGHT

Top


Solstice documentation Contained in the Solstice distribution.
package Solstice::Logger::File;

# $Id: File.pm 2393 2005-07-18 17:12:40Z jlaney $

use strict;
use warnings;
use 5.006_000;

use base qw(Solstice::Logger);

use Solstice::Model::LogMessage;

use Fcntl qw(:DEFAULT :flock);

use constant TRUE  => 1;
use constant FALSE => 0;

our ($VERSION) = ('$Revision: 1 $' =~ /^\$Revision:\s*([\d.]*)/);

sub writeLog {
    my $self = shift;
    my $message = shift;

    return FALSE unless (defined $message && defined $message->getContent());

    my $content = '';

    if (my $username = $message->getUsername()) {
        $content .= $username.' ';
    }
    if (my $acting_username = $message->getActingUsername()) {
        $content .= $acting_username.' ';
    }
    if (my $datetime = $message->getDateTime()) {
        # We only log a date if a DateTime obj is defined, but we're 
        # not using the DateTime object itself, due to a bug in 
        # Solstice::DateTime (no day_of_week data available)
        $content .= '['.(scalar localtime).'] ';
    }
    if (my $model = $message->getModel()) {
        $content .= $message->getModel().' ';
    }
    if (my $model_id = $message->getModelID()) {
        $content .= '('.$model_id.') ';
    }
    $content .= $message->getContent();
  
    return $self->_writeToFile($self->_getFilePath($message), $content);
}

sub _getFilePath {
    my $self = shift;
    my $message = shift;

    return unless ($message->getNamespace() && $message->getLogName());

    my $file_path = $self->getConfigService()->getDataRoot().'/';
    $self->_dirCheck($file_path);
    $file_path .= lc($message->getNamespace()).'/';
    $self->_dirCheck($file_path);
    $file_path .= $message->getLogName();

    return $file_path;
}

sub _writeToFile {
    my $self = shift;
    my $file_path = shift;
    my $message = shift;

    return FALSE unless defined $file_path;
    
    $message =~ s/\n/\\n/g;
    $message .= "\n";

    open(my $LOG, '>>', $file_path)
        or die "Cannot open '$file_path': $!\n";
    flock($LOG, LOCK_EX)
        or die "Cannot lock '$file_path' for writing: $!\n";
    seek($LOG, 0, 2)
        or die "Cannot seek to end of '$file_path': $!\n";
    print $LOG $message
        or die "Cannot write to '$file_path': $!\n";
    close($LOG);

    return TRUE;
}


1;
__END__