From: Peter Rabbitson Date: Fri, 17 Jun 2011 01:49:37 +0000 (+0200) Subject: Open the logfile in append mode X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=64b3598fcf53fcec068a67277d80363540f19427;p=dbsrgits%2FDBIx-Class-Historic.git Open the logfile in append mode --- diff --git a/Changes b/Changes index 2bf54e5..53622ed 100644 --- a/Changes +++ b/Changes @@ -6,6 +6,8 @@ Revision history for DBIx::Class conditions on resultset chaining - The Ordered component is now smarter wrt reordering of dirty objects, and does its job with less storage queries + - Logging via DBIC_TRACE=1= no longer overwrites the + logfile on every program startup, appending loglines instead * Fixes - Fix issue where the query was becoming overly mangled when trying diff --git a/lib/DBIx/Class/Storage/Statistics.pm b/lib/DBIx/Class/Storage/Statistics.pm index 714c8fb..eb536cd 100644 --- a/lib/DBIx/Class/Storage/Statistics.pm +++ b/lib/DBIx/Class/Storage/Statistics.pm @@ -61,7 +61,7 @@ sub debugfh { my $debug_env = $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG} || $ENV{DBIC_TRACE}; if (defined($debug_env) && ($debug_env =~ /=(.+)$/)) { - $fh = IO::File->new($1, 'w') + $fh = IO::File->new($1, 'a') or die("Cannot open trace file $1"); } else { $fh = IO::File->new('>&STDERR') diff --git a/t/storage/debug.t b/t/storage/debug.t index 37003ba..5e1958e 100644 --- a/t/storage/debug.t +++ b/t/storage/debug.t @@ -1,5 +1,6 @@ use strict; use warnings; +no warnings 'once'; use Test::More; use Test::Exception; @@ -11,38 +12,45 @@ use Path::Class qw/file/; my $schema = DBICTest->init_schema(); +my $lfn = file('t/var/sql.log'); +unlink $lfn or die $! + if -e $lfn; + # make sure we are testing the vanilla debugger and not ::PrettyPrint $schema->storage->debugobj(DBIx::Class::Storage::Statistics->new); ok ( $schema->storage->debug(1), 'debug' ); -$schema->storage->debugfh(file('t/var/sql.log')->openw); - +$schema->storage->debugfh($lfn->openw); $schema->storage->debugfh->autoflush(1); -my $rs = $schema->resultset('CD')->search({}); -$rs->count(); +$schema->resultset('CD')->count; -my $log = file('t/var/sql.log')->openr; -my $line = <$log>; -$log->close(); -like($line, qr/^SELECT COUNT/, 'Log success'); +my @loglines = $lfn->slurp; +is (@loglines, 1, 'one line of log'); +like($loglines[0], qr/^SELECT COUNT/, 'File log via debugfh success'); $schema->storage->debugfh(undef); -$ENV{'DBIC_TRACE'} = '=t/var/foo.log'; -$rs = $schema->resultset('CD')->search({}); -$rs->count(); -$log = file('t/var/foo.log')->openr; -$line = <$log>; -$log->close(); -like($line, qr/^SELECT COUNT/, 'Log success'); -$schema->storage->debugobj->debugfh(undef); -delete($ENV{'DBIC_TRACE'}); + +{ + local $ENV{DBIC_TRACE} = "=$lfn"; + unlink $lfn; + + $schema->resultset('CD')->count; + + my $schema2 = DBICTest->init_schema(no_deploy => 1); + $schema2->storage->_do_query('SELECT 1'); # _do_query() logs via standard mechanisms + + my @loglines = $lfn->slurp; + is(@loglines, 2, '2 lines of log'); + like($loglines[0], qr/^SELECT COUNT/, 'Env log from schema1 success'); + like($loglines[1], qr/^SELECT 1:/, 'Env log from schema2 success'); + + $schema->storage->debugobj->debugfh(undef) +} open(STDERRCOPY, '>&STDERR'); -stat(STDERRCOPY); # nop to get warnings quiet close(STDERR); dies_ok { - $rs = $schema->resultset('CD')->search({}); - $rs->count(); + $schema->resultset('CD')->search({})->count; } 'Died on closed FH'; open(STDERR, '>&STDERRCOPY');