Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

(simple|short|script) logging

by hiseldl (Priest)
on Dec 04, 2006 at 19:53 UTC ( #587726=snippet: print w/replies, xml ) Need Help??
Description:

Have you ever written a script and needed a quick way to log-to-a-file. I've done this over and over, in the last several years that I've been writing perl. It's a simple and short way to log things to a file. For example:

# start the logfile slog "$logfile"; slog "=======================================START="; slog "= ".(scalar localtime); # do some stuff noslog; # send to another logfile slog "$another_logfile"; slog "=======================================START="; slog "= ".(scalar localtime); # do some stuff noslog;

It has limitations, such as, only one log file can be written at a time. It uses IO::File, but that shouldn't be too much of a limitation, anymore. It also puts $_slogfh in the main:: namespace. Since I mostly use this for quick testing-type scripts, I don't run into any naming collisions.

My typical usage is that I remove newlines from each sub to get them both on their own lines, then, i just add them to any scripts where I need a quick log-to-a-file solution.

sub slog(@) { 
    $::_slogfh||=new IO::File $_[0],">>";
    print $::_slogfh @_,$/ 
};
sub noslog() { undef $::_slogfh };
Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://587726]
help
Chatterbox?
[choroba]: but still, small-talk about smuggling guns and how Trump is great with US expats isn't my cup of tea
[LanX]: brutal beer?
[LanX]: met a Canadian yesterday, he suggested building a wall and let the US pay for it
[choroba]: No, but lots of pictures of the Agent Orange victims, plus some deformed foeti
[LanX]: ugh

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2017-02-26 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (371 votes). Check out past polls.