Chapter 2
module could be changed to automatically open the log file as soon as possible
during compile time. To cause code to be run at compile time, move the code from
open_log() into a BEGIN block:
BEGIN {
open(LOG_FILE, ">>logs/boa.log") or die "Unable to open log : $!";
print LOG_FILE "BOA log started: " . localtime(time) . "\n";
}
Now the log file is opened as soon as the BOA::Logger module is compiled. The
downside here is that the location of the log file is hard coded into BOA::Logger.
END
It is often useful to know when an application exited. BOA::Logger can provide this
by registering an action to take place when the application exits. This is done with
an END block the opposite of the BEGIN block described earlier.
END {
print LOG_FILE "BOA log exited: " . localtime(time) . "\n";
close LOG_FILE or die "Unable to close log/boa.log : $!";
}
As an added bonus I get to feel like a good citizen by closing the LOG_FILE file han
dle instead of letting global destruction do it for me. Global destruction refers to
the phase in a Perl program's life when the interpreter is shutting down and will
automatically free all resources held by the program. END blocks are often used to
clean up resources obtained during BEGIN blocks.
Error Reporting
BOA::Logger is a careful module it always checks to make sure system calls like
open() and close() succeed. When they don't, BOA::Logger calls die(), which will
cause the program to exit if not caught by an eval.
10
This is all well and good, but
unfortunately the error messages generated aren't very helpful they make it look
as though there's a problem in BOA::Logger. For example, if you call open_log() on
a file that can't be opened, you'll receive the following error message:
Unable to open /path/to/log : No such file or directory at BOA/Logger.pm line 8.
10. This is Perl's poor man exception handling. For a more evolved system, see the Exception
module on CPAN.
34
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved