Module to handle substitution in error messages?

H

Henry Law

I've googled and CPAN'd for the answer to this one but all my queries
return either zero or a zillion irrelevant entries. Maybe someone here
will know the place to look, or alternatively confirm my suspicion that
what I'm looking for doesn't exist.

In previous programming projects, using languages other than Perl, I
created a set of routines which managed the issuing of error messages.
In summary my program would issue a call (speaking Perl now)

write_error_message(203,"foo","zurb failed",$zurb_rc);

In some file stored separately the error messages would be defined, with
placeholders for the bits that would vary from one invocation to
another; perhaps (I used XML but that's not essential)

<msg id="203" severity="5">
<text>Module %s encountered a '%s' error, rc=%s</text>
<explanation>Some text to guide interpretation</explanation>
</msg>

Then on executing the call an error message would appear thus:

203-5 Module foo encountered a 'zurb failed' error, rc= ... etc

Is there a Message::parameterise module, or something?
 
J

John Bokma

Henry Law said:
I've googled and CPAN'd for the answer to this one but all my queries
return either zero or a zillion irrelevant entries. Maybe someone here
will know the place to look, or alternatively confirm my suspicion that
what I'm looking for doesn't exist.

In previous programming projects, using languages other than Perl, I
created a set of routines which managed the issuing of error messages.
In summary my program would issue a call (speaking Perl now)

write_error_message(203,"foo","zurb failed",$zurb_rc);

In some file stored separately the error messages would be defined, with
placeholders for the bits that would vary from one invocation to
another; perhaps (I used XML but that's not essential)

<msg id="203" severity="5">
<text>Module %s encountered a '%s' error, rc=%s</text>
<explanation>Some text to guide interpretation</explanation>
</msg>

Then on executing the call an error message would appear thus:

203-5 Module foo encountered a 'zurb failed' error, rc= ... etc

Is there a Message::parameterise module, or something?


Yes, it's called sprintf / printf :-D.

sub write_error_message {

my ( $id, @params ) = @_;

my ( $text, $explanation ) = look_up( $id );

printf STDERR "$text\n", @params;
printf STDERR "$explanation\n";
}


of course you could wrap in nicely up in a nice module :-D.
 
U

usenet

Henry said:
In previous programming projects, using languages other than Perl, I
created a set of routines which managed the issuing of error messages...

In Perl (as you suspected but were unable to find) all these routines
have been written for you. I recommend Log::Dispatch (which has
extensions for XML, databases, Apache error logs, etc). This procedure
will be familar to Java programmers.

In any "serious" (non-trivial) program that I write, I use
Log::Dispatch for ALL message output (including output to the terminal
- I don't use "print"). It can send you e-mails, or do all sorts of
other things based on the severity of the log message.

Here is my "skeleton" code that I use to set up several dispatchers
(but not an XML dispatcher) and log the first status message. As you
see, I create one logging object and then add several dispatcher
methods to it. These will "fire" anytime I log a message of the
appropriate severity level. For example, debug messages go to the TTY
and a logfile, but only log messages at "error" or higher would also
kick off the e-mail dispatcher.

## Create some log dispatchers

use Mail::Sendmail;
use Log::Dispatch;
use Log::Dispatch::Screen;
use Log::Dispatch::File;
use Log::Dispatch::Email::MailSendmail;

my $log; #or maybe 'our log' for some conveniences
do{ #use 'do' to lexically isolate variables
my $logdir = $cfg{'dir'}{'log'} || "/var/tmp";

my @admin_email = @{$cfg{'email'}{'admin'}} ||
sprintf('%s@%s', scalar getpwuid($<), hostname);

unshift @{$Mail::Sendmail::mailcfg{'smtp'}},
$cfg{'email'}{'smtp'} || 'localhost';

my $add_lf = sub { my %p = @_; "$p{'message'}\n"};

my $add_timestamp = sub { my %p = @_;
sprintf "%s - %s", scalar(localtime),
$p{'message'}; };

$log = Log::Dispatch->new ( callbacks => $add_lf );

$log ->add( Log::Dispatch::Screen ->new(
name => 'screen',
min_level => 'debug',
stderr => 0, )
);

$log ->add( Log::Dispatch::File ->new(
name => 'file',
min_level => 'debug',
filename => sprintf ( "%s/%s.log",
$logdir,
$FindBin::Script ),
mode => 'append',
callbacks => $add_timestamp ),
);

$log ->add( Log::Dispatch::Email::MailSendmail ->new(
name => 'email',
min_level => 'error',
to => \@admin_email,
subject => "ERROR in $PROGRAM_NAME",
from => sprintf ("SERVER<%s\@%s>",
(hostname =~ /^([^\.]*)/)[0],
'do_not_reply.com' ) ),
);
}; #do

#dispatch our very first message - print all the runtime info
$log -> debug(sprintf
"[%s] Begin %s\n\tVersion %s on %s as %s\n"
."\tConfigFile: %s\n\tKillfile(s):\n%s",
__LINE__, __FILE__,
$main::VERSION,
hostname(),
"$REAL_USER_ID ($ENV{'USER'})",
$cfg{'inifile'},
map {"\t\t$_\n"} keys %{$cfg{'killfile'}},
);
 
A

Anno Siegel

John Bokma said:
Yes, it's called sprintf / printf :-D.

sub write_error_message {

my ( $id, @params ) = @_;

my ( $text, $explanation ) = look_up( $id );

printf STDERR "$text\n", @params;
printf STDERR "$explanation\n";
}

Tiny correction: The last "printf" should rather be "print". While the
$text part appears to be a printf format, $explanation looks like it
might use "%" innocently.

Anno
 
B

Brian McCauley

Henry said:
In previous programming projects, using languages other than Perl, I
created a set of routines which managed the issuing of error messages.
In summary my program would issue a call (speaking Perl now)

write_error_message(203,"foo","zurb failed",$zurb_rc);

In some file stored separately the error messages would be defined, with
placeholders for the bits that would vary from one invocation to
another; perhaps (I used XML but that's not essential)
Is there a Message::parameterise module, or something?

Consider Data::phrasebook
 
H

Henry Law

In Perl (as you suspected but were unable to find) all these routines
have been written for you. I recommend Log::Dispatch (which has
extensions for XML, databases, Apache error logs, etc). This procedure
will be familar to Java programmers.
<followed by lots of good code which I've snipped>

My thanks to the other posters but I think this reply of David's is what
I was looking for. Yes I could use printf, John, but there's that
little matter of the "look_up( $id )" function that you slipped in :)
Data::phrasebook is attractive, Brian, but I see that Log::Dispatch will
also replace the logging code that I've written, which is an added bonus.

Thanks all ... usenet is still alive and well.
 
J

John Bokma

Henry Law said:
My thanks to the other posters but I think this reply of David's is what
I was looking for. Yes I could use printf, John, but there's that
little matter of the "look_up( $id )" function that you slipped in :)

Yes, I couldn't think about a module supporting XML like in your example
:) (Not that I know all CPAN modules, but I check CPAN often)
Thanks all ... usenet is still alive and well.

Yup, even though there are every year rumours about its upcoming death.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top