Module to handle substitution in error messages?

Discussion in 'Perl Misc' started by Henry Law, Nov 19, 2005.

  1. Henry Law

    Henry Law Guest

    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?

    --

    Henry Law <>< Manchester, England
     
    Henry Law, Nov 19, 2005
    #1
    1. Advertising

  2. Henry Law

    John Bokma Guest

    Henry Law <> wrote:

    > 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.


    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
     
    John Bokma, Nov 19, 2005
    #2
    1. Advertising

  3. Henry Law

    Guest

    Henry Law wrote:
    > 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'}},
    );
     
    , Nov 20, 2005
    #3
  4. Henry Law

    Anno Siegel Guest

    John Bokma <> wrote in comp.lang.perl.misc:
    > Henry Law <> wrote:
    >
    > > 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";
    > }


    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
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Nov 20, 2005
    #4
  5. Henry Law wrote:

    > 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
     
    Brian McCauley, Nov 20, 2005
    #5
  6. Henry Law

    Henry Law Guest

    wrote:
    > Henry Law wrote:
    >
    >>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.

    <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.

    --

    Henry Law <>< Manchester, England
     
    Henry Law, Nov 20, 2005
    #6
  7. Henry Law

    John Bokma Guest

    -berlin.de (Anno Siegel) wrote:

    > John Bokma <> wrote in comp.lang.perl.misc:


    >> 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.


    Ouch, you're right. Moreover:
    <http://johnbokma.com/mexit/2005/06/14/>

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
     
    John Bokma, Nov 20, 2005
    #7
  8. Henry Law

    John Bokma Guest

    Henry Law <> wrote:

    > 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.

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
     
    John Bokma, Nov 20, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rh0dium
    Replies:
    2
    Views:
    356
    rh0dium
    Aug 4, 2005
  2. Replies:
    2
    Views:
    320
    Larry Bates
    Sep 12, 2005
  3. Replies:
    0
    Views:
    379
  4. Louis Erickson

    Messages from FreezeThaw of a DBI handle

    Louis Erickson, Aug 20, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    87
    Louis Erickson
    Aug 20, 2003
  5. Jai
    Replies:
    8
    Views:
    139
    Denis McMahon
    Dec 18, 2013
Loading...

Share This Page