소스 검색

Unify log locations w/ tpsgi

George Baugh 5 달 전
부모
커밋
e210ae84ce
8개의 변경된 파일과 24개의 추가작업 그리고 25개의 파일을 삭제
  1. 0 1
      bin/tcms-useradd
  2. 7 1
      lib/TCMS.pm
  3. 0 2
      lib/Trog/Config.pm
  4. 12 14
      lib/Trog/Log.pm
  5. 2 5
      lib/Trog/Log/Metrics.pm
  6. 1 0
      lib/Trog/SQLite.pm
  7. 0 2
      lib/Trog/Themes.pm
  8. 2 0
      www/templates/html/components/metrics.tx

+ 0 - 1
bin/tcms-useradd

@@ -13,7 +13,6 @@ use List::Util qw{first};
 use Trog::Auth;
 use Trog::Data;
 use Trog::Config;
-use Trog::Log;
 
 # Don't murder our terminal when done
 $ENV{NOHUP} = 1;

+ 7 - 1
lib/TCMS.pm

@@ -6,6 +6,7 @@ use warnings;
 no warnings 'experimental';
 use feature qw{signatures state};
 
+use Carp qw{confess};
 use Clone        qw{clone};
 use Date::Format qw{strftime};
 
@@ -67,6 +68,11 @@ sub build_routes {
         my $cb_wrap = sub {
             my ( $tpsgi, $query ) = @_;
 
+            confess "Cannot determine directory for logs!" unless $tpsgi->{log_dir};
+
+            # Make sure the tCMS logs live in the same dir as the tpsgi logs.
+            log_init("$tpsgi->{log_dir}/tpsgi.log", $tpsgi->{verbose} ? 'debug' : 'info');
+
             # Let's open up our default route if needed before we bother thinking any harder
             return $routes{default}{callback}->($query) unless -f "config/setup";
 
@@ -74,7 +80,7 @@ sub build_routes {
             %$Trog::Log::DBI::urchin = map { $_ => delete $query->{$_} } qw{utm_source utm_medium utm_campaign utm_term utm_content};
 
             # Set the IP of the request so we can fail2ban
-            $Trog::Log::ip = $query->{req_address};
+            $Trog::Log::ip = $query->{ip};
 
             # Set the referer & ua to go into DB logs, but not logs in general.
             # The referer/ua largely has no importance beyond being a proto bug report for log messages.

+ 0 - 2
lib/Trog/Config.pm

@@ -7,7 +7,6 @@ use feature qw{state};
 use FindBin::libs;
 
 use Config::Simple;
-use Trog::Log ();
 
 =head1 Trog::Config
 
@@ -29,7 +28,6 @@ sub get {
     foreach my $cfg2try ($home_cfg, $default) {
         next unless -f "config/$cfg2try";
         $cf = Config::Simple->new("config/$cfg2try");
-        Trog::Log::INFO("Loaded config file: $cfg2try");
         last;
     }
     die "Could not find config file!" unless $cf;

+ 12 - 14
lib/Trog/Log.pm

@@ -8,6 +8,7 @@ use Log::Dispatch;
 use Log::Dispatch::DBI;
 use Log::Dispatch::Screen;
 use Log::Dispatch::FileRotate;
+use File::Basename qw{dirname};
 
 use Trog::SQLite;
 use Trog::Log::DBI;
@@ -16,16 +17,21 @@ use Exporter 'import';
 our @EXPORT_OK   = qw{log_init is_debug INFO DEBUG WARN FATAL};
 our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK );
 
-my $LOGNAME = 'logs/tcms.log';
-$LOGNAME = $ENV{CUSTOM_LOG} if $ENV{CUSTOM_LOG};
-
-my $LEVEL = $ENV{WWW_VERBOSE} ? 'debug' : 'info';
-
+my ($LEVEL, $LOGDIR);
 our ( $log, $user );
+
 $Trog::Log::user = 'nobody';
 $Trog::Log::ip   = '0.0.0.0';
 
 sub log_init {
+    my ($LOGNAME, $LEVEL)  =@_;
+
+    die "Cannot initialize logs without log name and log level" unless $LOGNAME && $LEVEL;
+
+    $LOGNAME //= 'logs/tcms.log';
+    $LEVEL   //= 'info';
+
+    $LOGDIR = dirname($LOGNAME);
 
     # By default only log requests & warnings.
     # Otherwise emit debug messages.
@@ -64,7 +70,7 @@ sub log_init {
 my $rq;
 
 sub _dbh {
-    return Trog::SQLite::dbh( 'schema/log.schema', "logs/log.db" );
+    return Trog::SQLite::dbh( 'schema/log.schema', "$LOGDIR/log.db" );
 }
 
 sub is_debug {
@@ -90,27 +96,19 @@ sub _log {
 }
 
 sub DEBUG {
-    _check_init();
     $log->debug( _log( shift, 'DEBUG' ) );
 }
 
 sub INFO {
-    _check_init();
     $log->info( _log( shift, 'INFO' ) );
 }
 
 sub WARN {
-    _check_init();
     $log->warning( _log( shift, 'WARN' ) );
 }
 
 sub FATAL {
-    _check_init();
     $log->log_and_die( level => 'error', message => _log( shift, 'FATAL' ) );
 }
 
-sub _check_init {
-    log_init() unless $log;
-}
-
 1;

+ 2 - 5
lib/Trog/Log/Metrics.pm

@@ -7,6 +7,7 @@ no warnings 'experimental';
 use feature qw{signatures state};
 
 use Trog::SQLite;
+use Trog::Log;
 
 =head1 Trog::Log::Metrics
 
@@ -15,10 +16,6 @@ and for reasoning about the various things that it's Urchin-compatible data can
 
 =cut
 
-sub _dbh {
-    return Trog::SQLite::dbh( 'schema/log.schema', "logs/log.db" );
-}
-
 =head2 requests_per(ENUM period{second,minute,hour,day,month,year}, INTEGER num_periods, [TIME_T before], INTEGER[] @codes)
 
 Returns a data structure of the following form
@@ -64,7 +61,7 @@ sub requests_per ( $period, $num_periods, $before, @codes ) {
 
     my $query = "SELECT count(*) FROM all_requests $whereclause GROUP BY date / ? HAVING date < ? LIMIT ?";
 
-    my @results = map { $_->[0] } @{ _dbh()->selectall_arrayref( $query, undef, @input ) };
+    my @results = map { $_->[0] } @{ Trog::Log::_dbh()->selectall_arrayref( $query, undef, @input ) };
     my $np      = @results < $num_periods ? @results : $num_periods;
     my @labels  = reverse map { "$_ $period(s) ago" } ( 1 .. $np );
 

+ 1 - 0
lib/Trog/SQLite.pm

@@ -46,6 +46,7 @@ sub dbh {
     my ( $schema, $dbname ) = @_;
     $dbh //= {};
     return $dbh->{$dbname} if $dbh->{$dbname};
+
     File::Touch::touch($dbname) unless -f $dbname;
     my $db = DBI->connect( "dbi:SQLite:dbname=$dbname", "", "" );
 

+ 0 - 2
lib/Trog/Themes.pm

@@ -10,7 +10,6 @@ use FindBin::libs;
 
 use Trog::Vars;
 use Trog::Config;
-use Trog::Log;
 
 =head1 Trog::Themes
 
@@ -41,7 +40,6 @@ sub get_dir {
         my $themedir = "www/themes/$theme";
         $tdir = $themedir if -d $themedir;
     }
-    Trog::Log::INFO("Loading Theme '$theme' from '$tdir'");
     return $tdir;
 };
 

+ 2 - 0
www/templates/html/components/metrics.tx

@@ -47,6 +47,8 @@
             });
         };
     </script>
+    <div>Keep in mind that this only measures direct hits to the application, which shouldn't happen aside from API calls or logged in users.</div>
+    <br /><br />
     <form>
         Period:
         <select name=period class="cooltext" >