http://qs321.pair.com?node_id=509758

matt.tovey has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to come up with a neat way to log messages from my script. I'm logging messages to any of several destinations - stdout, a file, or a text widget, and currently I do this by calling multiple functions (one of which is included below), which isn't very clean. I'm also prioritising ($LOG_LEVEL) each message, and the other functions also support a $PrintFormal boolean, to switch on/off the timestamp output in some cases.
use constant DEBUG => 0; # Log levels use constant INFO => 1; use constant NORMAL => 2; use constant WARN => 3; use constant ERROR => 4; my @LOG_LEVEL_NAME = ('DEBUG', 'INFO', 'NORMAL', 'WARN', 'ERROR'); my $LOG_LEVEL = DEBUG; sub LogMsg { my $level = shift; unless ($level >= $LOG_LEVEL) {return} my ($sec, $min, $hour, $day, $mon, $year) = localtime; $LogCounter++; if (open LOG, ">> $LOGFILE") { printf LOG "%04d.%02d.%02d %02d:%02d:%02d.%d: ", $year+1900, $mon+1, $day, $hour, $min, $sec, $LogCounter; print LOG "$$: $Display: $LOG_LEVEL_NAME[$level]: @_\n"; close LOG; } } LogMsg INFO, "Starting chooser mode...";

What I'd like to do is combine my three functions into one, and seeing as how the function gets used quite a lot, I want it's usage to be as straight-forward as possible. I'm figuring this would probably work with a bit of binary arithmetic:

use constant FILE => 0b0001; use constant STDOUT => 0b0010; use constant GUI => 0b0100; LogMsg FILE|STDOUT, "Message";

But before I go ahead and implement this, I wanted to ask if anyone has any tips, or can point me to some existing code? Is there already a standard way of doing this? Ideally, I'd also like to be able to select a priority and formatting for each of the destinations!