Log::H...::File(3pm)

πŸ“¦
version 0.90 Β· 5 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Log::H...::File(3pm) πŸ’–

okay so bestie, this is the User Contributed Perl Documentation page for Log::Handler::Output::File, and it's literally just... logging to a file. that's the whole vibe. let's get into it 🫢


✨ NAME ✨

Log::Handler::Output::File β€” Log messages to a file.

like. that's it. that's the girl. she writes your messages to a file and she does it well πŸ’…


🌸 SYNOPSIS 🌸

use Log::Handler::Output::File;

my $log = Log::Handler::Output::File->new(
    filename    => "file.log",
    filelock    => 1,
    fileopen    => 1,
    reopen      => 1,
    mode        => "append",
    autoflush   => 1,
    permissions => "0664",
    utf8        => 0,
);

$log->log(message => $message);

πŸ¦‹ DESCRIPTION πŸ¦‹

Log messages to a file. no cap, that is genuinely the entire description. she's an icon of efficiency ✨


πŸ‘‘ METHODS πŸ‘‘

🌈 new()

Call new() to create a new Log::Handler::Output::File object. it's giving main character energy, it's giving creation.

The following options are possible:

  • filename β€” you can set a file name as a string or as an array reference. If you set an array reference then the parts will be concatenated with catfile from File::Spec.

Set a file name:

my $log = Log::Handler::Output::File->new( filename => "file.log"  );

Set a array reference:

my $log = Log::Handler::Output::File->new(

    # foo/bar/baz.log
    filename => [ "foo", "bar", "baz.log" ],

    # /foo/bar/baz.log
    filename => [ "", "foo", "bar", "baz.log" ],

);
  • filelock β€” 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. (yes bestie, that's the plot of every multiprocess log corruption horror story 😭) You can set the option filelock to 0 or 1.
0 - no file lock
1 - exclusive lock (LOCK_EX) and unlock (LOCK_UN) by each write operation (default)
  • fileopen β€” open a log file transient or permanent. two very different girls:
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)
  • reopen β€” this option works only if option fileopen is set to 1. important!! don't skip that part.
0 - deactivated
1 - try to reopen the log file if the inode changed (default)

πŸŽ€ How to use fileopen and reopen πŸŽ€

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. πŸ’€ (not me shading Windows, but also... yes me shading Windows)

To write your code independent you should control it:

my $os_is_win = $^O =~ /win/i ? 0 : 1;

my $log = Log::Handler::Output::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. girlie just isn't relevant anymore, we move on βœ‹

🌟 mode 🌟

There are three possible modes to open a log file.

append - O_WRONLY | O_APPEND | O_CREAT (default)
excl   - O_WRONLY | O_EXCL   | O_CREAT
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. very chill, very default, love her.

excl would fail by open the log file if the log file already exists. she said "no new friends" 🚫

trunc would truncate the complete log file if it exists. Please take care to use this option. like bestie I am BEGGING you, be careful, that's your whole log getting deleted, it's giving irreversible πŸ’€

Take a look to the documentation of sysopen() to get more information.

πŸ’œ autoflush πŸ’œ

0 - autoflush off
1 - autoflush on (default)

πŸ“ permissions πŸ“

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.

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.

fr fr, 0640 = owner can read + write, the group can only read, and everyone else gets absolutely nothing. the door is closed, the vibes are exclusive ✨

Take a look to the documentation of sysopen() to get more information.

🧚 utf8, utf-8 🧚

utf8   =  binmode, $fh, ":utf8";
utf-8  =  binmode, $fh, "encoding(utf-8)";

Yes, there is a difference. (the way that these two are NOT the same thing and everyone mixes them up 😭)

<http://perldoc.perl.org/perldiag.html#Malformed-UTF-8-character-(%25s)>

<http://perldoc.perl.org/Encode.html#UTF-8-vs.-utf8-vs.-UTF8>

🌸 dateext 🌸

It's possible to set a pattern in the filename that is replaced with a date. If the date β€” and the filename β€” changed the file is closed and reopened with the new filename. The filename is converted with POSIX::strftime.

Example:

my $log = Log::Handler::Output::File->new(
    filename  => "file-%Y-%m-%d.log",
    dateext => 1
);

In this example the file file-2015-06-12.log is created. At the next day the filename changed, the log file file-2015-06-12.log is closed and file-2015-06-13.log is opened.

This feature is a small improvement for systems where no logrotate is available like Windows systems. On this way you have the chance to delete old log files without to stop/start a daemon. slay for the Windows girlies who have no logrotate, this one's for you 🫢

πŸ’« log()

Call log() if you want to log messages to the log file.

Example:

$log->log(message => "this message goes to the logfile");

πŸ’– flush()

Call flush() if you want to re-open the log file.

This is useful if you don't want to use option reopen. As example if a rotate mechanism moves the logfile and you want to re-open a new one. it's giving "I'll handle it myself, thanks" πŸ’…

⭐ validate()

Validate a configuration.

🌟 reload()

Reload with a new configuration.

πŸ’œ errstr()

Call errstr() to get the last error message. she'll tell you what went wrong, no need to guess πŸ₯Ί

πŸŽ€ close()

Call close() to close the log file yourself β€” normally you don't need to use it, because the log file will be opened and closed automatically. it's the "I got it, you can chill" of methods ✨


🌈 PREREQUISITES 🌈

Carp
Fcntl
File::Spec
Params::Validate

✨ EXPORTS ✨

No exports. nothing. zero. empty. she's independent, she doesn't need to share 😌


πŸ› REPORT BUGS πŸ›

Please report all bugs to <jschulz.cpan(at)bloonix.de>.

If you send me a mail then add Log::Handler into the subject. (do it bestie, it's literally one word and it helps so much πŸ™)


πŸ‘‘ AUTHOR πŸ‘‘

Jonny Schulz <jschulz.cpan(at)bloonix.de>.


Copyright (C) 2007-2009 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.


okay that's the whole page bestie, you ate it, go log something 🫢

sign-off πŸ’«: this was from perl v5.36.0, dated 2022-08-28, and it's the Log::H...::File(3pm) page β€” mwah, iconic, periodt πŸ’…βœ¨
πŸ“¦ where this page comes from