Carp::Notify - Loudly complain in lots of places when things break badly |
Carp::Notify - Loudly complain in lots of places when things break badly
version 1.13
Use it in place of die or croak, or warn or carp.
# with Carp; use Carp; if ($something_a_little_bad) { carp("Oh no, a minor error!")}; if ($something_bad) { croak ("Oh no an error!")};
# with Carp::Notify; use Carp::Notify; if (something_a_little_bad) {notify("Oh no, a minor error!")}; if ($something_bad) { explode ("Oh no an error!")};
Carp::Notify is an error reporting module designed for applications that are running unsupervised (a CGI script, for example, or a cron job). If a program has an explosion, it terminates (same as die or croak or exit, depending on preference) and then emails someone with useful information about what caused the problem. Said information can also be logged to a file. If you want the program to tell you something about an error that's non fatal (disk size approaching full, but not quite there, for example), then you can have it notify you of the error but not terminate the program.
Defaults are set up within the module, but they can be overridden once the module is used, or as individual explosions take place. Please set up your appropriate defaults in the module. It'll save you headaches later.
This version is nearly identical to, and is bug-for-bug compatible with, version 1.10 which has been on CPAN for years but has not been indexed.
This public release is intended to catch the attention of anyone actually using this module and let them know that changes are coming!
Perl 5.005, Socket (for emailing)
will require and import the module, same as always. What you decide to import it with is up to you. You can choose to import additional functions into your namespace, set up new default values, or give it a list of variables to store.
Carp::Notify will always export the explode function and the notify function. Carp always exports carp, croak, and confess, so I figure that I can get away with always exporting explode and notify. Nyaah.
Be sure that you set your default variables before using it!
use Carp::Notify ( "log_it" => 1, "email_it => 0, "email" => 'thomasoniii@yahoo.com' );
These are hash keys that allow you to override the Carp::Notify's module defaults. These can also all be overloaded explicitly
in your explode()
calls, but this allows a global modification.
If you'd like, you can give log_file a reference to a glob that is an open filehandle instead of a scalar containing the log name. This is most useful if you want to redirect your error log to STDERR or to a pipe to a program.
Be sure to use globrefs only explicitly in your call to explode, or to wrap the definition of the filehandle in a begin block before using the module. Otherwise, you'll be trying to log to a non-existent file handle and consequently won't log anything. That'd be bad.
log_file stores all notifications AND explosions
If you'd like, you can give explode_log a reference to a glob that is an open filehandle instead of a scalar containing the log name. This is most useful if you want to redirect your explosion log to STDERR or to a pipe to a program.
Be sure to use globrefs only explicitly in your call to explode, or to wrap the definition of the filehandle in a begin block before using the module. Otherwise, you'll be trying to log to a non-existent file handle and consequently won't log anything. That'd be bad.
explode_log stores ONLY explosions
If you'd like, you can give notify_log a reference to a glob that is an open filehandle instead of a scalar containing the log name. This is most useful if you want to redirect your notification log to STDERR or to a pipe to a program.
Be sure to use globrefs only explicitly in your call to notify, or to wrap the definition of the filehandle in a begin block before using the module. Otherwise, you'll be trying to log to a non-existent file handle and consequently won't log anything. That'd be bad.
notify_log stores ONLY notifications
email_it
is true.
email_it
is true.
email_it
is true.
death_function's use will cause the program to terminate with a simple 'exit'. You'll have to die within death_function yourself if you want to pass on that way.
death_function may be a coderef or a string containing a function name ('death_function', 'main::death', '&death', etc.)
error_function may be a coderef or a string containing a function name ('error_function', 'main::error', '&error', etc.) =back
use Carp::Notify ('$scalar', '@array');
$scalar = "some_value"; @array = qw(val1 val2 val3);
explode("An error!");
will write out the values ``$scalar : some_value'' and ``@array : val1 val2 val3'' to the log file.
This can only be used to store global variables. Dynamic or lexical variables need to be explicitly placed in explode()
calls.
You can store variables from other packages if you'd like:
use Carp::Notify ('$other_package::scalar', '@some::nested::package::array');
Only global scalars, arrays, and hashes may be stored in 1.00. 1.10 allows you to store a function call as well, the function will be called with the same arguements that death_message would receive. Its return value will be stored along with other stored variables.
make_storable('$new_scalar', '@different_array');
make_unstorable('$scalar', '@different_array');
You can override your default values here (see Using the module above), if you'd like, and otherwise specify as many error messages as you'd like to show up in your logs.
# override who the mail's going to, and the log file. explode("email" => "thomasoniii@yahoo.com", log_file => "/home/jim/jim_explosions.log", "A terrible error: $!");
# Same thing, but with a globref to the same file open (LOG, ">>/home/jim/jim_explosions.log"); explode("email" => "thomasoniii@yahoo.com", log_file => \*LOG, "A terrible error: $!");
# don't log. explode ("log_it" => 0, "A terrible error: $!");
# keep the defaults explode("A terrible error: $!", "And DBI said: $DBI::errstr");
So what's the point of this thing?
It's for programs that need to keep running and that need to be fixed quickly when they break.
But I like Carp
I like Carp too. :)
This isn't designed to replace Carp, it serves a different purpose. Carp will only tell you the line on which your error occurred. While this i helpful, it doesn't get your program running quicker and it doesn't help you to find an error that you're not aware of in a CGI script that you think is running perfectly.
Carp::Notify tells you ASAP when your program breaks, so you can inspect and correct it quicker. You're going to have less downtime and the end users will be happier with your program because there will be fewer bugs since you ironed them out quicker.
Wow. That was a real run-on sentence
Yeah, I know. That's why I'm a programmer and not an author. :)
What about CGI::Carp?
That's a bit of a gray area. Obviously, by its name, CGI::Carp seems designed for CGI scripts, whereas Carp::Notify is more obvious for anything (cron jobs, command line utilities, as well as CGIs).
Carp::Notify also can store more information with less interaction from the programmer. Plus it will email you, if you'd like to let you know that something bad happened.
As I understand it, CGI::Carp is a subset feature-wise of Carp::Notify. If CGI::Carp is working fine for you -- great, continue to use it. If you want more flexible error notification, then try out Carp::Notify.
But I can send email with CGI::Carp by opening up a pipe to send mail and using that as my error log. What do you have to say about that?
Good for you. I can too. But most people that I've interacted with either don't have the know-how to do that or just plain wouldn't have thought of it. Besides, it's still more of a hassle than just using Carp::Notify.
Why are your stored variables kept in an array instead of a hash? Hashes are quicker to delete from, after all
While it is definitely true that variables can be unstored a little quicker in a hash, I figured that stored variables will only rarely be unstored later. Arrays are quicker for storing and accessing the items later, since they're only accessed en masse. I'll live with the slight performance hit for the rarer case.
Can I store variables that are in another package from the one that called Carp::Notify?
You betcha. Just prepend the classpath to the variable name, same as you always have to to access variables not in your name space. If the variable is already in your name space (you imported it), you don't need the classpath since explode will just pick it up within your own namespace.
Can I store local or my variables?
Not in the use statement, but you can in an explicit explode.
Are there any bugs I should be aware of?
If you import explode into your package, then subclass it and export explode back out it won't correctly pick up your stored variables unless you fully qualified them with the class path ($package::variable instead of just $variable)
Solution? Don't re-export Carp::Notify. But you already knew that you should just re-use it in your subclass, right?
Always exports explode and notify no matter what is specified in the use statement. This is according to the original design but contrary to current conventions and is now considered to be a bug.
make_storable and make_unstorable don't scope the variables they store/unstore by the calling package, which means that they don't affect the results of store_vars.
store_vars destroys the contents of the stored variables, making multiple notifications from the same package not work well.
store_vars doesn't gracefully handle being called directly from the main script (i.e. it expects there to be at least one other function in the call stack).
The internal function today doesn't always report GMT offsets for some timezones that don't use whole hours (e.g. Indian/Cocos aka CCT, which is sometimes reported as +07-30 instead of +0630).
The internal function today has an edge condition where the GMT offset could report one additional or one less minute of offset (rare).
Could I see some more examples?
Sure, that's the next section.
Okay, you've convinced me. What other nifty modules have you distributed?
Mail::Bulkmail and Text::Flowchart.
Was that a shameless plug?
Why yes, yes it was.
# store $data, do email the errors, and alias croak => explode use Carp::Notify ('$data', 'email_it' => 1, "croak");
# email it to a different address, and don't log it. use Carp::Notify ("email" => 'thomasoniii@yahoo.com', 'log_it' => 0);
# don't use global log file, log explosions and notifys separately. use Carp::Notify ('log_it' => 0, 'log_explode' => 1, 'log_notify => 1);
# die with an explosion. explode("Ye gods! An error!");
# die with an explosion and a different email subject. explode ('subject' => "HOLY SHIT THIS IS BAD!", "The reactor core breached.");
# explode, but do it quietly. explode ("die_quietly" => 1, "Ye gods! An error!");
# notify someone of a problem, but keep the program running notify ("Ye gods! A little error!");
v1.12 - Mar 31, 2016 -
* Fix tests relying on hash ordering
* fix double adding of 1900 to year in calculations
* minor cleanups for Test::Perl::Critic and Test::Pod::Coverage
* eliminate conditional 'my' statements
v1.11 - Feb 1, 2013 -
* Updated documentation and maintainership.
v1.10 - April 13, 2001 -
* Long overdue re-write/enhancements.
* Cleaned up the internals.
* Added support for different log files for notifys/explodes.
* Added support for changing all the defaults externally.
* Improved today()
function with Mail::Bulkmail's date generation.
* Added death_function to call upon dying instead of death_message.
* Added error_function to help handle unforeseen problems.
* Perl 5.6's 'caller' function returns an extra two fields of information. Jeff Boes pointed this out and extra-helpfully provided a patch for it as well.
* Added the ability to config the SMTP port. I was just being lazy in v1.00. :*)
v1.00 - August 10, 2000 - Changed the name from Explode to Carp::Notify. It's more descriptive and I don't create a new namespace.
v1.00 FC1 - June 9, 2000 - First publically available version.
This software is copyright (c) 2016 by Brian Conry, and 2000, 2001 by Jim Thomason.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
You can find documentation for this module with the perldoc command.
perldoc Carp::Notify
The following websites have more information about this module, and may be of help to you. As always, in addition to those websites please use your favorite search engine to discover more resources.
A modern, open-source CPAN search engine, useful to view POD in HTML format.
http://metacpan.org/release/Carp-Notify
Search CPANThe default CPAN search engine, useful to view POD in HTML format.
http://search.cpan.org/dist/Carp-Notify
RT: CPAN's Bug TrackerThe RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
https://rt.cpan.org/Public/Dist/Display.html?Name=Carp-Notify
AnnoCPANThe AnnoCPAN is a website that allows community annotations of Perl module documentation.
http://annocpan.org/dist/Carp-Notify
CPAN RatingsThe CPAN Ratings is a website that allows community ratings and reviews of Perl modules.
http://cpanratings.perl.org/d/Carp-Notify
CPAN ForumThe CPAN Forum is a web forum for discussing Perl modules.
http://cpanforum.com/dist/Carp-Notify
CPANTSThe CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
http://cpants.cpanauthors.org/dist/Carp-Notify
CPAN TestersThe CPAN Testers is a network of smokers who run automated tests on uploaded CPAN distributions.
http://www.cpantesters.org/distro/C/Carp-Notify
CPAN Testers MatrixThe CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
http://matrix.cpantesters.org/
CPAN Testers DependenciesThe CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
Please report any bugs or feature requests by email to bug-carp-notify at rt.cpan.org
, or through
the web interface at https://rt.cpan.org/Public/Bug/Report.html?Queue=Carp-Notify. You will be automatically notified of any
progress on the request by the system.
The code is open to the world, and available for you to hack on. Please feel free to browse it and play with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull from your repository :)
https://github.com/buc0/carp-notify
git clone git://github.com/buc0/carp-notify.git
Carp::Notify - Loudly complain in lots of places when things break badly |