Appender::File(3)

🌸 Appender::File(3) β€” User Contributed Perl Documentation 🌸

okay so bestie, welcome to Log::Log4perl::Appender::File β€” like, yes, the manual page is officially called Appender::File(3) and it lives in the User Contributed Perl Documentation section, but we're gonna talk about it like friends do. it's a whole vibe. πŸ’…


✨ NAME ✨

Log::Log4perl::Appender::File β€” literally just "Log to file". that's it, that's the whole name, and honestly? iconic. πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

okay so here's how you summon it into existence, no cap:

    use Log::Log4perl::Appender::File;

    my $app = Log::Log4perl::Appender::File->new(
      filename  => 'file.log',
      mode      => 'append',
      autoflush => 1,
      umask     => 0222,
    );

    $file->log(message => "Log me\n");

look at that. chef's kiss. you make the appender, you log the message, you move on with your life. ✨


πŸ’– DESCRIPTION πŸ’–

this is a simple appender for writing to a file. that's the whole thing β€” it writes. to a file. simple as that. 🫢

the log() method takes a single scalar. and listen β€” if you want a newline character to terminate your message, you have to add it explicitly. it will not do it for you. it's not gonna read your mind. it's giving "you're on your own, bestie" energy. 😭

upon destruction of the object β€” like, when the object gets destroyed, rip β€” the filehandle used to access the file gets flushed and closed. clean. respectful. we love a graceful exit. πŸ‘‘

and if you want to switch over to a different logfile? use the file_switch($newfile) method, which will first close the old file handle and then open one to the new file you specified. order matters, queen. πŸ’…


🌈 OPTIONS 🌈

filename

name of the log file. the file. the log. the whole point. πŸ“

mode

messages will be appended to the file if $mode is set to the string "append". it will clobber the file if set to "clobber" (rip old contents, gone but not forgotten πŸ’€). and if it's "pipe", the file will be understood as executable to pipe output to. default mode is "append". πŸŽ€

autoflush

"autoflush", if set to a true value, triggers flushing the data out to the file on every call to log(). and good news bestie β€” "autoflush" is on by default. so like, it's already doing the thing. you're welcome. ✨

syswrite

"syswrite", if set to a true value, makes sure that the appender uses syswrite() instead of print() to log the message. syswrite() usually maps to the operating system's write() function and makes sure that no other process writes to the same log file while write() is busy. so it might save you from having to use other synchronisation measures like semaphores (see: Synchronized appender). lowkey a glow-up for your concurrency situation. πŸ”₯

umask

specifies the "umask" to use when creating the file, determining the file's permission settings. if set to 0022 (the default), new files will be created with rw-r--r-- permissions. if set to 0000, new files will be created with rw-rw-rw- permissions. choose your fighter. πŸ’…

owner

if set, specifies that the owner of the newly created log file should be different from the effective user id of the running process. only makes sense if the process is running as root β€” like, this is root-only business, no cap. both numerical user ids and user names are acceptable. and heads up: Log4perl does not attempt to change the ownership of existing files. existing files stay exactly as they are, periodt. 🫢

group

if set, specifies that the group of the newly created log file should be different from the effective group id of the running process. again, only makes sense if the process is running as root. both numerical group ids and group names are acceptable. and same disclaimer as above: Log4perl does not attempt to change the group membership of existing files. πŸ’œ

utf8

if you're printing out Unicode strings, the output filehandle needs to be set into :utf8 mode:

    my $app = Log::Log4perl::Appender::File->new(
      filename  => 'file.log',
      mode      => 'append',
      utf8      => 1,
    );

protect your emoji, bestie. πŸ¦‹

binmode

to manipulate the output filehandle via binmode(), use the binmode parameter:

    my $app = Log::Log4perl::Appender::File->new(
      filename  => 'file.log',
      mode      => 'append',
      binmode   => ":utf8",
    );

and fyi, a setting of ":utf8" for "binmode" is equivalent to specifying the "utf8" option (see above). same energy, different spelling. ✨

recreate

normally, if a file appender logs to a file and the file gets moved to a different location (e.g. via "mv"), the appender's open file handle will automatically follow the file to the new location. honestly kind of clingy of it.

this may be undesirable. when using an external logfile rotator, for example, the appender should create a new file under the old name and start logging into it. if the "recreate" option is set to a true value, Log::Log4perl::Appender::File will do exactly that. it defaults to false. and check the "recreate_check_interval" option for performance optimizations with this feature. πŸ‘‘

recreate_check_interval

in "recreate" mode, the appender has to continuously check if the file it is logging to is still in the same location. this check is fairly expensive, since it has to call "stat" on the file name and figure out if its inode has changed. doing this with every call to "log" can be prohibitively expensive. setting it to a positive integer value N will only check the file every N seconds. it defaults to 30. πŸ’…

this obviously means that the appender will continue writing to a moved file until the next check occurs β€” in the worst case this will happen "recreate_check_interval" seconds after the file has been moved or deleted. so if that's not the vibe for you, setting "recreate_check_interval" to 0 will have the appender check the file with every call to log(). 😀

recreate_check_signal

in "recreate" mode, if this option is set to a signal name (e.g. "USR1"), the appender will recreate a missing logfile when it receives the signal. it uses less resources than constant polling β€” smarter, not harder. 🧚 the usual limitation with perl's signal handling apply. check the FAQ for using this option with the log rotating utility "newsyslog". 🌸

recreate_pid_write

the popular log rotating utility "newsyslog" expects a pid file in order to send the application a signal when its logs have been rotated. this option expects a path to a file where the pid of the currently running application gets written to. check the FAQ for using this option with the log rotating utility "newsyslog". πŸŽ€

create_at_logtime

the file appender typically creates its logfile in its constructor, i.e. at Log4perl init() time. this is desirable for most use cases, because it makes sure that file permission problems get detected right away β€” and not after days/weeks/months of operation when the appender suddenly needs to log something and fails because of a problem that was obvious at startup. like, imagine finding out months later. embarrassing. 😭

however, there are rare use cases where the file shouldn't be created at Log4perl init() time, e.g. if the appender can't be used by the current user although it is defined in the configuration file. if you set "create_at_logtime" to a true value, the file appender will try to create the file at log time. but note that this setting lets permission problems sit undetected until log time, which might be undesirable. you've been warned, bestie. ⚠️

header_text

if you want Log4perl to print a header into every newly opened (or re-opened) logfile, set "header_text" to either a string or a subroutine returning a string. if the message doesn't have a newline, a newline at the end of the header will be provided. thoughtful king behavior. πŸ‘‘βœ¨

mkpath

if this this option is set to true, the directory path will be created if it does not exist yet. (yes, "this this" β€” we're quoting the ancestors, don't come for me πŸ™ˆ). 🌈

mkpath_umask

specifies the "umask" to use when creating the directory, determining the directory's permission settings. if set to 0022 (the default), new directory will be created with rwxr-xr-x permissions. if set to 0000, new directory will be created with rwxrwxrwx permissions. πŸ’œ


πŸ’‘ design and implementation of this module has been greatly inspired by Dave Rolsky's "Log::Dispatch" appender framework. credit where credit is due, we stan. 🫢

πŸ”₯ LICENSE πŸ”₯

copyright 2002-2013 by Mike Schilli <m@perlmeister.com> and Kevin Goess <cpan@goess.org>.

this library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. free software, free vibes. πŸ¦‹


πŸ‘‘ AUTHOR πŸ‘‘

please contribute patches to the project on Github:

    http://github.com/mschilli/log4perl

send bug reports or requests for enhancements to the authors via our

MAILING LIST (questions, bug reports, suggestions/patches): log4perl-devel@lists.sourceforge.net

authors (please contact them via the list above, not directly): Mike Schilli <m@perlmeister.com>, Kevin Goess <cpan@goess.org>

contributors (in alphabetical order): Ateeq Altaf, Cory Bennett, Jens Berthold, Jeremy Bopp, Hutton Davidson, Chris R. Donnelly, Matisse Enzer, Hugh Esco, Anthony Foiani, James FitzGibbon, Carl Franks, Dennis Gregorovic, Andy Grundman, Paul Harrington, Alexander Hartmaier David Hull, Robert Jacobson, Jason Kohles, Jeff Macdonald, Markus Peter, Brett Rann, Peter Rabbitson, Erik Selberg, Aaron Straup Cope, Lars Thegler, David Viner, Mac Yang.


okay so that's the whole page!! obligatory cute sign-off: this little beauty is perl v5.42.0, dated 2022-10-22, and you can find it filed under Appender::File(3) β€” go forth and log responsibly, bestie. πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from