http://qs1969.pair.com?node_id=11150244

MoodyDreams999 has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to create a log file, to log phone numbers in the file that aren't 10 digits, been searching the documentation on adding an array variable into the log file, but had no such luck , I need some guidance.
# Read in all rows from existing file my $rows = read_excel($in_file); my $headers = [ "source_id", "first_name", "middle", "last_name", "address1", "city", "state", "postal_code", "phone_number", "address3", "province", "email", ]; my $CA_zip = 19005; # Update the rows for my $row (@$rows) { if ($row->[6] eq 'CA' && ! $row->[7]) { $row->[7] = $CA_zip; } elsif ($row->[8] >10 || $row->[8] < 10) { my $a = ($rows); } } # add headers as first row unshift(@$rows, $headers); # Write the updated rows to new file my $col_num = scalar @$headers - 1; write_excel($out_file, $rows, $col_num); sub read_excel { my ( $file, $sheet ) = @_; $sheet ||= 0; my $parser = Spreadsheet::ParseXLSX->new(); my $workbook = $parser->parse($file); if ( not defined $workbook ) { die $parser->error; } my $worksheet = $workbook->worksheet($sheet); my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); my @rows; for my $row ( $row_min .. $row_max ) { my @cells; for my $col ( $col_min .. $col_max ) { my $cell = $worksheet->get_cell( $row, $col ); if (not $cell) { push(@cells,''); next; } my $value = $cell->value(); push(@cells,$value); } push(@rows,\@cells); } return \@rows; } sub write_excel { my ( $file, $rows, $col_max ) = @_; my $workbook = Excel::Writer::XLSX->new( $file ); if ( not defined $workbook ) { die "Could not open file: $!"; } my $worksheet = $workbook->add_worksheet(); my $worksheet2 = $workbook->add_worksheet(); my $row_num = 0; for my $row ( @$rows ) { for my $col (0 .. $col_max) { $worksheet->write( $row_num, $col, $row->[$col] ); } $row_num++; use Log::Log4perl qw(get_logger); #Defining configuration my $config = q( log4perl.logger = ERROR, FileApp log4perl.appender.FileApp = Log::Log4perl::Append +er::File log4perl.appender.FileApp.filename = test.log log4perl.appender.FileApp.layout = PatternLayout log4perl.appender.FileApp.layout.ConversionPattern = %d> % +m%n $a ); #logging behavior Log::Log4perl->init( \$config ); my $logger = Log::Log4perl::get_logger(""); $logger->error("Oh my, a dreadful error!"); $logger->warn("Oh my, a dreadful warning!"); } $workbook->close(); return; }

Replies are listed 'Best First'.
Re: Log file
by 1nickt (Canon) on Feb 08, 2023 at 21:53 UTC

    If you are adding a new logging mechanism such as Log4perl to your program, I would highly recommend experimenting in a small program that only does logging. 95% of the code you've posted here has nothing to do with your issue. (See SSCCE.)

    One thing that stands out is that you are instantiating your logger each time through the loop. You want it instantiated once and available in the loop. I also don't see any attempt to log an array -- or anything for that matter other than the test statements at error and warn.

    Finally, you might consider whether a big logging framework is the right tool for your task or whether simply opening and appending to a file would be enough.


    The way forward always starts with a minimal test.
      Thank you, Sorry about SSCCE, shouldve just asked the question I'm sure that would of sufficed.