{"id":2152,"date":"2012-06-06T18:25:40","date_gmt":"2012-06-06T18:25:40","guid":{"rendered":"http:\/\/craftydba.com\/?p=2152"},"modified":"2024-02-17T14:29:00","modified_gmt":"2024-02-17T14:29:00","slug":"application-logging-2","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=2152","title":{"rendered":"Application Logging"},"content":{"rendered":"<p>Error handling and application logging are the most import tasks associated with programming that are over looked by many developers.<\/p>\n<p>After a program is released to the field, how do you know if it is performing as it should? Are there issues with the language execution (1-tier), with the database (2-tier) or with the middle-ware (3-tier) ? Many times, issues are only noticed and acted upon after several complaints have been issued.<\/p>\n<p>Here is a short list of why you should adding logging to all programs that you create.<\/p>\n<ul>\n<li>Traps program execution for tracing down coding causing the error.<\/li>\n<li>Traps error events for follow up by developers before users complain.<\/li>\n<li>If captured in windows event log, 3rd party tools can harvest logs to one central server.<\/li>\n<li>Dashboards created at the central server can show the heart beat of your whole infrastructure.<\/li>\n<\/ul>\n<p>The original application logging <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/342311f1(v=vs.84).aspx\">CLASS<\/a> that I wrote in Visual Basic writes to both a local file and the windows event log. There is no equivalent PERL <a href=\"http:\/\/www.tutorialspoint.com\/perl\/perl_modules.htm\">PACKAGE<\/a> that does just that. Therefore, I am going to build the PERL package to over come this deficiency.<\/p>\n<p>The following example creates a sample package named MyLog to write a string to a file.<\/p>\n<pre class=\"lang:PERL theme:familiar mark:1,2-3\" title=\"perl code - custom logging pkg\">\r\n#\r\n# Define package name \/ export variables\r\n#\r\n\r\n# My package name\r\npackage MyLog;   \r\n\r\n# Export name space\r\nrequire Exporter;\r\nour @ISA = qw(Exporter);\r\n\r\n#\r\n# Define local variables.\r\n#\r\n\r\n# Data element 1 \r\nmy $FileName = '';\r\n\r\n# Data element 2\r\nmy $Message = '';\r\n\r\n#\r\n# Define properties\r\n#\r\n\r\n#  Set file name\r\nsub SetFileName\r\n{\r\n    my ($var) = @_; \r\n    $MyLog::FileName = $var;\r\n    return;\r\n}\r\n\r\n#  Set message\r\nsub SetMessage\r\n{\r\n    my ($var) = @_; \r\n    $MyLog::Message = $var;\r\n    return;\r\n}\r\n\r\n#\r\n# Define methods\r\n#\r\n\r\n#  Write to log file\r\nsub WriteToTxtFile\r\n{\r\n\r\n    # Open the text file\r\n    open(OUT, \">>$MyLog::FileName\");\r\n\r\n    # Create the string        \r\n    my @aryData = localtime(time);\r\n\r\n    my $Line = '';\r\n    $Line .= (1900 + $aryData[5]) . '-';\r\n    $Line .= (1 + $aryData[4]) . '-';\r\n    $Line .= $aryData[3] . ' ';\r\n\r\n    $Line .= $aryData[2] . ':';\r\n    $Line .= $aryData[1] . ':';\r\n    $Line .= $aryData[0] . ' - ';\r\n    $Line .= $MyLog::Message;\r\n\r\n    # Write string\t\r\n    print OUT $Line, \"\\n\";    \r\n\r\n    # Close the file\r\n    close(OUT);\r\n\r\n    # Just exit\r\n    return;\r\n};\r\n<\/pre>\n<\/p>\n<p>We need to tell PERL where our custom pakage is located by using the <span style=\"color: #008000; font-size: small;\">use lib<\/span> statement.<\/p>\n<pre class=\"lang:PERL theme:familiar mark:1,2-3\" title=\"perl code - include pkg\">\r\n# Set library path\r\nuse lib 'c:\\perl-depot\\logs';\r\n\r\n# Use perl module\r\nuse MyLog;\r\n<\/pre>\n<\/p>\n<p>The new module is already in the name space of the PERL script. We just need to set the properties of the file name and message before calling the <span style=\"color: #008000; font-size: small;\">Write()<\/span> method.<\/p>\n<pre class=\"lang:PERL theme:familiar mark:1,2-3\" title=\"perl code - sample call 2 pkg\">\r\n# Set properties & Write to logs\r\nMyLog::SetFileName('c:\\perl-depot\\logs\\MyApp.log');\r\nMyLog::SetMessage('This is a real error!');\r\nMyLog::Write();\r\n<\/pre>\n<\/p>\n<p>The table below has complete application logging code that you can use in your next PERL Script. I used the CPAN module (<a href=\"http:\/\/search.cpan.org\/~jdb\/Win32-OLE-0.1709\/lib\/Win32\/OLE.pm\">Win32::OLE<\/a>) to leverage the Windows Scripting module. This technique opens up all the windows objects that you call in Visual Basic Script to PERL.<\/p>\n<table border=\"1\" cellspacing=\"1\" cellpadding=\"1\" width=\"600\" align=\"left\">\n<tbody>\n<tr>\n<td style=\"border: thin solid gray;\">\n<a href='https:\/\/craftydba.com\/wp-content\/uploads\/2012\/06\/AppLog.pm_.txt'>AppLog.pm<\/a>\n<\/td>\n<td style=\"border: thin solid gray;\">App Log Package<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\">\n<a href='https:\/\/craftydba.com\/wp-content\/uploads\/2012\/06\/tst-app-logging.pl_.txt'>tst-app-logging.pl<\/a>\n<\/td>\n<td style=\"border: thin solid gray;\">Sample App Log Program<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\">\n<a href='https:\/\/craftydba.com\/wp-content\/uploads\/2012\/06\/MyApp.log_.txt'>MyApp.log<\/a>\n<\/td>\n<td style=\"border: thin solid gray;\">Sample log file<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\">\n<a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2012\/06\/perl-warning-event-viewer.jpg\">perl warning<\/a>\n<\/td>\n<td style=\"border: thin solid gray;\">Windows event entry<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\nIn summary, creating user defined packages allows you to create objects that can be easily reused. In fact, an advance technique would be the compiling of the PERL package for distribute to others. This advance task is out of scope for this talk. Adding application logging to your scripts will save you time in the long run when there are application, database, middle-ware or network issues plaguing your company.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Error handling and application logging are the most import tasks associated with programming that are over looked by many developers. After a program is released to the field, how do you know if it is performing as it should? Are there issues with the language execution (1-tier), with the database (2-tier) or with the middle-ware (3-tier) ? Many times, issues are only noticed and acted upon after several complaints have been issued. Here is a short list of why you should adding logging to all programs that you create. Traps&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[14,441,15,443,446,445,447],"class_list":["post-2152","post","type-post","status-publish","format-standard","hentry","category-other","tag-application-logging","tag-cpan-perl-modules","tag-john-f-miner-iii","tag-perl-script-2","tag-text-file","tag-win32ole","tag-windows-event-viewer"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/2152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2152"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/2152\/revisions"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}