Log.pm 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package Trog::Log;
  2. use strict;
  3. use warnings;
  4. use POSIX qw{strftime};
  5. use Log::Dispatch;
  6. use Log::Dispatch::DBI;
  7. use Log::Dispatch::Screen;
  8. use Log::Dispatch::FileRotate;
  9. use Trog::SQLite;
  10. use Trog::Log::DBI;
  11. use Exporter 'import';
  12. our @EXPORT_OK = qw{is_debug INFO DEBUG WARN FATAL};
  13. our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK );
  14. my $LOGNAME = -d '/var/log' ? '/var/log/www/tcms.log' : '~/.tcms/tcms.log';
  15. $LOGNAME = $ENV{CUSTOM_LOG} if $ENV{CUSTOM_LOG};
  16. my $LEVEL = $ENV{WWW_VERBOSE} ? 'debug' : 'info';
  17. # By default only log requests & warnings.
  18. # Otherwise emit debug messages.
  19. my $rotate = Log::Dispatch::FileRotate->new(
  20. name => 'tcms',
  21. filename => $LOGNAME,
  22. min_level => $LEVEL,
  23. 'mode' => 'append',
  24. size => 10 * 1024 * 1024,
  25. max => 6,
  26. );
  27. # Only send fatal events/errors to prod-web.log
  28. my $screen = Log::Dispatch::Screen->new(
  29. name => 'screen',
  30. min_level => 'error',
  31. );
  32. # Send things like requests in to the stats log
  33. my $dblog = Trog::Log::DBI->new(
  34. name => 'dbi',
  35. min_level => $LEVEL,
  36. dbh => _dbh(),
  37. );
  38. our $log = Log::Dispatch->new();
  39. $log->add($rotate);
  40. $log->add($screen);
  41. $log->add($dblog);
  42. uuid("INIT");
  43. DEBUG("If you see this message, you are running in DEBUG mode. Turn off WWW_VERBOSE env var if you are running in production.");
  44. uuid("BEGIN");
  45. #memoize
  46. my $rq;
  47. sub _dbh {
  48. return Trog::SQLite::dbh( 'schema/log.schema', "data/log.db" );
  49. }
  50. sub is_debug {
  51. return $LEVEL eq 'debug';
  52. }
  53. sub uuid {
  54. my $requestid = shift;
  55. $rq = $requestid if $requestid;
  56. $requestid //= return $rq;
  57. }
  58. #XXX make perl -c quit whining
  59. BEGIN {
  60. our $user;
  61. $Trog::Log::user = 'nobody';
  62. $Trog::Log::ip = '0.0.0.0';
  63. }
  64. sub _log {
  65. my ( $msg, $level ) = @_;
  66. $msg //= "No message passed. This is almost certainly a bug. ";
  67. #XXX Log lines must start as an ISO8601 date, anything else breaks fail2ban's beautiful mind
  68. my $tstamp = strftime "%Y-%m-%dT%H:%M:%SZ", gmtime;
  69. my $uuid = uuid();
  70. return "$tstamp [$level]: RequestId $uuid From $Trog::Log::ip |$Trog::Log::user| $msg\n";
  71. }
  72. sub DEBUG {
  73. $log->debug( _log( shift, 'DEBUG' ) );
  74. }
  75. sub INFO {
  76. $log->info( _log( shift, 'INFO' ) );
  77. }
  78. sub WARN {
  79. $log->warning( _log( shift, 'WARN' ) );
  80. }
  81. sub FATAL {
  82. $log->log_and_die( level => 'error', message => _log( shift, 'FATAL' ) );
  83. }
  84. 1;