Last active
January 2, 2020 18:09
-
-
Save bert/94fb6ce57842f6d317fdf284b0e85773 to your computer and use it in GitHub Desktop.
Perl_FileLogger_example_module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package FileLogger; | |
| # FileLogger.pm | |
| # Retrieved from: https://www.perltutorial.org/perl-module/ | |
| # Example for using modules in perl. | |
| use strict; | |
| use warnings; | |
| my $LEVEL = 1; # default log level | |
| sub open | |
| { | |
| my $logfile = shift; | |
| # open log file for appending | |
| open(LFH, '>>', $logfile) or die "cannot open the log file $logfile: $!"; | |
| # write logged time: | |
| print LFH "Time: ", scalar(localtime), "\n"; | |
| } | |
| sub log | |
| { | |
| my($log_level,$log_msg) = @_; | |
| # append log if the passed log level is lower than | |
| # the module log level | |
| if ($log_level <= $LEVEL) | |
| { | |
| print LFH "$log_msg\n"; | |
| } | |
| } | |
| sub close | |
| { | |
| close LFH; | |
| } | |
| sub set_level | |
| { | |
| # set the log level | |
| my $log_level = shift; | |
| # check if the passed log level is a number | |
| if ($log_level =~ /^\d+$/) | |
| { | |
| $LEVEL = $log_level; | |
| } | |
| } | |
| 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/perl | |
| use strict; | |
| use warnings; | |
| use FileLogger; | |
| FileLogger::open ("logtest.log"); | |
| FileLogger::log (1,"This is a test message"); | |
| FileLogger::close (); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment