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!
In reply to Logging messages - is there a better way? by matt.tovey
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |