Log.pm 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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{log_init is_debug INFO DEBUG WARN FATAL};
  13. our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK );
  14. my $LOGNAME = 'logs/tcms.log';
  15. $LOGNAME = $ENV{CUSTOM_LOG} if $ENV{CUSTOM_LOG};
  16. my $LEVEL = $ENV{WWW_VERBOSE} ? 'debug' : 'info';
  17. our $log;
  18. our $user;
  19. $Trog::Log::user = 'nobody';
  20. $Trog::Log::ip = '0.0.0.0';
  21. sub log_init {
  22. # By default only log requests & warnings.
  23. # Otherwise emit debug messages.
  24. my $rotate = Log::Dispatch::FileRotate->new(
  25. name => 'tcms',
  26. filename => $LOGNAME,
  27. min_level => $LEVEL,
  28. 'mode' => 'append',
  29. size => 10 * 1024 * 1024,
  30. max => 6,
  31. );
  32. # Only send fatal events/errors to prod-web.log
  33. my $screen = Log::Dispatch::Screen->new(
  34. name => 'screen',
  35. min_level => 'error',
  36. );
  37. # Send things like requests in to the stats log
  38. my $dblog = Trog::Log::DBI->new(
  39. name => 'dbi',
  40. min_level => $LEVEL,
  41. dbh => _dbh(),
  42. );
  43. $log = Log::Dispatch->new();
  44. $log->add($rotate);
  45. $log->add($screen);
  46. $log->add($dblog);
  47. uuid("INIT");
  48. DEBUG("If you see this message, you are running in DEBUG mode. Turn off WWW_VERBOSE env var if you are running in production.");
  49. uuid("BEGIN");
  50. return 1;
  51. }
  52. #memoize
  53. my $rq;
  54. sub _dbh {
  55. # Too many writers = lock sadness, so just give each fork it's own DBH.
  56. return Trog::SQLite::dbh( 'schema/log.schema', "logs/db/$$.db" );
  57. }
  58. sub is_debug {
  59. return $LEVEL eq 'debug';
  60. }
  61. sub uuid {
  62. my $requestid = shift;
  63. $rq = $requestid if $requestid;
  64. $requestid //= return $rq;
  65. }
  66. sub _log {
  67. my ( $msg, $level ) = @_;
  68. $msg //= "No message passed. This is almost certainly a bug. ";
  69. #XXX Log lines must start as an ISO8601 date, anything else breaks fail2ban's beautiful mind
  70. my $tstamp = strftime "%Y-%m-%dT%H:%M:%SZ", gmtime;
  71. my $uuid = uuid();
  72. return "$tstamp [$level]: RequestId $uuid From $Trog::Log::ip |$Trog::Log::user| $msg\n";
  73. }
  74. sub DEBUG {
  75. _check_init();
  76. $log->debug( _log( shift, 'DEBUG' ) );
  77. }
  78. sub INFO {
  79. _check_init();
  80. $log->info( _log( shift, 'INFO' ) );
  81. }
  82. sub WARN {
  83. _check_init();
  84. $log->warning( _log( shift, 'WARN' ) );
  85. }
  86. sub FATAL {
  87. _check_init();
  88. $log->log_and_die( level => 'error', message => _log( shift, 'FATAL' ) );
  89. }
  90. sub _check_init {
  91. die "You must run log_init() before using other Trog::Log methods" unless $log;
  92. }
  93. 1;