File: logging.cgi

package info (click to toggle)
libcgi-tiny-perl 1.003-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 516 kB
  • sloc: perl: 1,307; makefile: 2
file content (37 lines) | stat: -rwxr-xr-x 907 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use CGI::Tiny;
use Log::Any;
use Log::Any::Adapter
  {category => 'cgi-script'}, # only log our category here
  File => '/path/to/log/file.log',
  binmode => ':encoding(UTF-8)',
  log_level => $ENV{MYCGI_LOG_LEVEL} || 'info';

my $log = Log::Any->get_logger(category => 'cgi-script');

local $SIG{__WARN__} = sub {
  my ($warning) = @_;
  chomp $warning;
  $log->warn($warning);
};

cgi {
  my $cgi = $_;

  $cgi->set_error_handler(sub {
    my ($cgi, $error, $rendered) = @_;
    chomp $error;
    $log->error($error);
  });

  # only logged if MYCGI_LOG_LEVEL=debug set in CGI server environment
  $log->debugf('Method: %s, Path: %s, Query: %s', $cgi->method, $cgi->path, $cgi->query);

  my $number = $cgi->param('number');
  die "Excessive number\n" if abs($number) > 1000;
  my $doubled = $number * 2;
  $cgi->render(text => "Doubled: $doubled");
};