Message Logger Package.

 

Source.

#################################################
#	Message Logger v1.1 - by fckorea.
#   http://open-source.tistory.com
#	Variables are changed naming. [2014-01-20]
#################################################
#
#   Logger is file or printing message package.
#
#   Log file name format
#	%HOSTNAME%: Hostname of this system.
#	%DATE%: Current date.
#	%TIME%: Current time.
#
#   Message format
#	%DATE%: Current date.
#		%YEAR%: Current Year.
#		%MONTH%: Current Month.
#		%DAY%: Current Day.
#	%TIME%: Current time.
#		%HOUR%: Current Hour.
#		%MIN%: Current Minute.
#		%SEC%: Current Second.
#	%TYPE%: Message Type. [Info, Error, Etc...]
#	%MESSAGE%: Message contents.
#
#   Logging Output Type[integer]
#	1: Output message to console only.
#	10: Output message to file only.
#	11: Output message to console, file.
#
#################################################
package Logger;
use Time::localtime;	# for localtime
use Sys::Hostname;	# for hostname

###============================== Constructor ==============================###
sub new	### (self[, Logging file name{string}[, Logging console msg format{string}[, Logging file msg format{string}[, Log date format{string}[, Log time format{string}]]]]]) return self;
{
	#
	### Example: my $clsLogger = Logger->new();
	### Example: my $clsLogger = Logger->new("Result_Logger_%HOSTNAME%_%DATE%.log");
	### Example: my $clsLogger = Logger->new("Result_Logger_%HOSTNAME%_%DATE%.log", "%TIME%\t[%TYPE%] %MESSAGE%");
	### Example: my $clsLogger = Logger->new("Result_Logger_%HOSTNAME%_%DATE%.log", "%TIME%\t[%TYPE%] %MESSAGE%", "%DATE% %TIME%\t[%TYPE%] %MESSAGE%");
	### Example: my $clsLogger = Logger->new("Result_Logger_%HOSTNAME%_%DATE%.log", "%TIME%\t[%TYPE%] %MESSAGE%", "%DATE% %TIME%\t[%TYPE%] %MESSAGE%", "%YEAR%/%MONTH%/%DAY%", "%HOUR%:%MIN%:%SEC%");
	### http://open-source.tistory.com
	### by fckorea.
	### Code written by fckorea. [2013-02-03]
	#
	my ($l_clsSelf, $l_sFileName, $l_sConsoleMsgFormat, $l_sFileMsgFormat, $l_sLogDateFormat, $l_sLogTimeFormat) = @_;
	
	if($l_sFileName eq '')
	{
		$l_sFileName = (sprintf "Result_%s_%04d-%02d-%02d.log", hostname, localtime->year() + 1900, localtime->mon() + 1, localtime->mday());
	}
	elsif(index($l_sFileName, '%') > -1)
	{
		$sDATE = (sprintf "%04d-%02d-%02d", localtime->year() + 1900, localtime->mon() + 1, localtime->mday());
		$sTIME = (sprintf "%02d%02d%02d", localtime->hour(), localtime->min(), localtime->sec());
		$sHOSTNAME = hostname;
		
		$l_sFileName =~ s/%DATE%/$sDATE/g;
		$l_sFileName =~ s/%TIME%/$sTIME/g;
		$l_sFileName =~ s/%HOSTNAME%/$sHOSTNAME/g;
	}
	
	$l_sConsoleMsgFormat = "\t[%TYPE%] %MESSAGE%" if($l_sConsoleMsgFormat eq undef);	
	$l_sFileMsgFormat = "%DATE%\t%TIME%: [%TYPE%] %MESSAGE%" if($l_sFileMsgFormat eq undef);
	
	$l_sLogDateFormat = "%YEAR%-%MONTH%-%DAY%" if($l_sLogDateFormat eq undef);
	$l_sLogTimeFormat = "%HOUR%:%MIN%:%SEC%" if($l_sLogTimeFormat eq undef);
	
	my $self = {
		g_hFileLogger => undef,
		g_sLoggingFileName => $l_sFileName,
		g_sLoggingConsoleMsgFormat => $l_sConsoleMsgFormat,
		g_sLoggingFileMsgFormat => $l_sFileMsgFormat, 
		g_sLogDateFormat => $l_sLogDateFormat,
		g_sLogTimeFormat => $l_sLogTimeFormat
	};
	
	bless $self, $l_clsSelf;
	
	return $self;
}

###============================== Package info functions ==============================###
sub fnGetLogFileName	### (self) return g_sLoggingFileName;
{
	#
	### http://open-source.tistory.com
	### by fckorea.
	### Code written by fckorea. [2013-02-03]
	#

	my $l_hSelf = $_[0];
	return $l_hSelf->{g_sLoggingFileName};
}

sub fnGetConsoleMsgFormat	### (self) return g_sLoggingConsoleMsgFormat;
{
	#
	### http://open-source.tistory.com
	### by fckorea.
	### Code written by fckorea. [2013-02-03]
	#

	my $l_hSelf = $_[0];
	return $l_hSelf->{g_sLoggingConsoleMsgFormat};
}

sub fnGetFileMsgFormat	### (self) return g_sLoggingFileMsgFormat;
{
	#
	### http://open-source.tistory.com
	### by fckorea.
	### Code written by fckorea. [2013-02-03]
	#
	
	my $l_hSelf = $_[0];
	return $l_hSelf->{g_sLoggingFileMsgFormat};
}

sub fnGetLogDateFormat	### (self) return g_sLogDateFormat;
{
	#
	### http://open-source.tistory.com
	### by fckorea.
	### Code written by fckorea. [2013-02-03]
	#
	
	my $l_hSelf = $_[0];
	return $l_hSelf->{g_sLogDateFormat};
}

sub fnGetLogTimeFormat	### (self) return g_sLogTimeFormat;
{
	#
	### http://open-source.tistory.com
	### by fckorea.
	### Code written by fckorea. [2013-02-03]
	#
	
	my $l_hSelf = $_[0];
	return $l_hSelf->{g_sLogTimeFormat};
}

sub fnLogFileReset	### (self) return void;
{
	#
	### http://open-source.tistory.com
	### by fckorea.
	### Code written by fckorea. [2013-02-03]
	#
	
	my $l_hSelf = $_[0];
	open($g_hFileLogger, ">$l_hSelf->{g_sLoggingFileName}");
	close($g_hFileLogger) if ($g_hFileLogger);
}
	

###============================== Logger functions ==============================###
sub fnLogging	### (self, Logging Message[string], Message Type{string}, Output Type{integer}[, Temporary Message Format{string}[, Temporary Console Message Format{string}]]) return void;
{
	#
	### Example: $clsLogger->fnLogging("####### My Logger Start!!! #######", "", 11, "%DATE% %TIME%\t%MESSAGE%", "%TIME%\t%MESSAGE%");
	### Example: $clsLogger->fnLogging("No Printing ;)\n", "Error", 0);
	### Example: $clsLogger->fnLogging("Test Message in Console only!!!\n", "Info", 1);
	### Example: $clsLogger->fnLogging("Test Message in File only!!!\n", "Info", 10);
	### Example: $clsLogger->fnLogging("Test Message in Console & File!!!\n", ";)", 11);
	### http://open-source.tistory.com
	### by fckorea.
	### Code written by fckorea. [2013-02-03]
	#
	
	my ($l_hSelf, $l_sLoggingMsg, $l_sLoggingMsgType, $l_iLoggingMsgOutputType, $l_sTempMessageFormat, $l_sTempConsoleMessageFormat) = @_;
	
	my $l_sYear = (sprintf "%04d", localtime->year() + 1900);
	my $l_sMonth = (sprintf "%02d", localtime->mon() + 1);
	my $l_sDay = (sprintf "%02d", localtime->mday());
	
	my $l_sDate = $l_hSelf->{g_sLogDateFormat};
	$l_sDate =~ s/%YEAR%/$l_sYear/g;
	$l_sDate =~ s/%MONTH%/$l_sMonth/g;
	$l_sDate =~ s/%DAY%/$l_sDay/g;
	
	my $l_sHour = (sprintf "%02d", localtime->hour());
	my $l_sMin = (sprintf "%02d", localtime->min());
	my $l_sSec = (sprintf "%02d", localtime->sec());
	
	my $l_sTime = $l_hSelf->{g_sLogTimeFormat};
	$l_sTime =~ s/%HOUR%/$l_sHour/g;
	$l_sTime =~ s/%MIN%/$l_sMin/g;
	$l_sTime =~ s/%SEC%/$l_sSec/g;

	if($l_iLoggingMsgOutputType % 10)
	{
		# for Console
		my $l_sConsoleMsg = $l_hSelf->{g_sLoggingConsoleMsgFormat};
		$l_sConsoleMsg = $l_sTempMessageFormat if($l_sTempMessageFormat ne undef);
		$l_sConsoleMsg = $l_sTempConsoleMessageFormat if($l_sTempConsoleMessageFormat ne undef);
		
		# Log data mapping
		$l_sConsoleMsg =~ s/%DATE%/$l_sDate/g;
		$l_sConsoleMsg =~ s/%TIME%/$l_sTime/g;
		$l_sConsoleMsg =~ s/%TYPE%//g if($l_sLoggingMsgType eq undef);
		$l_sConsoleMsg =~ s/%TYPE%/$l_sLoggingMsgType/g;
		$l_sConsoleMsg =~ s/%MESSAGE%/$l_sLoggingMsg/g;
		
		$l_hSelf->fnPrint($l_sConsoleMsg, 1);
	}
	
	if(int($l_iLoggingMsgOutputType / 10))
	{
		# for File
		my $l_sFileMsg = $l_hSelf->{g_sLoggingFileMsgFormat};
		$l_sFileMsg = $l_sTempMessageFormat if($l_sTempMessageFormat ne undef);
		
		# Log data mapping
		$l_sFileMsg =~ s/%DATE%/$l_sDate/g;
		$l_sFileMsg =~ s/%TIME%/$l_sTime/g;
		$l_sFileMsg =~ s/%TYPE%//g if($l_sLoggingMsgType eq undef);
		$l_sFileMsg =~ s/%TYPE%/$l_sLoggingMsgType/g;
		$l_sFileMsg =~ s/%MESSAGE%/$l_sLoggingMsg/g;
		
		$l_hSelf->fnPrint($l_sFileMsg, 10);
	}
}

sub fnPrint	### (self, Logging Message{string}, Output Type{integer}) return void;
{
	#
	### http://open-source.tistory.com
	### by fckorea.
	### Code written by fckorea. [2013-02-03]
	#
	
	my ($l_hSelf, $l_sPrintMsg, $l_iPrintMsgOutputType) = @_;
	my $l_hFileLogger;
	
	if($l_iPrintMsgOutputType % 10)
	{
		print $l_sPrintMsg;
		print "\n" if not $l_sPrintMsg =~ m/.*(\r\n|\r|\n)$/;
	}
	
	if(int($l_iPrintMsgOutputType / 10))
	{
		open($l_hFileLogger, ">>$l_hSelf->{g_sLoggingFileName}");
		
		print $l_hFileLogger $l_sPrintMsg;
		print $l_hFileLogger "\n" if not $l_sPrintMsg =~ m/.*(\r\n|\r|\n)$/;
		
		close($l_hFileLogger) if ($l_hFileLogger);
	}
}

1;	### End Of Package;

 

Usage.

use Logger;

my $clsLogger = Logger->new("Result_Logger_%HOSTNAME%_%DATE%.log", "%TIME%\t[%TYPE%] %MESSAGE%", "%DATE% %TIME%\t[%TYPE%] %MESSAGE%", "%YEAR%/%MONTH%/%DAY%", "%HOUR%:%MIN%:%SEC%");

$clsLogger->fnLogging("####### My Logger Start!!! #######", "", 11, "%DATE% %TIME%\t%MESSAGE%", "%TIME%\t%MESSAGE%");
$clsLogger->fnLogging("No Printing ;)\n", "Error", 0);
$clsLogger->fnLogging("Test Message in Console only!!!\n", "Info", 1);
$clsLogger->fnLogging("Test Message in File only!!!\n", "Info", 10);
$clsLogger->fnLogging("Test Message in Console & File!!!\n", ";)", 11);
$clsLogger->fnLogging("####### My Logger Terminated... #######", "", 11, "%DATE% %TIME%\t%MESSAGE%", "%TIME%\t%MESSAGE%");

 

Result.

2013/03/26 19:03:07	####### My Logger Start!!! #######
2013/03/26 19:03:07	[Info] Test Message in File only!!!
2013/03/26 19:03:07	[;)] Test Message in Console & File!!!
2013/03/26 19:03:07	####### My Logger Terminated... #######

'Perl' 카테고리의 다른 글

[ Perl::Function ] fnOptionsParser  (0) 2013.04.18
[ Perl ] Data Types  (0) 2013.02.17
[ Perl::Function ] fnGetDirFileList  (0) 2013.02.08

+ Recent posts