Log::Handler::Logger::File - Log messages to a file.
use Log::Handler::Logger::File; my $log = Log::Handler::Logger::File->new( filename => 'file.log', filelock => 1, fileopen => 1, reopen => 1, mode => 'append', autoflush => 1, permissions => '0664', utf8 => 0, ); $log->write($message);
Log messages to a file.
Call new() to create a new Log::Handler::Logger::File object.
new()
The new() method expected the options for the log file. Each option will be set to a default value if not set. If this method is called with no options then it creates an object to log to STDOUT with all default values.
Call write() if you want to log messages to the log file.
write()
Example:
$log->write('this message goes to the logfile');
This function returns the last error message.
With filename you can set a file name, a GLOBREF or you can set a string as an alias for STDOUT or STDERR. The default is STDOUT for this option.
filename
Set a file name:
my $log = Log::Handler::Logger::File->new( filename => 'file.log' );
Set a GLOBREF
open FH, '>', 'file.log' or die $!; my $log = Log::Handler::Logger::File->new( filename => \*FH );
Or the same with
open my $fh, '>', 'file.log' or die $!; my $log = Log::Handler::Logger::File->new( filename => $fh );
Set STDOUT or STDERR
my $log = Log::Handler::Logger::File->new( filename => \*STDOUT ); # or my $log = Log::Handler::Logger::File->new( filename => \*STDERR );
If the option filename is set in a config file and you want to debug to your screen then you can set *STDOUT or *STDERR as a string.
*STDOUT
*STDERR
my $log = Log::Handler::Logger::File->new( filename => '*STDOUT' ); # or my $log = Log::Handler::Logger::File->new( filename => '*STDERR' );
That is not possible:
my $log = Log::Handler::Logger::File->new( filename => '*FH' );
Note that if you set a GLOBREF to filename some options will be forced (overwritten) and you have to control the handles yourself. The forced options are
fileopen => 1 filelock => 0 reopen => 0
Maybe it's desirable to lock the log file by each write operation because a lot of processes write at the same time to the log file. You can set the option filelock to 0 or 1.
filelock
0 - no file lock 1 - exclusive lock (LOCK_EX) and unlock (LOCK_UN) by each write operation (default)
Open a log file transient or permanent.
0 - open and close the logfile by each write operation 1 - open the logfile if C<new()> called and try to reopen the file if C<reopen> is set to 1 and the inode of the file has changed (default)
This option works only if option fileopen is set to 1.
fileopen
0 - deactivated 1 - try to reopen the log file if the inode changed (default)
Please note that it's better to set reopen and fileopen to 0 on Windows because Windows unfortunately haven't the faintest idea of inodes.
reopen
To write your code independent you should control it:
my $os_is_win = $^O =~ /win/i ? 0 : 1; my $log = Log::Handler::Logger::File->new( filename => 'file.log', mode => 'append', fileopen => $os_is_win );
If you set fileopen to 0 then it implies that reopen has no importance.
There are three possible modes to open a log file.
append - O_WRONLY | O_APPEND | O_CREAT excl - O_WRONLY | O_EXCL | O_CREAT (default) trunc - O_WRONLY | O_TRUNC | O_CREAT
append would open the log file in any case and appends the messages at the end of the log file.
append
excl would fail by open the log file if the log file already exists. This is the default option because some security reasons.
excl
trunc would truncate the complete log file if it exists. Please take care to use this option.
trunc
Take a look to the documentation of sysopen() to get more informations.
sysopen()
0 - autoflush off 1 - autoflush on (default)
The option permissions sets the permission of the file if it creates and must be set as a octal value. The permission need to be in octal and are modified by your process's current "umask".
permissions
That means that you have to use the unix style permissions such as chmod. 0640 is the default permission for this option. That means that the owner got read and write permissions and users in the same group got only read permissions. All other users got no access.
chmod
0640
If this option is set to 1 then UTF-8 will be set with binmode() on the output filehandle.
binmode()
Fcntl Params::Validate
No exports.
Please report all bugs to <jschulz.cpan(at)bloonix.de>.
Jonny Schulz <jschulz.cpan(at)bloonix.de>.
Do you have any questions or ideas?
MAIL: <jschulz.cpan(at)bloonix.de>
If you send me a mail then add Log::Handler into the subject.
Copyright (C) 2007 by Jonny Schulz. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
To install Log::Handler, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Log::Handler
CPAN shell
perl -MCPAN -e shell install Log::Handler
For more information on module installation, please visit the detailed CPAN module installation guide.