Singleton logger

From: Laszlo (graf.laszlo_at_axis.hu)
Date: 04/20/04


Date: 20 Apr 2004 06:19:15 -0700

Hi

I need a GL::Logger wich implements Class::Singleton
to be a singleton and Log::Log4perl to can log amy
message into a file. Here is the module source:
-------------------------------------------------------------------------
package GL::Logger;

use Class::Singleton;
use vars qw( $ERROR );
@ISA = qw(Class::Singleton);
use Log::Log4perl;
my $ERROR = '';
my $logger;

sub instance(){
    my $class = shift;
    my $self = bless { }, $class;
    Log::Log4perl::init('c:/log4perl/log4perl.conf');
    unless (defined ($self->{ $logger } = Log::Log4perl->get_logger('...'))) {
        $ERROR = "Cannot get configuration file (log4perl.conf)\n";
        return undef;
    }
    $self;
}

sub log(){
    my($level, $msg) = @_;
    if($level eq 'DEBUG'){
        $logger->debug($msg);
    }
    if($level eq 'INFO'){
        $logger->info($msg);
    }
    if($level eq 'WARN'){
        $logger->warn($msg);
    }
    if($level eq 'ERROR'){
        $logger->error($msg);
    }
    if($level eq 'FATAL'){
        $logger->fatal($msg);
    }
    return 0;
}
1;
-------------------------------------------------------------------------

And this is the way how I want to use it:

-------------------------------------------------------------------------
#!perl -w
use GL::Logger;
my $logger = GL::Logger->instance();
$logger->log("ERROR", "blablabla");
-------------------------------------------------------------------------

It retunr the following error message:

Use of uninitialized value in hash element at c:/Perl/site/lib/GL/Logger.pm line 14.

What is wrong ?
--------------------------------------------
Laszlo Graf



Relevant Pages

  • Singleton logger
    ... to be a singleton and Log::Log4perl to can log amy ... package GL::Logger; ... use vars qw; ... sub instance(){ ...
    (perl.beginners)
  • Modules inter-relay ?
    ... package will be used through out the other project ... use LocalePrint; ... sub PhaseError { ... # using %vars ...
    (perl.beginners)
  • Re: How to create a variable dynamically?
    ... > use strict 'vars'; ... > sub use_x ... you need to tell Perl that $x is in package z ...
    (perl.beginners)
  • How to create a variable dynamically?
    ... and keep "use strict 'vars'" in the code below: ... sub use_x ...
    (perl.beginners)